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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/* Program: Grade Matrix
* Description: To manipulate one and two dimensional arrays and pass arrays as
* arguments to functions in order to calcualte student averages and quiz
* averages.
*/
# include <fstream>
# include <iostream>
using namespace std;
const int NUM_STU = 4;
const int NUM_QUIZ = 3;
// function prototypes
void getMatrix (ifstream&, int matrix [] [NUM_QUIZ]);
//reads matrix from file
void compStuAvg (const int matrix [] [NUM_QUIZ], float []);
//calcs avgs to a one-dimensional array
void compQuizAvg (const int matrix [] [NUM_QUIZ], float []);
//calcs quiz avgs
void displayData (ofstream& outfile, const int matrix [] [NUM_QUIZ], const float [],
const float []);
//displays data
int main ()
{
ifstream data;
ofstream out;
int matrix [NUM_STU][NUM_QUIZ];
float student_avg, quiz_avg;
data.open ("grade_matrix.txt"); //file for input
if (!data)
{
cout << "Error!!! Failure to open grade_matrix.txt" << endl;
system ("pause");
return 1;
}
out.open ("out.txt"); //file for output
if (!out)
{
cout << "Error!!! Failure to open out.txt" << endl;
system ("pause");
return 1;
}
getMatrix (data, matrix);
compStuAvg (matrix, student_avg);
compQuizAvg (matrix, quiz_avg);
displayData (outfile, matrix, student_avg, quiz_avg);
//do I need a loop here??? Such as a while (data)... {}
system ("pause");
return 0;
} //end of main
void getMatrix (ifstream& data, int matrix [] [NUM_QUIZ])
{
for (int i = 0; i < NUM_STU; i++)
{
for (int j = 0; j < NUM_QUIZ; j++)
{
data >> matrix [i][j];
}//inner loop
}//outer loop
}//getMatrix
void compStuAvg (const int matrix [] [NUM_QUIZ], float student_avg [])
{
float sum;
for (int i = 0; i < NUM_STU; i++)
{
sum = 0;
for (int j = 0; j < NUM_QUIZ; j++)
sum = sum + matrix [i][j];
student_avg [i] = sum / NUM_STU;
//should I be returning a value or even just 0???
}
}
//compStuAvg
void compQuizAvg (const int matrix [] [NUM_QUIZ], float quiz_avg []);
{
float sum;
for (int i = 0; i < NUM_STU; i++)
{
sum = 0;
for (int j = 0; j < NUM_QUIZ; j++)
sum = sum + matrix [i][j];
quiz_avg [i] = sum / NUM_QUIZ;
//should I be returning a value or even just 0???
}
}
//compQuizAvg
void displayData (ofstream& outfile, const int matrix[] [NUM_QUIZ],
const float student_avg[], const float quiz_avg[])
outfile << /*??? I'm lost here after I do the above calculations, how do I
display that??? I can't find a good example anywhere quite like this in my book
or on the web...
/
*/
|