How do you write a program, in C++, that takes in an input file (the input file
ID: 3625914 • Letter: H
Question
How do you write a program, in C++, that takes in an input file (the input file contains on each line a student ID(5 digits), 4 test grades, and a Final exam grade. Also the input file can hold up to 50 students, but may not). An overal grade is calculated by 40% test grades average plus 60% Final exam grade. How do you make the program print to the screen the number of students, the maximum score for grades, the minimum score for grades, the average score for grades, and the ID and grade for each student and that grade compared to the average grade using (<, >, =, <=, >=) signs.Explanation / Answer
Since, there is no input data given, I will use my own (made up on):
ABCD 50 60 20 40 80
BCDE 40 45 67 43 75
XYZA 75 75 80 69 90
Now, here is the code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream inf;
float max,min;
int no=0;
string ID[50];
float marks[50][5];
float overall[50];
float total=0;
float avg=0;
inf.open("student.txt");
if(!inf)
{
cout<<"file not found!!"<<endl;
no=0;
}
else
{
while(!inf.eof())
{
inf>>ID[no]>>marks[no][0]>>marks[no][1]>>marks[no][2]>>marks[no][3]>>marks[no][4];
no++;
}
}
//after reading all the records close the file
inf.close();
//Now calculate overall score with 40% of Test Grades and 60% of Final Exam
for(int i=0;i<no;i++)
{
for(int j=0;j<4;j++)
{
overall[i] += marks[i][j];
}
overall[i] = (overall[i] / 4) * 0.40;
overall[i] = overall[i] + (marks[i][4] * 0.60);
}
//Initialize to first one for finding maximum and minimum of oerall
max = overall[0];
min= overall[0];
for(int i=1;i<no;i++)
{
if(overall[i] > max)
{
max = overall[i];
}
if(overall[i] < min)
{
min = overall[i];
}
}
//Calculation for average
for(int i=0;i<no;i++)
{
total += overall[i];
}
avg = total / no;
//Display all the results
cout<<"***************************************"<<endl;
cout<<"Total Number of Students : "<<no<<endl;
cout<<"***************************************"<<endl;
cout<<"Maximum Overall Grade : "<<max<<endl;
cout<<"Minimum Overall Grade : "<<min<<endl;
cout<<"***************************************"<<endl;
cout<<"Average of Overall Grades : "<<avg<<endl;
cout<<"***************************************"<<endl;
for(int i=0;i<no;i++)
{
cout<<"Student ID's : "<<ID[i]<<endl;
cout<<"Grade : "<<overall[i];
if(overall[i] < avg)
{
cout<<" is less than Average Grade."<<endl;
}
else if(overall[i] > avg)
{
cout<<" is greater than Average Grade."<<endl;
}
else
{
cout<<" is same as Average Grade."<<endl;
}
}
cout<<"***************************************"<<endl;
system("pause");
return 0;
}
Please rate:-LiFesAVER
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.