Codeblock Logical Error
I have written this simple program (codeblocks), look at the results I get in the console:
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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int rows = 5; // 5 students
const int cols = 8;
int total[rows][1];
double average[rows][1];
int grades[rows][cols];
srand(time(NULL));
for (int r = 0; r < rows; ++r)
{
for (int c = 0; c < cols; ++c)
grades[r][c] = rand() % 81 + 20;
}
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c)
cout << grades[r][c] << " ";
cout << endl;
}
cout << endl;
for (int r = 0; r < rows; ++r) {
total[r][1] = 0;
for (int c = 0; c < cols; ++c)
total[r][1] += grades[r][c];
average[r][1] = (double) total[r][1] / cols;
}
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c)
cout << grades[r][c] << " ";
cout << endl;
}
cout << endl;
for (int r = 0; r < rows; ++r) {
cout << "Student " << r+1 << ": ";
for (int c = 0; c < cols; ++c)
cout << grades[r][c] << " ";
cout << "\tThe average: " << average[r][1] << endl;
}
cout << endl;
for (int r = 0; r < rows; ++r) {
total[r][1] = 0;
for (int c = 0; c < cols; ++c)
total[r][1] += grades[r][c];
average[r][1] = (double) total[r][1] / cols;
}
for (int r = 0; r < rows; ++r) {
cout << "Student " << r+1 << ": ";
for (int c = 0; c < cols; ++c)
cout << grades[r][c] << " ";
cout << "\tThe average: " << average[r][1] << endl;
}
return 0;
}
|
31 73 82 30 34 68 74 40
57 25 83 52 30 62 83 45
97 86 42 34 88 100 61 41
100 23 51 92 38 79 53 55
71 26 65 34 73 97 66 36
1078804480 73 82 30 34 68 74 40
57 25 83 52 30 62 83 45
97 86 42 34 88 100 61 41
100 23 51 92 38 79 53 55
71 26 65 34 73 97 66 36
Student 1: 1078804480 73 82 30 34 68 74 40 The average: 54
Student 2: 57 25 83 52 30 62 83 45 The average: 54.625
Student 3: 97 86 42 34 88 100 61 41 The average: 68.625
Student 4: 100 23 51 92 38 79 53 55 The average: 61.375
Student 5: 71 26 65 34 73 97 66 36 The average: 58.5
Student 1: 1078804480 73 82 30 34 68 74 40 The average: 1.34851e+008
Student 2: 57 25 83 52 30 62 83 45 The average: 54.625
Student 3: 97 86 42 34 88 100 61 41 The average: 68.625
Student 4: 100 23 51 92 38 79 53 55 The average: 61.375
Student 5: 71 26 65 34 73 97 66 36 The average: 58.5
Process returned 0 (0x0) execution time : 0.616 s
Press any key to continue.
|
Topic archived. No new replies allowed.