find min, max, median and mode from a file that contains 40 numbers

Hello everyone,

I have trouble doing this homework. I'm supposed to read a file that contains 40 numbers as scores and then find its highest, lowest, average, median and mode.
But somehow, my program doesn't return the right values and for median and mode the program is just crashed. I've been working on it for 2 days and asked my classmates, but still don't know why. Hope someone here could help me out.Also I apologize if it looks messy here. I'm new and I don't know how to use the format function of the site.

Thanks!


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <string>
#include <fstream>


using namespace std;


//prototypes:

int getScores (int scores[], int max_scores );

void selectionSort (int scores[], int num_scores);

int findHighest (int scores[], int num_scores);

int findLowest (int scores[], int num_scores);

double calcAverage (int scores[], int num_scores);

double getMedian (int scores[], int num_scores);

int getMode (int scores[], int num_scores);




int main()
{
    int option; // option?

    const int max_scores = 40;
    int scores [max_scores];
    int num_scores;
    int i, highest, lowest, average, median, mode;



    cout << "Hello! \n"; // Greeting!

    num_scores = getScores (scores, max_scores);

    do
    {
        cout << "This program can calculate \n"; // Ask user to choose an option
        cout <<"\n";
        cout << "\t(1) Retrieve highest score \n" ; //(1) find highest score
        cout <<"\n";
        cout << "\t(2) Retrieve lowest score \n" ; // (2) find lowest score
        cout <<"\n";
        cout << "\t(3) Calculate the mean \n" ; // (3) calculate the average
        cout <<"\n";
        cout << "\t(4) Calculate the median \n" ; // (4) find median
        cout <<"\n";
        cout << "\t(5) Calculate the mode  \n" ; // (5) find mode
        cout <<"\n";
        cout << "\t(6) Exit program\n" ; // (7) terminate program
        cout <<"\n";
        cout << "Enter your option here : "; // Ask user to enter his option (1-6)
        cin >> option;
        cout <<"\n";
        cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
        cout <<"\n";
        cout <<"\n";



        switch (option)
        {
            case 1:
            highest = findHighest (scores, num_scores);
            cout << "The highest score is " << highest << endl;
            break;


            case 2:
            int lowest;
            lowest = findLowest (scores, num_scores);
            cout <<"The lowest score is " << lowest <<endl;
            break;


            case 3:
            average = calcAverage (scores, num_scores);
            cout <<"The average score is " << average <<endl;
            break;




            case 4:
            selectionSort (scores, num_scores);
            median = getMedian (scores, num_scores);
            cout << "The median is " <<median <<endl;
            break;


            case 5:
            selectionSort (scores, num_scores);
            mode = getMode (scores, num_scores);
            cout << "The mode is " << mode <<endl;
            break;


            case 6:
            return 0;

            default:
            cout << "You entered an invalid option."; // print the result
            cout << "\n";
            cout << "\n";
            return 0;
        }
        cout <<"\n";
        cout <<"\n";
        cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
    }
    while (option<7);

    return 0;
}


int getScores (int scores[], int max_scores)
{

    // Define file stream objects.
    ifstream inFile;
    string filename;

    cout <<"Please enter filename: ";
    cin >> filename;


    // Open the files.
    inFile.open("lab6.txt");




    if (filename == "lab6.txt")
    {

        for( int i = 0; i < max_scores; i++)
        {
            while (inFile >> scores [i])
            {
            cout << scores[i] <<" " << " ";
            }
            return scores [i];
        }


    inFile.close();

    }

    // If the files unsuccessfully opened, process it.
    else
    {
        //Display an error message.
        cout <<"\nError opening file.\n";
        return 0;
    }


}

int findHighest (int scores[], int num_scores)
{

int highest = scores[0]; // highest?

for (int i = 1; i < num_scores; i++)// highest
{
    if (scores[i] > highest)
    {
        highest = scores [i];
    }
    return highest;
    }

}


int findLowest (int scores[], int num_scores)
{
    int lowest = scores[0];

    for (int i = 1; i < num_scores; i++)
    {
        if (scores[i] < lowest)
        {
            lowest = scores[i];
        }
    return lowest;
    }
}





