Need help with arrays
Im not sure if my program is correct & need help checking it.
Last edited on
What's wrong with the program? It compiles and run fine at my system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
const int num_students = 5;
const int num_tests = 3;
double testScores[num_students][num_tests];
string filename;
ifstream inputFile;
cout << "Enter name of file to read: ";
cin >> filename;
inputFile.open(filename.c_str());
if ( !inputFile )
{
cout << "Can't open file " + filename +"\n";
//system("pause");
return 1;
}
for(int i = 0; i < num_students; i++)
{
for(int j = 0; j < num_tests; j++)
{
inputFile >> testScores[i][j];
}
}
inputFile.close();
cout << "\nAverage per student: ";
for(int i = 0; i < num_students; i++)
{
double sumForStudent = 0;
for (int j = 0; j < num_tests; j++)
{
sumForStudent = sumForStudent + testScores[i][j];
}
double avgOfStudent = sumForStudent / num_tests;
cout << "\t" << setiosflags(ios::fixed) << setprecision(2) << avgOfStudent;
}
cout << "\n\nAverage per test: ";
for(int i = 0; i < num_tests; i++)
{
double sumForTest = 0;
for (int j = 0; j < num_students; j++)
{
sumForTest = sumForTest + testScores[j][i];
}
double avgOfTest = sumForTest / num_students;
cout << "\t" << setiosflags(ios::fixed) << setprecision(2) << avgOfTest;
}
double max = 0, countofF = 0;
for(int i = 0; i < num_students; i++)
{
for (int j = 0; j < num_tests; j++)
{
if(testScores[i][j] > max)
max = testScores[i][j];
if(testScores[i][j] <= 59)
++countofF;
}
}
cout << "\n\nOverall best score: " << max;
cout << "\n\nNumber of F's: " << countofF << "\n\n";
//system("pause");
return 0;
}
|
Topic archived. No new replies allowed.