Question

Given the Student struct: struct Student{ string fName; string lName; int age; float gpa; float scores[NUM];//English,...

Given the Student struct:

struct Student{

string fName;

string lName;

int age;

float gpa;

float scores[NUM];//English, Mathematics, Biology

};

  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);

  1. 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);

Homework Answers

Answer #1

C++ code for above problem

#include<iostream>
#include<fstream>
#define MAX 20
#define NUM 3
using namespace std;

struct Student
{
   string fName;
   string lName;
   int age;
   float gpa;
   float scores[NUM];
};

// function that reads data from file and returns number of lines read from file
int read_data(Student s[])
{
   int len=0;
   ifstream myfile("Test.txt");
   if(myfile.is_open())
    {
       string line;
       while(getline(myfile,line))
       {
           len++;
       }
       myfile.close();
      
       ifstream file("Test.txt");
      
       for(int i=0;i<len;i++)
       {
           file >> s[i].fName;
           file >> s[i].lName;
           file >> s[i].age;
           file >> s[i].gpa;
           for(int j=0;j<NUM;j++)
           {
               file >> s[i].scores[j];
           }
       }
      
       file.close();
    }
    return len;
}

// function that retuns the average marks in English subject
float AvgEnglish(Student s[], int numberOfStudents)
{
   if(numberOfStudents==0)
       return 0.0;
   float sum=0.0;
   for(int i=0;i<numberOfStudents;i++)
   {
       sum+=s[i].scores[0];
   }
   return sum/numberOfStudents;
}

// function that returns the Name of Best Student in Biology subject
string BestStudentBiology(Student s[], int numberOfStudents)
{
   if(numberOfStudents==0)
       return "";
   int max_index=0;
   for(int i=1;i<numberOfStudents;i++)
   {
       if(s[max_index].scores[2]<s[i].scores[2])
       {
           max_index=i;
       }
   }
   return s[max_index].fName+" "+s[max_index].lName;
}

// testing main function
int main()
{
   struct Student students[MAX];
   int len=read_data(students);
   cout << "Average marks in English: " << AvgEnglish(students,len) << endl;
   cout << "Best Student in Biology: " << BestStudentBiology(students,len) << endl;
   return 0;
}

Sample output

if "Test.txt" has following data

Captain America 25 8.5 67 78 8
Iron Man 24 9.3 45 63 56
Black Panther 23 7.7 65 72 88
Doctor Strange 27 6.9 45 47 65
Ant Man 25 6.6 77 54 58
Spider Man 22 7.3 44 76 85
Captain Marvel 21 8.8 65 66 67

then after running the above code, output looks as follows:

Mention in comments if any mistakes or errors are found. Thank you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. (Use C++ ) #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int...
14.14 Structure - read and print structure Assuming you have the following structure struct Student {...
14.14 Structure - read and print structure Assuming you have the following structure struct Student { int id; string name; float GPA; }; You are required to read students' information from an input file whose filename is provided by standard input; Please use std::cout to output students' name who have a GPA > 3.0 in the reversed order. input id=1234567 name=Peter GPA=3.6 id=1234568 name=Adina GPA=3.8 id=1234569 name=Mike GPA=3.0 id=1234569 name=Gina GPA=2.9 Note: input file may be empty. output Adina Peter...
What is the output from the following code? struct   StudentType{ long id; string name; int age;...
What is the output from the following code? struct   StudentType{ long id; string name; int age; } StudentType stud1; stud1.id = 12345; stud1.name = “Josh”; stud1.age =20; StudentType* ptr = & stud1; ptr->age++; ptr->name = “John”; cout<<stud1.name<<” “ <<stud1.age;
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It must inherit from Professor, but add a new String attribute named term. (The term must be a six digit string, with the year followed by two digits to represent the term: "01" for Winter, "05" for Spring, and "09" for Fall. Thus, 201705 represents the Spring 2017 term. Override print so that it prints CAS data like: McGarrity, Ivan Department: English Term: 201705 Write...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a string, get a string from the user, open file, close file, read file, and allocate heap memory. You can use more macros than these if you like. Main Program File: Allocate 1024 bytes of dynamic memory and save the pointer to the area. The main program is a loop in which you ask the user for a filename. If they enter nothing for the...
Having below tables: create table Student(sid char(10) primary key, sname varchar(20) not null, gpa float, major...
Having below tables: create table Student(sid char(10) primary key, sname varchar(20) not null, gpa float, major char(10), dob DATE); create table Course(cno char(10) primary key, cname varchar(20) not null, credits int, dept char(10)); create table Reg( sid references Student(sid) on delete cascade, cno references Course(cno) on delete cascade, grade char(2), primary key (sid, cno)); questions For each course, display the number of students who got each grade, ordered by the course and the letter grade. List the current age in...
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Having below tables: create table Student(sid char(10) primary key, sname varchar(20) not null, gpa float, major...
Having below tables: create table Student(sid char(10) primary key, sname varchar(20) not null, gpa float, major char(10), dob DATE); create table Course(cno char(10) primary key, cname varchar(20) not null, credits int, dept char(10)); create table Reg( sid references Student(sid) on delete cascade, cno references Course(cno) on delete cascade, grade char(2), primary key (sid, cno)); questions (Oracle live SQL) For each course, display the number of students who got each grade, ordered by the course and the letter grade. List the...