Your assignment is to convert student marks to grades. An experienced C++ progra
ID: 3721434 • Letter: Y
Question
Your assignment is to convert student marks to grades. An experienced C++ programmer will achieve this task in about 40 minutes and take about 100 lines of code. As inexperienced programmers, the project is expected to require 4 hours work per week over 5 weeks.
Your exchange must:
Requirement 1 The program MUST read in data from a file.
Requirement 2 The program MUST provide a user interface option and functionality to order the grades by Student Name, by Student Number or by Grade. • Hint: this is most easily implemented as a list of structures.
Requirement 3 The program SHOULD provide a user interface to output the reordered data in a new file.
Explanation / Answer
Solution:
Note: Please save all the .txt file in the same package/folder before executing the code.
code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *inputFile,*fileOutput;
int i,j = 0;
char studentNames [20][40],ch;
int studentGrades [20][10];
inputFile = fopen("grades.txt", "r");
if (inputFile == NULL)
printf("File does not exist");
else{
while (fscanf(inputFile,"%[^ ]s",studentNames[i])!=EOF) //while there are student studentNames in the file
{
for(j=0;j<10;j++) //reading 10 studentNames, corresponding to the student
fscanf(inputFile, "%d", &studentGrades[i][j]);
fscanf(inputFile,"%c",&ch); //reading the extra newline character after the 10 th grade
i++; //incrementing i so it points for next student
}
fileOutput = fopen("averages.txt", "w"); //opening output file
int size = i; //no.of students == i;
for(i=0;i<size;i++) //For each student
{
double total=0,average;
for(j=0;j<10;j++) //calculating the total
total=total+studentGrades[i][j];
average = total/10; //calculating the average
fprintf(fileOutput,"%s %.1lf ",studentNames[i],average); //Printing to the output file
}
fclose(inputFile); //Closing the files
fclose(fileOutput);
}
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.