For this programming assignment you will read a list of names and grades from a
ID: 3557855 • Letter: F
Question
For this programming assignment you will read a list of names and grades from a text file. The file will be user-specified. You will then print out the original list and them the list of names and grades in order by grade. Also print the number of and the average, highest, and lowest grade. So if the data is as follows: You must use functions and comments must be included. Some of you have not been including comments in your program. No program will be accepted that does not satisfy the requirements. No late programs will be accepted. Variables used within functions must be local. Good program structure and design are expected and required. There should not be a major amount of processing in the main program. Streams should be used for i/o.Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
typedef char Str[10];
void Sort(int grades[], Str names[],int i)
{
// SORT THEM USING GRADE.
for(int k=i-1; k>=0; k--)
{
int max = k;
for(int p=0; p<k; p++)
{
if(grades[p] > grades[max])
max = p;
}
if(max!=k)
{
int temp = grades[k];
grades[k] = grades[max];
grades[max] = temp;
Str local;
strcpy(local,names[k]);
strcpy(names[k],names[max]);
strcpy(names[max],local);
}
}
}
void count_grades(int grades[],int grage_count[],int i)
{
for(int k=0; k<i; k++)
{
if(grades[k]>=90 && grades[k]<=100)
grage_count[0]++;
else if(grades[k]>=80 && grades[k]<=89)
grage_count[1]++;
else if(grades[k]>=70 && grades[k]<=79)
grage_count[2]++;
else if(grades[k]>=50 && grades[k]<=69)
grage_count[3]++;
else grage_count[4]++;
}
}
double average(int grades[],int i)
{
double sum = grades[0];
for(int k=1; k<i; k++)
{
sum+= grades[k];
}
return sum/i;
}
int main()
{
Str names[20];
int grades[20],i=0;
int grage_count[5] = {0};
char fn[30];
cout <<"Enter a file name :";
cin >> fn;
ifstream infile(fn);
if(infile)
{
for(; infile >> names[i] >> grades[i]; i++);
infile.close();
}
Sort(grades, names, i);
cout << endl;
for(int k=i-1; k>=0; k--)
{
cout << names[k] << " " << grades[k] << endl;
}
count_grades(grades, grage_count, i);
for(int p=0; p<5; p++)
{
cout <<"There were "<< grage_count[p] << " " << static_cast<char> (p+65) << "'s"<< endl;
}
cout <<"Highest :" <<grades[i-1] << endl;
cout <<"Lowest :" <<grades[0] << endl;
cout <<"Average :" <<average(grades,i) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.