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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
#include <iomanip>
#include <fstream>
// global variables
const int ROW = 5, CLM = 3;
using namespace std;
// function prototypes
void testAverage(int table[][CLM], int); // average score for each test
void studentAverage(int table[][CLM], int); // average score for each student
void bestScore(int table[][CLM], int); // best score for each test
void numberOfA(int table[][CLM], int); // number of a scores for each student
int main()
{
int table[ROW][CLM];
int row;
ifstream inputFile;
inputFile.open("p6.dat"); // open file
for (int rowNum = 0; rowNum < ROW; rowNum++) // read info from file
{
for (int clmNum = 0; clmNum < CLM; clmNum++)
{
inputFile >> table[rowNum][clmNum];
}
}
inputFile.close(); // close file
for(int row = 0; row < ROW; row++)
{
for (int col = 0; col < CLM; col++)
cout << table[row][col] << endl;
}
studentAverage(table, row);
testAverage (table, row);
bestScore (table, row);
numberOfA (table, row);
return 0;
}
void studentAverage(const int scores[][CLM], int row)
{
double average, total = 0;
int col;
for(int row = 0; row < ROW; row++)
{
for (col = 0; col < CLM; col++)
total += scores[row][col];
average = total / CLM;
cout << showpoint << setprecision(2) << fixed;
cout << "The average score for student " << (row + 1) << " is " << average << endl;
}
}
void testAverage(const int scores[][CLM], int row)
{
double average, total = 0;
int col;
for (col = 0; col < CLM; col++)
{
for (int row = 0; row < ROW; row++)
total += scores[row][col];
average = total / ROW;
}
cout << showpoint << setprecision(2) << fixed;
cout << "The average score for test " << (col + 1) << " is " << average << endl;
}
void bestScoreStudent(const int scores[][CLM], int row)
{
int highest, col;
highest = scores[0][0];
for(int row = 0; row < ROW; row++)
{
for (col = 0; col < CLM; col++)
{
if (scores [row][col] > highest)
highest = scores[row][col];
}
}
cout << "The highest test score is " << highest << endl;
}
void numberOfA(const int scores[][CLM], int row)
{
const int ACE = 90;
int numOfA = 0, col;
for(int row = 0; row < ROW; row++)
{
for (col = 0; col < CLM; col++)
{
if (scores [row][col] > ACE)
numOfA++;
}
}
cout << "The number of A grades is " << numOfA << endl;
}
|