Let\'s consider an input file that contains student id (int), first name (string
ID: 3574836 • Letter: L
Question
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.Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
struct student
{
int id;
string fname;
string lname;
float grade;
};
void maxGrad(struct student* s,int size)
{
float maxgpa=0;
string fname,lname;
for (int i = 0; i < size; ++i)
{
if(s[i].grade>maxgpa)
{
maxgpa=s[i].grade;
fname=s[i].fname;
lname=s[i].lname;
}
}
cout<<"Maximum grade is "<<maxgpa<<endl;
cout<<"First name of student with maximum grade is "<<fname<<endl;
cout<<"Last name of student with maximum grade is "<<lname<<endl;
}
void minGrade(struct student* s,int size)
{
float mingpa=999;
string fname,lname;
for (int i = 0; i < size; ++i)
{
if(s[i].grade<mingpa)
{
mingpa=s[i].grade;
fname=s[i].fname;
lname=s[i].lname;
}
}
cout<<"Minimum grade is "<<mingpa<<endl;
cout<<"First name of student with Minimum grade is "<<fname<<endl;
cout<<"Last name of student with Minimum grade is "<<lname<<endl;
}
void avgGrade(struct student* s,int size)
{float sum=0;
for (int i = 0; i < size; ++i)
{
sum+=s[i].grade;
}
float avg=sum/size;
cout<<"Avearge grade is "<<avg<<endl;
}
int main(int argc, char const *argv[])
{
ifstream file("input.txt");
int id;
string fname;
string lname;
float grade;
struct student s[6];
int i=0;
while(file.good())
{
file>>id;
s[i].id=id;
file>>fname;
s[i].fname=fname;
file>>lname;
s[i].lname=lname;
file>>grade;
s[i].grade=grade;
i++;
}
maxGrad(s,5);
minGrade(s,5);
avgGrade(s,5);
return 0;
}
===========================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ fstudent.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Maximum grade is 100
First name of student with maximum grade is jay
Last name of student with maximum grade is shinde
Minimum grade is 1.2
First name of student with Minimum grade is raj
Last name of student with Minimum grade is shinde
Avearge grade is 31.952
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.