In this lab i read and open a text file that contains names of students and thei
ID: 3768315 • Letter: I
Question
In this lab i read and open a text file that contains names of students and their test scores. I need to print their names and each students' average test score in a table. Why am I getting grabage values for my student averages? Here is my c++ code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
double calcAverage (double arr[], int n);
double calcMedian (double [], int);
int main()
{
ifstream inputFile;
string nameOfFile, studentName;
//Asking the user for the file name
cout << "Please enter the file name: " << endl;
cin >> nameOfFile;
//Open the file
inputFile.open(nameOfFile.c_str());
//If file cannot be opened, an error message is printed
if(!inputFile)
{
cout << "Error opening file" << endl;
}
else
{
const int MAX_STUDENTS = 12;
double finalScores[MAX_STUDENTS];
const int MAX_SCORES = 10;
double scores[MAX_SCORES];
int k = 0;
cout << "------------------------------" << endl;
cout << setw(8) << "Name " << setw(5) << "|" << " Average" << endl;
cout << "------------------------------" << endl;
while (inputFile >> studentName)
{
for (int i = 0; i < MAX_SCORES; i++)
{
inputFile >> scores[i];
}
finalScores[k] = calcAverage(scores, MAX_SCORES);
k++;
cout << setw(8) << studentName << setw(5) << "|" << setw(5) << finalScores[k] << endl;
}
cout << " " << endl;
cout << "Final Scores From Least to Greatest: " << endl;
cout << "Class Median: " << endl;
cout << "Class Average: " << endl;
cout << "Closing file" << endl;
inputFile.close();
}
return 0;
}
//This function calculates the average score of each student and the average class score
double calcAverage (double arr[], int n)
{
double average;
int totalScores = 0;
for(int k = 0; k < n; k++)
{
totalScores += arr[k];
}
average = totalScores / n;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
double calcAverage (double arr[], int n);
double calcMedian (double [], int);
int main()
{
ifstream inputFile;
string nameOfFile, studentName;
//Asking the user for the file name
cout << "Please enter the file name: " << endl;
cin >> nameOfFile;
//Open the file
inputFile.open(nameOfFile.c_str());
//If file cannot be opened, an error message is printed
if(!inputFile)
{
cout << "Error opening file" << endl;
}
else
{
const int MAX_STUDENTS = 12;
double finalScores[MAX_STUDENTS];
const int MAX_SCORES = 10;
double scores[MAX_SCORES];
int k = 0;
cout << "------------------------------" << endl;
cout << setw(8) << "Name " << setw(5) << "|" << " Average" << endl;
cout << "------------------------------" << endl;
while (inputFile >> studentName)
{
for (int i = 0; i < MAX_SCORES; i++)
{
inputFile >> scores[i];
}
finalScores[k] = calcAverage(scores, MAX_SCORES);
k++;
cout << setw(8) << studentName << setw(5) << "|" << setw(5) << finalScores[k] << endl;
}
cout << " " << endl;
cout << "Final Scores From Least to Greatest: " << endl;
cout << "Class Median: " << endl;
cout << "Class Average: " << endl;
cout << "Closing file" << endl;
inputFile.close();
}
return 0;
}
//This function calculates the average score of each student and the average class score
double calcAverage (double arr[], int n)
{
double average;
double totalScores = 0; //totalScores should be of double datatype
for(int k = 0; k < n; k++)
{
totalScores += arr[k];
}
average = totalScores / n;
return average;//return the calculated average
}
find extra comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.