min value issue /how to get mode and median

so im trying to find the min, max, range, mean, median, and mode. The max number is right but the min isnt. Once that is corrected, i can get range and mean easily. Also, i have no idea how to get mode and median. Im guessing the values in the arr2 would have to be sorted. Any thoughts? Id appreciate it.
This refers to the values in arr2 which are the calculated values in the formula. You can enter -2 and 2 for userMin and userMax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream> 
#include <iomanip>

using namespace std;

int main()
{

   cout << fixed << setprecision(1);
   float userMin;
   float userMax;

   const int SIZE = 21;
   float arr[SIZE];
   const int SIZE2 = 21;
   float arr2[SIZE2];
   int count = 0;
   cout << "enter min ";
   cin >> userMin;
   cout << "enter max ";
   cin >> userMax;
   float inc = (userMax - userMin) / (SIZE-1);
   float x = inc*count + userMin;

   double total = 0;
   double mn=arr2[0];
   double mx=arr2[0];
   float range;
   float median;
   float mode;
  

   for (; x <= userMax;)
   {
      for (int e = 0; e < SIZE; e++)
      {
         arr[e] = x;
         arr2[e] = (0.0572*cos(4.667*x) + 0.0218*cos(12.22*x));
         cout << setw(15) << setprecision(1)<< arr[e]
            << setw(15) << setprecision(3) << arr2[e]
            << endl;
         total += arr2[e];

         count++;
         x = userMin + inc*count;
         if (mn > arr2[e])
         {
            mn = arr2[e];
         }

         if (mx < arr2[e])
         {
            mx = arr2[e];
         }



      }
   }

   cout << endl;
   cout << "min " << mn << endl;
   cout << "max " << mx << endl;
   //cout << "mode" << mode << endl;
   //cout << "median " << median << endl;
   cout << "mean " << total / SIZE << endl;
   cout << "range " << mx - mn << endl;
   return 0;
}
Last edited on
For median you are going to need to sort your code and then just divide your array in half. Whichever value is at the half point will be your median. If your array has an even number of values, you are going to have to make an arbitrary choice on whether the higher or lower value should be picked as the median.

As for mode, that requires that each array element also has a way of keeping track of it's occurrences. You can do this multiple ways, create a secondary array that holds the number of occurrences of the first array with the data. You could create a simple struct to hold the data and a counter for the number of times the data has been entered. Your choice really.
ok, can you tell me why the minimum doesnt display the correct value but the maximum does?
Topic archived. No new replies allowed.