Question 01 Consider an input file that contains student id (int), first name (s
ID: 3698922 • Letter: Q
Question
Question 01 Consider an input file that contains student id (int), first name (string), last name (string), and grade (float). We presume that the file contains anywhere from 1 to 100 grades Write a program that: 'Declares 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. *Do each of these tasks as separate functions: Reads grades from file named by user to fill up an array of type student. This file should be out of order on purpose (not in order by ID). Calculate and display the maximum grade in the array, along with student name. Calculate and display the minimum grade in the array, along with student name. o o o o Calculate and display how many students were processed. o Calculate and display the average grade of all students o Produce an output file with the information above, into a separate file provided by the user 'The main task is to explore the benefits of using structures so every task should be done after the array is filled up and not while it is being loaded. *Your test file must contain a minimum of 7 records EXTRA (+2.00): *Create an ordered array of students based on the initial array. This order can be by: id or grade - you choose 'You are required to print out the old and the new order to show your work. Make sure to comment your code for the sort algorithm selected (e.g. selection sort) You are not allowed to use data structures to automatically sort your array. This must be done by implementing an algorithm that you code as its own functionExplanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements.
//code.cpp
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//definining Student structure
struct Student{
int id;
string first;
string last;
float grade;
};
//defining an array of 100 students
Student students[100];
//keep the track of students processed
int numStudents;
//method to read students data from input file and fill the array,
//returns true if the file has been read successfully, else false
bool readFromFile(char filename[]){
numStudents=0;
ifstream inFile(filename);
if(!inFile){
cout<<"Can't find input file"<<endl;
return false;
}else{
int id;
string first,last;
float grade;
//assuming input file is in valid format
while(inFile>>id>>first>>last>>grade){
Student s;
s.id=id;
s.first=first;
s.last=last;
s.grade=grade;
students[numStudents]=s;
numStudents++;//incrementing students count
}
inFile.close();
return true;
}
}
//method to display all students in the array
void displayAll(){
for(int i=0;i<numStudents;i++){
cout<<"ID: "<<students[i].id<<", ";
cout<<"First name: "<<students[i].first<<", ";
cout<<"Last name: "<<students[i].last<<", ";
cout<<"Grade: "<<students[i].grade;
cout<<endl;
}
}
//method to calculate and return the student with maximum grade
Student maximumGrade(){
Student studWithMaxGrade;
if(numStudents>0){
//assuming first student has the maximum grade
studWithMaxGrade=students[0];
for(int i=0;i<numStudents;i++){
if(students[i].grade>studWithMaxGrade.grade){
studWithMaxGrade=students[i];
}
}
}
return studWithMaxGrade;
}
//method to calculate and return the student with minimum grade
Student minimumGrade(){
Student studWithMinGrade;
if(numStudents>0){
//assuming first student has the minimum grade
studWithMinGrade=students[0];
for(int i=0;i<numStudents;i++){
if(students[i].grade<studWithMinGrade.grade){
studWithMinGrade=students[i];
}
}
}
return studWithMinGrade;
}
//method to return the number of students processed
int studentsProcessed(){
return numStudents;
}
//method to calculate and return the average grade of all students combined
float averageGrade(){
float avg=0;
float total=0;
for(int i=0;i<numStudents;i++){
total+=students[i].grade;
}
if(numStudents!=0){
avg=(float)total/numStudents;
}
return avg;
}
//method to save data to a file
void saveToFile(char filename[]){
ofstream outFile(filename);
Student max=maximumGrade();
outFile<<"Maximum grade: "<<max.grade<<", student: "<<max.first<<" "<<max.last<<endl;
Student min=minimumGrade();
outFile<<"Minimum grade: "<<min.grade<<", student: "<<min.first<<" "<<min.last<<endl;
outFile<<"Number of students processed: "<<numStudents<<endl;
outFile<<"Average grade: "<<averageGrade()<<endl;
outFile.close();
cout<<"Stats have been saved to "<<filename<<endl;
}
//method to sort and display the students array (extra credit part)
void sortAndDisplay(){
cout<<" Unsorted list "<<endl;
displayAll();
/*performing selection sort*/
int i, j, min;
for (i = 0; i < numStudents-1; i++)
{
// Finding the smallest element (by id) in the unsorted array
min = i;
for (j = i+1; j < numStudents; j++)
if (students[j].id < students[min].id)
min = j;
// Swaping the elements
Student temp = students[min];
students[min]=students[i];
students[i]=temp;
}
cout<<" Sorted list "<<endl;
displayAll();
}
int main(){
char filename[25];
cout<<"Enter input file name: ";
cin>>filename;
//reading file
if(readFromFile(filename)){
sortAndDisplay();//displaying the initial list, sorting it, and displaying the final list
//finding and displaying other stats
Student max=maximumGrade();
cout<<"Maximum grade: "<<max.grade<<", student: "<<max.first<<" "<<max.last<<endl;
Student min=minimumGrade();
cout<<"Minimum grade: "<<min.grade<<", student: "<<min.first<<" "<<min.last<<endl;
cout<<"Number of students processed: "<<numStudents<<endl;
cout<<"Average grade: "<<averageGrade()<<endl;
cout<<"Enter output file name: ";
cin>>filename;
saveToFile(filename);
}
return 0;
}
//students.txt
123 Alice Al 3.4
587 Bob bobby 3.8
228 Dave D 3.1
321 Eric Erix 2.5
555 Chris christy 3.9
710 Zaid Z 3.2
124 Emily Clay 3.7
/*OUTPUT*/
Enter input file name: students.txt
Unsorted list
ID: 123, First name: Alice, Last name: Al, Grade: 3.4
ID: 587, First name: Bob, Last name: bobby, Grade: 3.8
ID: 228, First name: Dave, Last name: D, Grade: 3.1
ID: 321, First name: Eric, Last name: Erix, Grade: 2.5
ID: 555, First name: Chris, Last name: christy, Grade: 3.9
ID: 710, First name: Zaid, Last name: Z, Grade: 3.2
ID: 124, First name: Emily, Last name: Clay, Grade: 3.7
Sorted list
ID: 123, First name: Alice, Last name: Al, Grade: 3.4
ID: 124, First name: Emily, Last name: Clay, Grade: 3.7
ID: 228, First name: Dave, Last name: D, Grade: 3.1
ID: 321, First name: Eric, Last name: Erix, Grade: 2.5
ID: 555, First name: Chris, Last name: christy, Grade: 3.9
ID: 587, First name: Bob, Last name: bobby, Grade: 3.8
ID: 710, First name: Zaid, Last name: Z, Grade: 3.2
Maximum grade: 3.9, student: Chris christy
Minimum grade: 2.5, student: Eric Erix
Number of students processed: 7
Average grade: 3.37143
Enter output file name: out.txt
Stats have been saved to out.txt