Functions C++ - Error in the output of my code

I was assigned this question:

A Class of 40 students has received their grades for 5 exams (each out of 100). Write a structured C++ program (using top-down design) that will do the following:
a. Write function readData which reads the student data list which consist of the Student ID
b. Write function printAvg that prints the ID and average score for each student for 5 consecutive grades. Do the necessary validation on input.
c. Write a function worstAvg that calculates the worst average grade.
d. Write a function that prints the IDs of all students having the worst average grade

I wrote the code and it runs, however it calculates the average grade of each student wrong and I can't figure out why. Is it the for loop? Could someone please have a look at the code and tell me what I'm missing.
Thanks in advance!

Here's my code:

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
// Program directives
#include <iostream>
#include <string>
using namespace std;

// Supporting Functions
void readData (int);
void printAvg (int, int [], float []);
void worstAvg (int, float []);
void printID (int, int [], float [], float);

// Global variables
int numStudents; // Number of students input

int sizeID = 40;
int ID[40]; // ID Array
long int sizeGrades = 200;
float Grades[200]; // Grades Array
float Avg[40];

int id; // ID input
float scores, total=0; // Grades input
float worst = 100; // Worst possible Average

// Defining main function
int main()
{
    // Input
    cout << "Enter the number of students: ";
    cin >> numStudents;
    
    while (numStudents > 40)
    {
        cout << "Number of students is more than 40! Re-enter: ";
        cin >> numStudents;
    }
    
    readData(numStudents);
    
    return 0;
} // End of main function


// End of readData function
void readData(int numStudents)
{
    // Entering students ID
    for (int k = 0; k < numStudents; k++)
    {
        cout << "Enter the Student ID: ";
        cin >> id;
        
        ID[k] = id;
    }
    
    // Entering the 5 scores of the students
    for (int i = 0; i < numStudents; i++)
    {
        cout << "Enter the 5 scores of the Student: " << endl;
        
        for (int j = 0; j < 5; j++)
        {
            cin >> scores;
            
            while (scores > 100)
            {
                cout << "The score entered is bigger than 100! Re-enter: ";
                cin >> scores;
            }
            
            Grades[i] = scores;
        }
        
    }
    
    printAvg (numStudents, ID, Grades);
    
} // End of readData function


// Defining printAvg function
void printAvg(int numStudents, int ID[], float Grades[])
{
    float sum = 0;
    
    for (int n = 0; n < numStudents; n++)
        for (int m = 0; m <= (numStudents * 5); (m = m + 5))
        {
            sum = sum + (Grades[m] + Grades[m+1] + Grades[m+2] + Grades[m+3] + Grades[m+4]);
            Avg[n] = (sum / 5);
        }
    
    for (int c = 0; c < numStudents; c++)
        cout << "ID: " << ID[c] << " Average: " << Avg[c] << endl;
    
    worstAvg(numStudents, Avg);
    
} // End of printAvg function



// Defining worstAvg function
void worstAvg(int numStudents, float Avg[])
{
    for (int s = 0; s < numStudents; s++)
        if (Avg[s] < worst)
            worst = Avg[s];
    
    cout << "The worst average of them all is: " << worst << endl;
    
    printID(numStudents, ID, Avg, worst);
    
} // End of worstAvg function


// Defining printID function
void printID(int numStudents, int ID[], float Avg[], float worst)
{
    for (int h = 0; h < numStudents; h++)
        if (Avg[h] == worst)
            cout << "ID: " << ID[h] << " Average: " << Avg[h] << endl;
    
} // End of printID function 
Last edited on
This line:

Grades[i] = scores;

You write all 5 scores for student i in the ith element of the array. However, elements 0 - 4 should belong to student 0, 5 - 9 to student 1, etc.

When you post code, pleas use code tags. This will make it easier for us to read and comment on. Type "[code ]" (without the spaces) before and "[/code ]" after your code. You can also edit your post, highlight your code and click the "<>" Format button.
This did work once and generated a random board, but on the second run of the
program the board remains the same. I am new to C++ and trying to understand
why it will no generate a new set of random numbers into the board 1-9.

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <iomanip>


using namespace std;

void showgame();
void player();
bool gameover();
int bboard[3][3];


char player1, player2;
char user;
bool tie = false;


char aboard[3][3]= {{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}};


