summaryrefslogtreecommitdiff
path: root/get_mode.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'get_mode.cpp')
-rw-r--r--get_mode.cpp56
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;
+}