C++ ONLY********** Write a program that declares a struct to store the data of a
ID: 3768122 • Letter: C
Question
C++ ONLY**********
Write a program that declares a struct to store the data of a student’s last name, his or her class standing (freshman, sophomore, junior, and senior), number of credits completed and the grade point average (GPA). A typical record will look like: "Jones junior 72 3.8 "
Create your own data file and make sure to include it in your submission. Declare an array of 10 components to store the data for all the students.
Read the data and output the data.
Calculate and display the average GPA for all students.
Display the data by class standing using a function that accepts the class standing as a parameter along with other needed parameters.
So, if the function is called with “sophomore”, data for all sophomores is displayed.
Note that the search will be case insensitive. So, entering JUNioR should still be treated as Junior, etc.. USE functions.
C++ ONLY *****
Explanation / Answer
PFB the code & input file explained with comments
/* Header files */
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
/* Structure definition */
struct Student
{
string lname;
string classStand;
int credits;
float gpa;
};
/* function to display structure data based on classStand
The input parameters are :
classStand, array of structure Student, size of array
*/
void displayRecords(string clsStand, Struct Student *s1, int n)
{
int i=0;
clsStnd = tolower(clsStand);
for(i=0; i<n; i++)
{
if(tolower(s1[i].classStand) == clsStand)
{
printf("%s %s %d %f ", s1[i].lname, s1[i].classStand, s1[i].credits, s1[i].gpa);
}
}
}
/* Driver function - Main */
int main()
{
int i=0;
struct Student s1[10];
FILE *fin = freopen("input.txt", "r", STDIN);
for(i=0; i<10; i++)
{
scanf("%s %s %d %f", &s1[i].lname, &s1[i].classStand, &s1[i].credits, &s1[i].gpa );
}
/* Display Records for classStand value of "junior" */
displayRecords("junior", s1, 10);
/* Display Records for classStand value of "senior" */
displayRecords("senior", s1, 10);
return 0;
}
------------
input.txt
------------
Jones junior 72 3.8
James senior 79 4.1
Bones junior 68 3.4
Adam sophomore 67 3.3
Alice freshman 77 4.0
Jones1 senior 88 4.4
James1 junior 82 4.3
Bones1 sophomre 71 3.6
Adam1 freshman 69 3.4
Alice1 junior 78 4.1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.