In C++ Write a program that reads a text file containing a number of test scores
ID: 3862505 • 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
#include <iostream>
#include <iomanip>
#include <fstream>
#define SUCCESS 1
#define ERROR -1
using namespace std;
//The student's final score will be calculated as the average of the students individual scores.
double calcAverage (int scores[], int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
sum += scores[i];
return sum / n;
}
//Implement the SelectionSort function taught in class
void SelectionSort(int A[], int SIZE)
{
int i, j;
for(i = 0; i < SIZE-1; i++) //For each element.
{
int min = i; //Assumes the first element in the remaining array as min.
for(j = i; j < SIZE; j++) //Find the position of least element in remaining array.
if(A[j] < A[min])
min = j;
double temp = A[i]; //Exchange the first element in the array, with minimum element.
A[i] = A[min];
A[min] = temp;
}
}
//The median score is generated after sorting the list of final scores.
//This function should calculate and return the average of the scores passed in the parameter list.
double calcMedian(int scores[], int n)
{
SelectionSort(scores, n);
if(n % 2 == 0)
return (scores[n/2] + scores[n/2-1]) / 2;
else
return scores[n/2];
}
//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.
int getScores (int scores[], int array_size, int& num_scores)
{
ifstream fin;
fin.open("lab6.txt");
if(!fin.is_open())
return ERROR;
while(!fin.eof() && num_scores < array_size)
fin>>scores[num_scores++];
return SUCCESS;
}
int main()
{
//Define a constant MAX_SCORES = 10 and use it to define an array of scores.
const int MAX_SCORES = 10;
int scores[MAX_SCORES];
//Define a constant MAX_STUDENTS and use it to define the array of final scores.
const int MAX_STUDENTS = 12;
//double finalScores[MAX_STUDENTS];
//ifstream fin;
//string name;
//string fileName;
//StudentScores.txt holds the details.
//cout<<"Enter the name of the file: ";
//cin>>fileName;
//fin.open(fileName);
int count = 0;
/*while(!fin.eof())
{
fin>>name;
int sum = 0;
for(int i = 0; i < MAX_SCORES; i++)
fin>>scores[i];
//Store each student's average in an array of final scores.
finalScores[count] = calcAverage(scores, MAX_SCORES);
count++;
}*/
if(getScores(scores, MAX_SCORES, count) == ERROR)
{
cout << "Unable to open the file." << endl;
return 0;
}
//Print each student's final score and calculate the class average and median.
/*cout<<"The final scores of students is: ";
for(int i = 0; i < MAX_STUDENTS; i++)
cout<<fixed<<setprecision(2)<<finalScores[i]<<" ";
cout<<endl; */
cout<<"The class average is: "<<fixed<<setprecision(2)<<calcAverage(scores, count)<<endl;
cout<<"The median of the class is: "<<fixed<<setprecision(2)<<calcMedian(scores, count)<<endl;
}