double calcAverage (int scores[], int num_scores)
{

    int sum;
	double avg;
    for (int i = 0; i < num_scores; i++)
    {
        sum = sum + scores[i];
	    avg = (sum/num_scores);

	}
      return avg;//returns the average

}
void selectionSort (int scores[], int num_scores)
{
    int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (num_scores - 1); startScan++)
    {
        minIndex = startScan;
        minValue = scores [startScan];
        for (int index = startScan + 1; index < num_scores; index++)
        {
            if (scores [index] <minValue)
            {
                minValue = scores[index];
                minIndex = index;
            }
        }
        scores [minIndex] = scores [startScan];
        scores [startScan] = minValue;
    }

}


double getMedian (int scores[], int num_scores)
 {
     int median;
    if ((num_scores % 2) == 0)
    {
        median = (scores[num_scores/2] + scores[(num_scores/2) - 1])/2.0;
        return median;
    }

    else
    {
        median = scores[num_scores/2];
        return median;
    }
}

int getMode (int scores[], int num_scores)
 {
int number = scores[0];
int mode = number;
int count = 1;
int countMode = 1;

for (int i=1; i<num_scores; i++)
{
      if (scores[i] == number)
      { // count occurrences of the current number
         countMode++;
      }
      else
      { // now this is a different number
            if (count > countMode)
            {
                  countMode = count; // mode is the biggest ocurrences
                  mode = number;
            }
           count = 1; // reset count for the new number
           number = scores[i];
  }
}

cout << "mode : " << mode << endl;
}

Last edited on
I've not gone through the entire program, but just looked at a couple of the functions and some errors are visible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int findHighest (int scores[], int num_scores)
{
    int highest = scores[0]; // highest?

    for (int i = 0; i < num_scores; i++)// highest
    {
        if (scores[i] > highest)
        {
            highest = scores [i];
        }
        return highest;
    }

}


Above, the return statement at line 11 is inside the loop. That means it will be executed on the very first iteration. It should be moved out of the loop and placed just before the closing brace at line 13.

There's a similar error in functions findLowest() and calcAverage(). I've not gone through all of the code, but I think you need to go through the code slowly and verify the results step by step, one function at a time.

By the way, the code will be much easier to read, for everyone, if you use code tags, like this:

[code]your code here[/code]

(It would also be better if the distracting and misleading comments referring to circles and rectangles were deleted).


Edit: A few more issues I noticed...
Here, num_scores is assigned whatever value is returned by getScores().
int num_scores = getScores (scores, max_scores); but unfortunately that function doesn't return a value (except when it thinks there was an error, in which case it returns 0).

The program doesn't correctly check whether or not the file was opened properly. This won't do it:
 
    if (filename == "lab6.txt")
Make that instead something like this,
1
2
3
4
5
    if ( !inFile.is_open() )    
    {
        cout <<"\nError opening file.\n";
        return 0;
    }


Function getMode() also is supposed to return a value, but does not.



Last edited on
my program doesn't return the right values

This suggested to me that you weren't reading the numbers in correctly. "Garbage in, garbage out" as they say. Sure enough, look at the code that reads the file:
1
2
3
4
5
6
7
8
        for( int i = 0; i < max_scores; i++)
        {
            while (inFile >> scores [i])
            {
            cout << scores[i] <<" " << " ";
            }
            return scores [i];
        }

The first time though the for loop it executes the while loop, reading all the values and putting one after the other into scores[0]. Then it returns the value of scores[0], which is whatever the last value in the file was.

I suggest that you start by fixing this. Remember, you want to read the values in, AND you have to return the count of the number of values read.

Then, for debugging, add a 7th option to the switch statement in main() that just prints out the input:
1
2
3
4
5
6
case 7:
    for (int i=0; i<num_scores; ++i) {
        cout << scores[i];
    }
    cout << endl;
    break;

Now you can make sure you have the input data correct.

In calcAverage(), sum in uninitialized. Set it to zero before entering the loop. Also, there's no need to calculate avg until you exit the loop.
Topic archived. No new replies allowed.