From f2566da91396bc07356601794fd5db858a78f1f3 Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Sat, 3 Mar 2012 23:32:27 -0500 Subject: make basically everything work --- Makefile | 4 +++- Q2.cpp | 32 ++++++++++++++++++++++++++++++++ get_maximum.cpp | 36 ++++++++++++++++++++++++++++++++++++ get_mean.cpp | 25 +++++++++++++++++++++++++ get_minimum.cpp | 34 ++++++++++++++++++++++++++++++++++ mysqrt.cpp | 4 ++-- mysqrt.h | 2 ++ statistics.h | 17 +++++++++++++++++ 8 files changed, 151 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 0eafcb5..817c0e2 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,10 @@ Q1: Q1.cpp mysqrt.cpp mysqrt.h g++ -o Q1 mysqrt.cpp Q1.cpp +Q2: Q2.cpp write_data.cpp statistics.h get_mean.cpp get_mode.cpp get_maximum.cpp get_minimum.cpp + g++ -o Q2 write_data.cpp get_mean.cpp get_mode.cpp get_maximum.cpp get_minimum.cpp Q2.cpp + Q3a: Q3a.cpp g++ -o Q3a Q3a.cpp Q3b: Q3b.cpp - g++ -o Q3b Q3b.cpp diff --git a/Q2.cpp b/Q2.cpp index e69de29..30fd0a9 100644 --- a/Q2.cpp +++ b/Q2.cpp @@ -0,0 +1,32 @@ + +#include + +#include "statistics.h" + +using namespace std; + +int main(int argc, const char *argv[]) +{ + char input[256]; // max filename length for most systems + + float mean, min, max, mode; + + cout << "Please input a filename to use for data" << endl; + cout << ">> "; + cin >> input; + + if (argc > 1) + write_data(input, 1000, 500); + + mean = get_mean(input); + min = get_minimum(input); + max = get_maximum(input); + mode = get_mode(input); + + cout << "Mean: " << mean << endl; + cout << "Min: " << min << endl; + cout << "Max: " << max << endl; + cout << "Mode: " << mode << endl; + + return 0; +} diff --git a/get_maximum.cpp b/get_maximum.cpp index e69de29..c98d897 100644 --- a/get_maximum.cpp +++ b/get_maximum.cpp @@ -0,0 +1,36 @@ + +#include "statistics.h" + +using namespace std; +float get_maximum(const char *filename) +{ + ifstream in; + in.open(filename); + + unsigned size = 1024; + int *data = new int[size]; + + int i = 0; + + while (!in.eof()) { + in >> data[i]; + i++; + + if (i >= size) { // reallocate. Where is realloc in C++? + size *= 2; + int *newdata = new int[size]; + for (int j = 0; j <= i; j++) + newdata[j] = data[j]; + delete data; + data = newdata; + } + } + + i--; + int max = data[0]; + for (int j = 0; j < i; j++) + if (data[j] > max) + max = data[j]; + + return max; +} diff --git a/get_mean.cpp b/get_mean.cpp index e69de29..f62f606 100644 --- a/get_mean.cpp +++ b/get_mean.cpp @@ -0,0 +1,25 @@ + +#include "statistics.h" + +using namespace std; + +float get_mean(const char *filename) +{ + ifstream in; + in.open(filename); + + int count = 0; + float sum = 0; + + int cur = 0; + + while (!in.eof()) { + in >> cur; + sum += cur; + count++; + } + + in.close(); + + return sum/count; +} diff --git a/get_minimum.cpp b/get_minimum.cpp index e69de29..e994c98 100644 --- a/get_minimum.cpp +++ b/get_minimum.cpp @@ -0,0 +1,34 @@ +#include "statistics.h" + +using namespace std; +float get_minimum(const char *filename) +{ + ifstream in; + in.open(filename); + + unsigned size = 1024; + int *data = new int[size]; + + int i = 0; + + while (!in.eof()) { + in >> data[i]; + i++; + + if (i >= size) { // reallocate. Where is realloc in C++? + size *= 2; + int *newdata = new int[size]; + for (int j = 0; j <= i; j++) + newdata[j] = data[j]; + delete data; + data = newdata; + } + } + + i--; + int min = data[0]; + for (int j = 0; j < i; j++) + if (data[j] < min) + min = data[j]; + return min; +} diff --git a/mysqrt.cpp b/mysqrt.cpp index 18bb632..ddcda6f 100644 --- a/mysqrt.cpp +++ b/mysqrt.cpp @@ -7,7 +7,7 @@ double mysqrt(double x) bool goodenough = false; double lastGuess, nextGuess; - lastGuess = x / 10.0; + lastGuess = x / 2; if (x <= 0) return x; @@ -15,9 +15,9 @@ double mysqrt(double x) while (!goodenough) { nextGuess = (lastGuess + (x / lastGuess)) / 2.0; + goodenough = abs(nextGuess - lastGuess) <= 0.00001; lastGuess = nextGuess; - goodenough = abs(nextGuess - lastGuess) <= 0.000000000001; } return nextGuess; } diff --git a/mysqrt.h b/mysqrt.h index 459cc99..23ca4a9 100644 --- a/mysqrt.h +++ b/mysqrt.h @@ -3,6 +3,8 @@ #define MYSQRT_H #include +#include +#include double mysqrt(double x); diff --git a/statistics.h b/statistics.h index e69de29..042e52d 100644 --- a/statistics.h +++ b/statistics.h @@ -0,0 +1,17 @@ + +#ifndef STATISTICS_H +#define STATISTICS_H + +#include +#include +#include +#include + +void write_data(const char *filename, unsigned int N, unsigned int M); + +float get_mean(const char *filename); +float get_minimum(const char *filename); +float get_maximum(const char *filename); +float get_mode(const char *filename); + +#endif -- cgit v1.2.3-54-g00ecf