Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Write a program that reads a text file containing a number of test scores

ID: 3862506 • Letter: I

Question

In C++

Write a program that reads a text file containing a number of test scores into an array and calculates the average and median of the scores. Define a constant to hold the array size and use it to declare the array that holds the scores.

const int MAX_SCORES = 40;

You will need to implement the following functions:

int getScores (int scores[], int array_size, int& num_scores);

This function should open the file lab6.txt and read the scores into an array. The function will return SUCCESS if it opens the file successfully and ERROR if the file cannot be open. Define SUCCESS and ERROR as global constants in your program.

array_size is the maximum number of scores that you can read from the file. You need it to make sure you don't overstep the array boundaries. Even if the file has more than array_size scores, you can't read them all. The limit is array_size.

num_scores will have the actual number of scores read from the file. You need it in case the actual number of scores found in the input file is less than the array size.

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

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

double calcMedian (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.

Implement the SelectionSort function

Explanation / Answer

// C++ code
using namespace std;
#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>

#define SUCCESS 1
#define ERROR -1
const int MAX_SCORES = 40;

int getScores (int scores[], int array_size, int& num_scores);
double calcAverage (int scores[], int num_scores);
double calcMedian (int scores[], int num_scores);

int main()
{
int array_size = MAX_SCORES;
int scores[array_size];
int num_scores;

int result = getScores(scores,array_size,num_scores);
if(result == -1)
{
cout << "ERROR ";
return 0;
}

double average = calcAverage(scores, num_scores);
double median = calcMedian(scores, num_scores);

cout << "Average: " << average << " Median: " << median << endl;
}


int getScores (int scores[], int array_size, int& num_scores)
{
num_scores = 0;
ifstream inFile ("lab6.txt");
if (inFile.is_open())
{
while (true)
{
inFile >> scores[num_scores];
num_scores++;

if(inFile.eof() || num_scores == MAX_SCORES)
break;
}
inFile.close();
}
else
{
cout << "Unable to open file ";
return -1;
}

return 1;
}

//This function should calculate and return the average of the scores passed as parameters
double calcAverage (int scores[], int num_scores)
{
double average = 0;
for (int i = 0; i < num_scores; ++i)
{
average = average + scores[i];
}

return (average)/num_scores;
}

// This function should return the median value in the array. To get the median, the array must be sorted first.
double calcMedian (int scores[], int num_scores)
{
// selection sort
for (int i = 0; i < num_scores-1; i++)
{
// minimum element in unsorted scores
int minimumIdx = i;
for (int j = i+1; j < num_scores; j++)
if (scores[j] < scores[minimumIdx])
minimumIdx = j;

// Swap min element with first index value
int t = scores[minimumIdx];
scores[minimumIdx] = scores[i];
scores[i] = t;
}

double median;

if(num_scores%2 == 0)
median = (scores[(num_scores/2)-1] + scores[num_scores/2])/2.0;
else
median = scores[num_scores/2];

return median;
}


/*
lab6.txt
67
87
88
90
89
78
67
66
89
98

output:
Average: 81.9
Median: 87.5

*/