int main()
{


cout << "Tic Tac Toe\n";
do{
cout << " Player 1 is first select either X or O: ";
cin >> player1;
}while(player1 != 'X' && player1 != 'O');


if(player1 == 'X')
{
player2 = 'O';
user = 'X';
}
else if (player1 == 'O')
{
player2 = 'X';
user = 'O';
}


while(!gameover())

{
showgame();
player();
gameover();
}
if(user == 'X' && !tie)
{
showgame();
cout << "Tic Tac Toe: O Wins!\n";
}
else if(user == 'O' && !tie)
{
showgame();
cout << "Tic Tac Toe: X Wins!";
}
else
{
showgame();
cout << "Tie Game!";
}
}


void showgame()
{
int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::random_shuffle(numbers, numbers + 9);
int bboard[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
bboard[i][j] = numbers[i * 3 + j];

cout<< " "<<" Player 1: "<<player1<<"\n";
cout<< " "<<" Player 2: "<<player2<<"\n";
cout<< " "<<"-----------------------"<<endl;

cout << " | | " " " " | | "<<endl;
cout << " "<<aboard[0][0] << " | " <<aboard[0][1] << " | " <<aboard[0][2] << " " <<bboard[0][0] << " | " <<bboard[0][1] << " | " <<bboard[0][2] <<endl;
cout << " | | " " " " | | "<<endl;
cout << "______ _________ ______" " " "______ _________ ______"<<endl;
cout << " | | " " " " | | "<< endl;
cout << " "<<aboard[1][0] << " | " <<aboard[1][1] << " | " <<aboard[1][2] << " " <<bboard[1][0] << " | " <<bboard[1][1] << " | " <<bboard[1][2]<<endl;
cout << " | | " " " " | | "<< endl;
cout << "______ _________ ______ " " " "______ _________ ______ "<< endl;
cout << " | | " " " " | | "<<endl;
cout << " "<<aboard[2][0] << " | " <<aboard[2][1] << " | " <<aboard[2][2] << " "<<bboard[2][0] << " | " <<bboard[2][1] << " | " <<bboard[2][2]<< endl;
cout << " | | " " " " | | "<<endl;
cout << " | | " " " " | | "<<endl;
}

void player()
{
int selection;
int row=0, column = 0;


if (user == 'X')
{
cout <<"X is up: ";
}
else if (user == 'O')
{
cout <<"O is up: ";
}

cin >> selection;

switch (selection)
{
case 1: row = 0; column = 0;
break;
case 2: row = 0; column = 1;
break;
case 3: row = 0; column = 2;
break;
case 4: row = 1; column = 0;
break;
case 5: row = 1; column = 1;
break;
case 6: row = 1; column = 2;
break;
case 7: row = 2; column = 0;
break;
case 8: row = 2; column = 1;
break;
case 9: row = 2; column = 2;
break;
default:
cout << "Number input is incorrect, input a number from 1-9! Try again.\n";
player();
}

if (user == 'X' && aboard[row][column] != 'X' && aboard[row][column] != 'O')
{
aboard[row][column] = 'X';
bboard[row][column] = 'X';
user='O';

}
else if (user == 'O' && aboard[row][column] != 'X' && aboard[row][column] != 'O')
{
aboard[row][column] = 'O';
bboard[row][column] = 'O';
user='X';

}
else
{
cout << "The space is already taken by the other player, choose an open space.\n";
player();
}
}

bool gameover()
{
for (int i = 0; i < 3; i++)
{
if ((((((((aboard[i][0] == aboard[i][1] && aboard[i][1] == aboard[i][2]) ||

(aboard[0][i] == aboard[1][i] && aboard[1][i] == aboard[2][i])||

(aboard[0][0] == aboard[1][1] && aboard[1][1] == aboard[2][2])||

(aboard[0][2] == aboard[1][1] && aboard[1][1] == aboard[2][0])||

(bboard[i][0] == bboard[i][1] && bboard[i][1] == bboard[i][2])||

(bboard[0][i] == bboard[1][i] && bboard[1][i] == bboard[2][i])||

(bboard[0][0] == bboard[1][1] && bboard[1][1] == bboard[2][2])||

(bboard[0][2] == bboard[1][1] && bboard[1][1] == bboard[2][0]))))))))
{
return true;
}


for (int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
if ((aboard[x][y] != 'X' && aboard[x][y] != 'O') || (bboard[x][y] != 'X' && bboard[x][y] != 'O'))
{
return false;
}
}
tie = true;
return true;
return 0;
}
}
}
You need to seed the random number generator. Otherwise, random_shuffle will use the same sequence of random numbers each time, leading to the same resultant order of your array.

std::srand ( unsigned ( std::time(0) ) );

Do this once at the beginning our your program. See the example here:

http://www.cplusplus.com/reference/algorithm/random_shuffle/?kw=random_shuffle
Last edited on
Registered users can post here. Sign in or register to post.