In: Computer Science
Write a C++ program that outputs to a file, final marks and average mark for a primary school class. Your program should also output to a second a file, the student names and their average mark in ascending order. This is an example of how your program in action could look like: Sample Input Please enter student name and student number Kgosi Kgosi 20150986 Please student marks for Setswana, Maths, Science, English, and Social Studies 80 70 80 65 100 Please enter any comments about the Student Sample Output First File *************************************Report************************************************* Student Name: Kgosi Kgosi Student Number: 20150986 Course Name Marks % Letter Grade Setswana 80 A - Maths 70 B Science 80 AEnglish 65 B - Social Studies 100 A+ Average: 79 Excellent Comments: **************************************************************************************************** Page 3 of 4 The program should also generate a second file listing all students and their average mark. The list of students should be arranged according to their marks in descending order. Sample Output Second File Kgosi Kgosi 79 Masego Ndi 59 Des Rinn 23
Please let me know if anything is required. I am happy to help you.
Please share me the grading ranges to give the grades for the each subject.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile;
outfile.open("file1.txt"); //to store the contents of student
details in file1
ofstream outfile1;
outfile1.open("file2.txt"); //file listing all students and their
average marks arranged in descending order
int number;
cout<<"Please enter how many students data you want to enter
: "; //taking total number of students
cin>>number;
int grades[number]; //array to store the average marks
string names[number]; //array to store the names
for(int i=0;i<number;i++)
{
string student_name;
int student_number;
cout<<"Please enter student name and student number : ";
//taking student name and student number from the user
cin>>student_name>>student_number;
names[i] = student_name;
//taking the marks for the each subject
int Set,Ma,Sci,Eng,Soc;
cout<<"Please Enter student marks for Setswana, Maths,
Science, English, and Social Studies : ";
cin>>Set>>Ma>>Sci>>Eng>>Soc;
string Comments;
cout<<"Please enter any comments: ";
cin>>Comments; //taking Comments from the user
//writing the student and their marks details in file1
outfile<<"Student Name: "<<student_name<<"
Student Number: "<<student_number<<" Course Name Marks
% Letter Grade Setswana "<<Set<<" - Maths
"<<Ma<< "B Science "<<Sci<<" English
"<<Eng<<" - Social Studies "<<Soc<<"
Average: "<<(Set+Ma+Sci+Eng+Soc)/5<<" Comments:
"<<Comments<<"\n";
grades[i]=(Set+Ma+Sci+Eng+Soc)/5; //calculating the average
marks
}
//sorting the names and the average marks according to the average
marks
for (int i = 0; i < number-1; i++)
{
for (int j = 0; j < number-i-1; j++)
{
if (grades[j] > grades[j+1])
{
int temp = grades[j];
grades[j] = grades[j+1];
grades[j+1]= temp;
string temp1 = names[j];
names[j] = names[j+1];
names[j+1] = temp1;
}
}
}
for (int j = number-1; j >= 0; j--)
{
outfile1<<names[j]<<" "<<grades[j]<<" ";
//in descending order storing the names and average marks in second
file
}
cout<<"\n";
return 0;
}
Test Case:
output files:
File 1:
File2: