Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question 1. Read student information from a file, named “Test.txt”, to an ar

ID: 3589679 • Letter: C

Question

C++ Question

1. Read student information from a file, named “Test.txt”, to an array of type Student. The file should contain the details of 20 students. The information of each student should be formatted as follows. FirstName LastName Age Gpa EnglishScore MathScore BiologyScore (1). Write a function to calculate the average English score of all students you get from the above file, and call the function from the main function. Then print the value in the main function. The prototype of the function is given as follows. float AvgEnglish(Student s[], int numberOfStudents); (2). Write a function to return the full name of the student who got the highest score in the Biology class. The format of the full name should be the first name followed by the last name, separated by one space. Call this function from the main function and print the name of the student. string BestStudentBiology(Student s[], int numberOfStudents);

Explanation / Answer

// C++ code

#include <iomanip>

#include <iostream>

#include <stdio.h>

#include <string>

#include <sstream>

#include <fstream>

#include <stdlib.h>

#include <iomanip>

#include <vector>

using namespace std;

struct Student {

string firstName;

string lastName;

int age;

float gpa;

float scores[3];

};

float AvgEnglish(struct Student s[], int numberOfStudents) {

int i = 0;

float sum = 0, avg = 0;

for (i = 0; i < numberOfStudents; i++) {

sum = sum + s[i].scores[0]; //scores[0] is the English Score

}

avg = sum/numberOfStudents;

return avg;

}

string BestStudentBiology(Student s[], int numberOfStudents)

{

string fullname = s[0].firstName + " " + s[0].lastName;

float maxBiologyScore = s[0].scores[2];

for (int i = 0; i < numberOfStudents; ++i)

{

if(s[i].scores[2] > maxBiologyScore)

{

fullname = s[i].firstName + " " + s[i].lastName;

}

}

return fullname;

}

int main()

{

string fname, lname;

int age;

float gpa, english, math, biology;

struct Student st[20];

int i = 0;

float average = 0;

int totalRecords = 0;

ifstream inFile ("Test.txt");

if (inFile.is_open())

{

while ( true )

{

inFile << fname << lname << age << gpa << english << math << biology ;

st[i].firstName = fname;

st[i].lastName = lname;

st[i].age = age;

st[i].gpa = gpa;

st[i].scores[0] = english;

st[i].scores[1] = math;

st[i].scores[2] = biology;

i++;

totalRecords++;

if(inFile.eof())

break;

}

inFile.close();

}

else

{

cout << "Unable to open file";

return 0;

}

inFile.close();

average = AvgEnglish(st, totalRecords);

cout << "Average English score of all the students is: " << average << endl;

cout << "Student with highest biology score is: " << BestStudentBiology(st,totalRecords) << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote