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

Question 2 Let\'s consider a file with the following student information: John S

ID: 3806060 • Letter: Q

Question

Question 2 Let's consider a file with the following student information: John Smith 99 Sarah Johnson 85 Jim Robinson 70 Mary Anderson 100 Michael Jackson 92 Each line in the file contains the first name, last name, and test score ofa student. Write a program that prompts the user for the file name, then calculates and displays the average score by reading the data from the file. You should use a function to read/process the data from the file, this function must have parameters, like in our example. Your program should also present the highest score in class 2.1)Copy and paste the source code. 2.2) Provide one screenshot.

Explanation / Answer

// C++ code
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

double readFile(string filename)
{
string fname, lname;
double score;
int count = 0;
double average = 0;
double highest = 0;

// open file
ifstream inFile (filename.c_str());
if (inFile.is_open())
{
   // read file till end of file
while ( true )
{
inFile >> fname >> lname >> score;;
count++;
average = average + score;
if(score > highest)
{
    highest = score;
}

if(inFile.eof())
    break;
}
// close file
inFile.close();
}
else cout << "Unable to open file";


cout << "Average score: " << average/count << endl;

return highest;

}

int main()
{

string filename;
cout << "Enter filename: ";
cin >> filename;

double highest = readFile(filename);

cout << "Highest score: " << highest << endl;
  
return 0;
}


/*
output:
Enter filename: input.txt
Average score: 89.2
Highest score: 100


input.txt
John Smith 99
Sarah Johnson 85
Jim Robison 70
Mary Anderson 100
Michael Jackson 92
*/

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