Question 01 Let\'s consider an input file that contains student id (int), first
ID: 3576334 • Letter: Q
Question
Question 01 Let's consider an input file that contains student id (int), 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 (int), 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. This file should be out of order on purpose. Calculates and displays the maximum grade in the array with name. Calculates and displays the minimum grade in the array with name. Calculates and displays the average grade Calculates and displays how many students were processed. Produce a file with the information above based on a file name provided by the user. The main idea is to explore the benefits of using structs. So every task should be done after the whole array is filled up The files must contain at least 5 records All items must be done with functions w/ parameters. MUST BE IN C. THANKSExplanation / Answer
// C++ code
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string> // std::string, std::to_string
#include <math.h>
#include <fstream>
using namespace std;
struct student
{
int id;
string firstName;
string lastName;
float grade;
};
string getFileName()
{
string filename;
cout << "Enter filename: ";
cin >> filename;
return filename;
}
void fillArrayFromFile (ifstream& ins, student studentArray[], int &numberOfStudents)
{
while ( true )
{
if(ins.eof())
break;
ins >> studentArray[numberOfStudents].id;
ins >> studentArray[numberOfStudents].firstName;
ins >> studentArray[numberOfStudents].lastName;
ins >> studentArray[numberOfStudents].grade;
numberOfStudents++;
}
}
float calculateMaximumGrade( student studentArray[], int numberOfStudents)
{
float max;
for (int i = 0; i < numberOfStudents; ++i)
{
if(i == 0)
{
max = studentArray[i].grade;
}
else
{
if(studentArray[i].grade > max)
{
max = studentArray[i].grade;
}
}
}
return max;
}
float calculateMinimumGrade( student studentArray[], int numberOfStudents)
{
float min;
for (int i = 0; i < numberOfStudents; ++i)
{
if(i == 0)
{
min = studentArray[i].grade;
}
else
{
if(studentArray[i].grade < min)
{
min = studentArray[i].grade;
}
}
}
return min;
}
float calculateAverageGrade (student studentArray[], int numberOfStudents)
{
float total = 0;
for (int i = 0; i < numberOfStudents; ++i)
{
total = total + studentArray[i].grade;
}
return total/numberOfStudents;
}
int main()
{
int numberOfStudents = 0;
string filename = getFileName();
student studentArray[100];
ifstream inFile (filename.c_str());
if (inFile.is_open())
{
fillArrayFromFile(inFile,studentArray,numberOfStudents);
inFile.close();
}
else cout << "Unable to open file";
// ofstream outFile ("paid.txt");
// if (outFile.is_open())
// {
// outFile << "Total: $" << total << endl;
// outFile << "Average: $" << average << endl;
// outFile << "Max: $" << max << endl;
// outFile << "Min: $" << min << endl;
// outFile.close();
// }
// else cout << "Unable to open file";
cout << "Total students processed: " << numberOfStudents << endl;
cout << "Average: "<< calculateAverageGrade(studentArray, numberOfStudents) << endl;
cout << "Maximum: "<< calculateMaximumGrade(studentArray, numberOfStudents) << endl;
cout << "Minimum: "<< calculateMinimumGrade(studentArray, numberOfStudents) << endl;
return 0;
}
/*
student.txt1 ayush verma 44.5
2 akash jain 92.4
3 alex hales 65.2
4 jason roy 78.5
5 nasir hameed 48.4
6 mike cook 68.3
7 davin benaut 98.1
8 eoin morgan 78.4
9 ricky gille 88.5
output:
Enter filename: student.txt
Total students processed: 9
Average: 73.5889
Maximum: 98.1
Minimum: 44.5
*/