For this task you’ll be given a file, containing a list of students and their sc
ID: 3821496 • Letter: F
Question
For this task you’ll be given a file, containing a list of students and their scores in K subjects, where K is an arbitrary number. All of these values will be separated by commas. There will be one student record (Name + K scores) per line. The entire file will therefore be of this format
Name1, Score1, Score2, Score3 … ScoreK Name2, Score1, Score2, Score3 … ScoreK . . . NameN, Score1, Score2, Score3 … ScoreK
Your task will be to read the file, parse it into its individual components and populate two arrays with the names of each student and their average score respectively.
You will need to write the function-
int ReadScores(string filename, string names[], float avg_scores[], int array_size)
Where
filename is the name of the input file as specified above
names[] is an initially empty array of student names
avg_scores[] is an initially empty array of student’s average scores.
array_size is the size of the arrays avg_score and names
Your function will return the number of lines read.
Task Details You’ll first need to initialize every element in the name array with the empty string “”. Similarly every element in the avg_score array will be initialized with -1.
You will need to read each line in the file and parse the line of data to obtain all the values. Place the name into the names array and process all the scores to calculate an average to be placed into the corresponding position of the avg_scores array
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int array_size=8;
string names[array_size];
float avg_scores[array_size];
int ReadScores(char filename[], string names[], float avg_scores[],int array_size)
{
ifstream infile;
char line[100];
int total=0,score1,score2,score3,score4,score5;
int i=0,j=0;
infile.open(filename);
if(!infile)
{
cout<<" File does not exist: ";
}
else
{
cout<<" -----------Reading Data from File---------- ";
while (!infile.eof())
{
while(infile>>line)
{
char *pch, *pEnd;
pch = strtok (line," , ");
while (pch != NULL)
{
if(j==0)
names[i]=pch;
else if(j==1)
score1 = strtol (pch,&pEnd,10);//score1=pch;
else if(j==2)
score2 = strtol (pch,&pEnd,10);//score2=pch;
else if(j==3)
score3 = strtol (pch,&pEnd,10);//score3=pch;
else if(j==4)
score4 = strtol (pch,&pEnd,10);//score4=pch;
else if(j==5)
score5 = strtol (pch,&pEnd,10);//score5=pch;
pch = strtok (NULL, ",");
j++;
}
total=score1+score2+score3+score4+score5;
avg_scores[i]=total/5;
cout<<endl<<names[i]<<" "<<score1<<" "<<score2<<" "<<score3<<" "<<score4<<" "<<score5<<" "<<avg_scores[i];
i++;
}
}
}
infile.close();
return i;
}
void WriteGrades(char filename[], string names[], float avg_scores[],int n_students)
{
ofstream outfile;
int i=0;
outfile.open(filename);
if(!outfile)
{
cout<<" File does not exist: ";
}
else
{
cout<<" ---------Writing Data to File---------- ";
while(i<n_students)
{
outfile<<names[i]<<" "<<avg_scores[i]<<" ";
if(avg_scores[i]>=90)
outfile<<"A ";
else if(avg_scores[i]<90&&avg_scores[i]>=80)
outfile<<"B ";
else if(avg_scores[i]<80&&avg_scores[i]>=70)
outfile<<"C ";
else if(avg_scores[i]<70&&avg_scores[i]>=60)
outfile<<"D ";
else if(avg_scores[i]<60)
outfile<<"F ";
i++;
}
}
outfile.close();
}
int main()
{
char infile[20],outfile[20];
cout<<" Enter Input FileName : ";
cin>>infile;
int x=ReadScores(infile,names,avg_scores,array_size);
cout<<" Enter Output FileName : ";
cin>>outfile;
WriteGrades(outfile,names,avg_scores,x);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.