diff options
author | Michael Abed <michaelabed@gmail.com> | 2012-03-03 23:24:06 -0500 |
---|---|---|
committer | Michael Abed <michaelabed@gmail.com> | 2012-03-03 23:24:06 -0500 |
commit | 12993b517ff2f8ec6608c2deaf3e988c64f7f164 (patch) | |
tree | 314fc88490a036a3853f38d9c8649ef5d37776c8 /get_mode.cpp | |
parent | 973562e3bbffbe2502649ce30d55e7cc6cbc6c3c (diff) | |
download | ec327-lab2-12993b517ff2f8ec6608c2deaf3e988c64f7f164.tar.gz ec327-lab2-12993b517ff2f8ec6608c2deaf3e988c64f7f164.tar.bz2 ec327-lab2-12993b517ff2f8ec6608c2deaf3e988c64f7f164.zip |
finished for now
Diffstat (limited to 'get_mode.cpp')
-rw-r--r-- | get_mode.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/get_mode.cpp b/get_mode.cpp new file mode 100644 index 0000000..edf0958 --- /dev/null +++ b/get_mode.cpp @@ -0,0 +1,56 @@ + +#include "statistics.h" + +using namespace std; + +float get_mode(const char *filename) +{ + ifstream in; + in.open(filename); + + unsigned size = 1024; + int *data = new int[size]; + + int i = 0; + int max = 0; + + while (!in.eof()) { + in >> data[i]; + if (data[i] > max) + max = data[i]; + i++; + + if (i >= size) { // have to reallocate array if we get too big + size *= 2; + int *newdata = new int[size]; + for (int j = 0; j <= i; j++) + newdata[j] = data[j]; + delete data; + data = newdata; + } + } + i--; + int *freq = new int[i]; + + int j; + for (j = 0; j < i; j++) + freq[j] = 0; + + for (j = 0; j < i; j++) + freq[data[j]]++; + + float mode = 0; + max = 0; + for (j = 0; j < i; j++) { + if (freq[j] > max) { + max = freq[j]; + mode = (float)j; + } + } + + in.close(); + delete freq; + delete data; + + return mode; +} |