Use arrays to find mean median mode with inout text file

Write a program that reads a text file named lab6.txt containing a number of test scores into an array and waits for the user’s request to do any of the following:

Retrieve highest score

Retrieve lowest score

Calculate average

Calculate median

Calculate mode

You will need to implement the following functions:

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

This function should open the file lab6.txt and read the scores into an array (max_scores is the size of the array, so you should not read more then that number of scores). The function will return the number of scores actually read from the file.

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

This function should calculate and return the average of the scores passed as parameter.

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

This function should return the highest of the scores passed as parameter.

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

This function should return the lowest of the scores passed as parameter.

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

This function should return the median value in the array. To get the median, the array must be sorted first.

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

This function returns the value that is the most frequent in the array scores. Remember to compute the frequency array first.

Implement the SelectionSort function taught in class.

Make sure to test the return of getScores before you accept user’s requests. Also, let the user enter the filename instead of hardcoding it in your program.

Download lab6.txt from Catalyst.

Make sure the program compiles and runs correctly. You will be graded on the following rubric:

There is a header comment at the beginning of the source code and a comment explaining what each function does right above the header: 10%

Each function, including main, is implemented correctly: 70%

The program compiles and runs with no errors or warnings: 10%

The output is correct and legible: 10%


Here is what I have so far:

/* Reads a text file named lab6.txt containing a number of test scores into an array and waits for the user’s request to:
Retrieve highest score and lowest score,Calculate average, median and mode
CIS 22A
Lab Number #6
Aisha Perwez
6/23/14 */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Declare function prototypes for the four functions

int findHighest (int scores[], int num_scores);
// This function should return the highest of the scores passed as parameter.

int findLowest (int scores[], int num_scores);
// This function should return the lowest of the scores passed as parameter.

double getMedian (int scores[], int num_scores);
// This function should return the median value in the array. To get the median, the array must be sorted first.

int getMode (int scores[], int num_scores);
// This function returns the value that is the most frequent in the array scores.

void selectionSort(int array[], int size)

int main()

