c++ Question2 Consider a file with the following student information Joh Smith 9
ID: 3732467 • Letter: C
Question
c++
Question2 Consider a file with the following student information Joh 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 of a student. Write a program that prompts the user for the file name, then calculates and displays the average score of all students by reading the data from the named file. You should use a function to read/process the data from the file and this function must have parameters. Your program should also present the highest score in class.Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int getData(int *arr) {
cout << "Enter file name: ";
string filename;
cin >> filename;
ifstream in(filename.c_str());
int size = 0;
if(in.is_open()) {
int score;
string temp;
while(in >> temp >> temp >> score) {
arr[size++] = score;
}
} else {
cout << "can not open " << filename << " to read" << endl;
}
return size;
}
double avg(int *arr, int size) {
double total = 0;
for(int i = 0; i < size; ++i) {
total += arr[i];
}
return total/size;
}
int max(int *arr, int size) {
int temp = arr[0];
for(int i = 0; i < size; ++i) {
if(arr[i] > temp) {
temp = arr[i];
}
}
return temp;
}
int main() {
int arr[1000];
int size = getData(arr);
cout << "Average score is " << avg(arr, size) << endl;
cout << "Maximum score is " << max(arr, size) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.