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

Write a program that reads a text file containing a set of test scores, calculat

ID: 3816754 • Letter: W

Question

Write a program that reads a text file containing a set of test scores, calculates the average, median and mode. The file name must be entered by the user. Use the main function provided and implement all other functions.

getScores:

Ask the user for the file name and use it to open the file containing the scores. If the file opening was unsuccessful, ask the user to enter it again until you succeed or return zero to main.

Read all scores to a vector. You will need to pass the vector by reference to this function.

calcAverage:

Calculate the average by adding all scores and dividing by the number of scores.

calcMedian:

Calculate the median. This function should test whether the file has an even or odd number of scores. If there is an odd number of scores, the median is the score in middle, given that the scores are sorted. If there is an even number of scores, you need to average the two in the middle.

calcMode:

Determines the score that appears more often in the data set.

print:

The scores sorted from least to greatest

The average (rounded to one-decimal place), median(rounded to one-decimal place) and mode

You will need to sort the scores before calculating the median. Use the sort function to sort the vector. Refer to http://www.cplusplus.com/reference/algorithm/sort/ () to learn how to use sort.

Explanation / Answer

Hi

The below code works perfectly for the required question.

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


using namespace std;
int CalcAverage(int score[],int size);
int ClacMedian(int score[],int size);
int CalcMode(int score[], int size);
void print(int score[], int size);
int main()
{
bool run = true;
bool run1=true;
string filename;
int score[100]={0};
int size=0, choice=0;
while(run1)
{
cout << "Name your file> ";
getline (cin, filename);
fstream infile(filename, ios::in);
if (!inFile)
{
cout << "File did not open correctly" << endl;
}
else
{
while(!infile.eof()&&size<100)
{
infile>>score[size++];
infile.clear();
infile.ignore();
  
}
infile.close();
run=false;
}
}
cout<<"Average Marks of student"<<CalcAverage(score,size)<<endl;
cout<<"Median of the marks"<<CalcMedian(score,size)<<endl;
cout<<"Score in ascending order"<<print(score,size)<<endl;
}
int CalcAverage(int score[],int size)
{
int sum =0;
int avg;
for (int i=1;i<=size;i++)
{
sum=sum+score[i];
}
avg=sum/size;
return avg;
}
int ClacMode(int score[],int size)
{
int number = score[1];
int mode = number;
int count = 1;
int countMode = 1;
int i,j,temp;
  
for(i=1;i<=size;i++) //sorting the array because the below algo for mode is for sored array
{
for(j=1;j<=size-i;j++)
{
if(score[j]>score[j+1])
{
temp=score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
}   

for (int i=2; i<=size; i++)
{
if (score[i] == number)
{
++count;
}
else
{
if (count > countMode)
{
countMode = count;
mode = number;
}
count = 1;
number = array[i];
}
}
return mode;
}

int CalcMedian(int score[], int size)
{
int i,j,temp;
int median, middle;
middle=size/2;
  
for(i=0;i<=size;i++)
{
for(j=0;j<=size-i;j++)
{
if(score[j]>score[j+1])
{
temp=score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
}
if (size % 2)

{

median = (score[middle] + score[middle + 1]) / 2;

}

else

{

median = score[middle+1];


}

return median;
}

void print(int score[],int size)
{
int i,j,temp;
  
for(i=1;i<=size;i++)
{
for(j=1;j<=size-i;j++)
{
if(score[j]>score[j+1])
{
temp=score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
}   
for(i=1;i<=size;i++)
{
coutt<<score[i]<<" ";
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote