In: Computer Science
Here we will be taking data in as a file. Note that the sample file provided here is not the file that I will use to grade your assignment but the formatting will be the same.
The file you will be imputing to your program is a grade sheet. You will be taking in the grade sheet of 5 students with an unknown amount of grades (the number of possible grades is from 2 to 8). The number of grades will be provided at the top of the file. Your job is to take in the grades average them for each of the 5 students and output the grades in order from greatest to least.
Example input:
Grades 4
Bob 90 89 72 89
Tim 65 60 78 88
Kelly 99 67 75 81
Wesley 44 76 58 67
Anel 100 91 90 96
Example output:
Anel 94.25
Bob 85
Kelly 80.5
Tim 72.75
Wesley 61.25
Final submission should be your .cpp file only
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// gradesData.txt
Grades 4
Bob 90 89 72 89
Tim 65 60 78 88
Kelly 99 67 75 81
Wesley 44 76 58 67
Anel 100 91 90 96
____________________
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//Function declarations.
void sort(string names[],double avgs[],int size);
void display(string names[],double avgs[],int size);
int main()
{
//Declaring constant
const int SIZE=5;
//Declaring variables
string str;
int grades;
ifstream dataIn;
double avg=0,grade,sum=0;
//Declaring arrays
string names[SIZE];
double avgs[SIZE];
//Opening the input file
dataIn.open("gradesData.txt");
if(dataIn.fail())
{
cout<<"** File Not Found **"<<endl;
return 1;
}
else
{
//Read the data
from the file(i.e no of grades of each student)
dataIn>>str>>grades;
/* Reading the
data from the fiel and
* populate the
values in the array
*/
for(int i=0;i<SIZE;i++)
{
dataIn>>str;
names[i]=str;
for(int
j=0;j<grades;j++)
{
dataIn>>grade;
sum+=grade;
}
avg=sum/grades;
avgs[i]=avg;
sum=0;
}
//calling the
functions
sort(names,avgs,SIZE);
display(names,avgs,SIZE);
}
return 0;
}
//This function will sort the averages in decending order
void sort(string names[],double avgs[],int size)
{
//This Logic will Sort the Array of
elements in Decending order
double temp;
string tempName;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (avgs[i] < avgs[j])
{
temp = avgs[i];
avgs[i] = avgs[j];
avgs[j] = temp;
tempName = names[i];
names[i] = names[j];
names[j] = tempName;
}
}
}
}
//This function will diasplay the names and averages after
sorting
void display(string names[],double avgs[],int size)
{
for(int i=0;i<size;i++)
{
cout<<names[i]<<"\t"<<avgs[i]<<endl;
}
}
___________________________
// Output;
_______________Could you plz rate me well.Thank
You