{
const int MAX_SCORES = 40;

ifstream inFile; // This is a declaration of the inFile that holds the names and corresponding grades
ofstream outFile; // This is a declaration of the inFile that holds the names, highest, lowest, and average test score
int formula_selection; //Variable prompts user to chose a formula from the menu
int startScan, mindIndex, minValue;
string filename;
int scores[MAX_SCORES];
int numScores;
int highest;
double Avg; //Holds average of the 3 test scores


do {
cout << "\n**************************************\n";
cout << "\nWhat do you wish to do?\n";
cout << "1. Retrieve highest score\n";
cout << "2. Retrieve lowest score\n";
cout << "3. Calculate average\n";
cout << "4. Calculate median\n";
cout << "5. Calculate mode\n";

for (startScan = 0; startScan < (size - 1); startScan++)
{

minIndex = startScan;
minValue = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if arrary[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}

cout << "Name your file> ";

getline (cin, filename);


fstream file ( filename.c_str());

// Check to make sure the file opened correctly

if (!inFile)
{
cout << "File did not open correctly" << endl;
return EXIT_FAILIURE;
}

else
{
inFile >> score;

highest = findHighest (scores, numScores);

cout << "*************************************************"; // This is what will be displayed in the outfile
cout << "\n\n The average of the students' test scores is: " << Avg;
cout << "\n\n The student's highest test score is: " << Max;
cout << "\n\n The student's lowest test score is: " << Min;
cout << "\n\n**********************************************";
}

inFile.close(); //Good programming practice to close all files after using
}

return EXIT_SUCCESS;
}



This is what I have far so confused what order to put my things in, and I do not know how to call the function
closed account (j3Rz8vqX)
Not sure what you mean by other, but normally, you'd read the data from file first.

After reading data, you should probably display the available options and provide the features when prompt for.

Example: (Non dynamic array)
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
#include <iostream>
#include <fstream>
using namespace std;

//Function Prototypes:
void printArray(int score[], int size);
bool myLess(int score,int value);
bool myGreater(int score, int value);
int compare(int score[], int size, bool (*comparison)(int,int));
bool prompt(int &choice);

//Main program:
int main()
{
    bool run = true;
    int score[100]={0},//Set first element to 0 and default the remaining to 0.
        size=0, choice=0;
    fstream infile("data.txt", ios::in);
    while(!infile.eof()&&size<100)
    {
        infile>>score[size++];
        infile.clear();
        infile.ignore();
    }
    infile.close();
    while(run)
    {
        while(prompt(choice)&&(choice<1||choice>7))//0 is also invalid
            cout<<"Error, unavailable choice, try again\n";
        switch(choice)
        {
        case 1:
            cout<<"Highest score: "<<compare(score,size,myGreater)<<'\n';
            break;
        case 2:
            cout<<"Lowest score: "<<compare(score,size,myLess)<<'\n';
            break;
        case 3:
        case 4:
        case 5:
            cout<<"Not implemented.\n";
            break;
        case 6:
            printArray(score,size);
            break;
        case 7:
            run = false;
            break;
        }
    }
    return 0;
}

//Function Definitions:
bool prompt(int &choice)
{
    cout << "\n**************************************\n";
    cout << "\nWhat do you wish to do?\n";
    cout << "1. Retrieve highest score\n";
    cout << "2. Retrieve lowest score\n";
    cout << "3. Calculate average\n";
    cout << "4. Calculate median\n";
    cout << "5. Calculate mode\n";
    cout << "6. Print array\n";
    cout << "7. Exit\n";
    cout << "Choice: ";
    cin>>choice;
    cin.clear();
    cin.ignore(1000,'\n');
    return true;
}
int compare(int score[], int size, bool (*comparison)(int,int))
{
    int value=score[0];
    for(int i=1;i<size;++i)
        if(comparison(score[i],value))
            value=score[i];
    return value;
}
bool myGreater(int score, int value)
{
    return (score>value);
}
bool myLess(int score,int value)
{
    return (score<value);
}
void printArray(int score[], int size)
{
    for(int i=0;i<size;++i)
        cout<<score[i]<<' ';
    cout<<'\n';
}

**************************************

What do you wish to do?
1. Retrieve highest score
2. Retrieve lowest score
3. Calculate average
4. Calculate median
5. Calculate mode
6. Print array
7. Exit
Choice: 6
25 64 35 0 88 0 41 0 1 2 8 1 6 9 4 8 0

**************************************

What do you wish to do?
1. Retrieve highest score
2. Retrieve lowest score
3. Calculate average
4. Calculate median
5. Calculate mode
6. Print array
7. Exit
Choice: 7

Process returned 0 (0x0)   execution time : 6.783 s
Press any key to continue.

"data.txt":
25 64 35 0 88 d 41 k
1 2 8 1 6 9 4 8


Line 3 from the text file ' ' produced 0 at the end of the array.

Tips:
Median and mode: http://www.purplemath.com/modules/meanmode.htm
Last edited on
Hi, thank you so much for your response. It worked 100% and gave me the output screen results I needed.

However, I have to use these specified functions that return these parameters.
int getScores (int scores[], int max_scores);
double calcAverage (int scores[], int num_scores);
int findHighest (int scores[], int num_scores);
int findLowest (int scores[], int num_scores);
double getMedian (int scores[], int num_scores);
int getMode (int scores[], int num_scores);

So even though I have the correct outptut, I have to use these functions instead of
void printArray(int score[], int size);
bool myLess(int score,int value);
bool myGreater(int score, int value);
int compare(int score[], int size, bool (*comparison)(int,int));
bool prompt(int &choice);

Also, your feedback reads my input file correctly, however I cannot hardcode the file, I have to ask the user to enter the file.
Even though I have to have input file in a "Lab6.txt" document with the following info
85 74 71 99 92 89 78 82 90 85 67 71 98 87 72 77 64 78 85 84
91 87 66 70 77 73 66 72 85 83 82 74 78 68 79 89 93 96 95 86

Thank you for your time
closed account (j3Rz8vqX)
confused what order to put my things in, and I do not know how to call the function
It was not intended to substitute your HW but to demonstrate a possible flow and illustrate function calls.

I cannot hardcode the file
1
2
3
cout << "Name your file> ";
getline (cin, filename);
fstream file ( filename.c_str(), ios::in);//Only read, assuming no writing 


Tips:
Your functions arguments are: an array of integers called score[] and an integer that stores the size of your array, called num_scores; not sure why the first would be called max_scores, I would think it is the same - size of the array needs to be known to operate on the array.

Remember to stick to your design; I do not provide HW, but rather examples.
Topic archived. No new replies allowed.