pleas use C++ to answer the question Let\'s consider an input file that contains
ID: 3683366 • Letter: P
Question
pleas use C++ to answer the question
Let's consider an input file that contains student id (mt), first name (string), last name (string), and grade (float). We assume that the file contains between 1 and 100 grades. Write a program that: Declare the structure named student composed of id (mt), first (string), last (string), and grade (float). Asks the user for the name of the input file. Reads the grades from the file to fill up an array of type student Calculates and displays the maximum grade in the array. Calculates and displays the minimum grade in the array Calculates and displays the average grade. Your program should define and use the following functions:Explanation / Answer
#include <iostream> //for cout, cin
#include <fstream> //for file input/output
#include <cstdlib>
using namespace std;
/*
Structure to store students data
*/
struct Student
{
string first;
string last;
int id;
float grade;
};
Student* readStudentsFromFile(int &totalStudents);
float maxGrade(Student *students, int n);
float minGrade(Student *students, int n);
float average(Student *students, int n);
/*
* Entry point
*/
int main()
{
Student *students;
int totalStudents = 0;
students = readStudentsFromFile(totalStudents);
cout<<"Maximum grade: "<<maxGrade(students, totalStudents)<<endl;
cout<<"Minimum grade: "<<minGrade(students, totalStudents)<<endl;
cout<<"Average grade: "<<average(students, totalStudents)<<endl;
return 0;
}
Student* readStudentsFromFile(int &totalStudents)
{
Student *allStudentsPointer;
//input stream for students data
ifstream allStudentsInFile;
//open students file
char fileName[20];
cout<<"Enter fileName: ";
cin>>fileName;
allStudentsInFile.open(fileName);
//error handling in case file does not exist - start
if( !allStudentsInFile )
{
cout << "Error opening students.txt" << endl;
return NULL;
}
//error handling in case file does not exist - end
cout << "Success opening students.txt" << endl;
int i=0;
allStudentsPointer = new Student[100];
while(allStudentsInFile >> allStudentsPointer[i].first)
{
allStudentsInFile >> allStudentsPointer[i].last;
allStudentsInFile >> allStudentsPointer[i].id;
allStudentsInFile >> allStudentsPointer[i].grade;
totalStudents++;
i++;
}
allStudentsInFile.close();
return allStudentsPointer;
}
float maxGrade(Student *students, int n){
float max = students[0].grade;
for(int i=1; i<n; i++){
if(max < students[i].grade)
max = students[i].grade;
}
return max;
}
float minGrade(Student *students, int n){
float min = students[0].grade;
for(int i=1; i<n; i++){
if(min > students[i].grade)
min = students[i].grade;
}
return min;
}
float average(Student *students, int n){
float total = 0;
for(int i=0; i<n; i++){
total+=students[i].grade;
}
return total/n;
}
/*
Content of file should be in given below format:
abc.txt:
pravesh Kumar 1 89
mukesh mehta 2 78
sample output:
Enter fileName: students.txt
Success opening students.txt
Maximum grade: 89
Minimum grade: 78
Average grade: 83.5
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.