diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | Q2.cpp | 32 | ||||
-rw-r--r-- | get_maximum.cpp | 36 | ||||
-rw-r--r-- | get_mean.cpp | 25 | ||||
-rw-r--r-- | get_minimum.cpp | 34 | ||||
-rw-r--r-- | mysqrt.cpp | 4 | ||||
-rw-r--r-- | mysqrt.h | 2 | ||||
-rw-r--r-- | statistics.h | 17 |
8 files changed, 151 insertions, 3 deletions
@@ -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 @@ -0,0 +1,32 @@ + +#include <iostream> + +#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; +} @@ -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; } @@ -3,6 +3,8 @@ #define MYSQRT_H #include <cmath> +#include <cstdlib> +#include <ctime> 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 <iostream> +#include <fstream> +#include <ctime> +#include <cstdlib> + +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 |