Write a program that read the given file StudentScores.txt. Store the scores for
ID: 3822991 • Letter: W
Question
Write a program that read the given file StudentScores.txt. Store the scores for each student in an array (this should be done automatically by the program). Your program will calculate the following results and display the results on the screen. The lowest number in the array The highest number in the array The total of the numbers in the array (use integer type for total) The average of the numbers in the array (use integer type for average)
The grade based on the average according to the following grading scale
Test Score Letter Grade 90–100 A 80–89 B 70–79 C 60–69 D 0–59 F
For example, if the following is the records in StudentScores.txt
Kate 80 83 83 82 90 88
Tom 79 70 70 73 66 69
Andrew 90 92 94 92 90 87
The results displayed on the screen should look like the following.
Name lowest highest total average grade Kate 80 90 506 84 B Tom 66 79 427 71 C Andrew 87 94 548 91 A
using c++
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int highest(int arr[], int n)
{
int high = arr[0];
for(int i = 1; i < n; i++)
{
if (arr[i] > high)
{
high = arr[i];
}
}
return high;
}
int lowest(int arr[], int n)
{
int low = arr[0];
for(int i = 1; i < n; i++)
{
if (arr[i] < low)
{
low = arr[i];
}
}
return low;
}
int total(int arr[], int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
int average(int sum, int n)
{
return sum/n;
}
char grade(int average)
{
if (average >= 90)
{
return 'A';
}
else if(average >= 80)
{
return 'B';
}
else if(average >= 70)
{
return 'C';
}
else if(average >= 60)
{
return 'D';
}
else
{
return 'F';
}
}
int main()
{
ifstream myfile("StudentScores.txt");
string name;
int scores[6];
int n = 6;
cout << "Name lowest highest total average grade" << endl;
while(myfile >> name)
{
for(int i = 0; i < n; i++)
{
myfile >> scores[i];
}
int high = highest(scores, n);
int low = lowest(scores, n);
int sum = total(scores, n);
int avg = average(sum, n);
char grd = grade(avg);
cout << name << " " << low << " " << high << " " << sum << " " << avg << " " << grd << endl;
}
myfile.close();
return 0;
}
/*
Sample run
Name lowest highest total average grade
Kate 80 90 506 84 B
Tom 66 79 427 71 C
Andrew 87 94 545 90 A
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.