Need some "Proof-reading" to this code, help please :)

What you are about to see is probably the most inefficient code and disorganized for a beginner, however, i need your help to proof-read this? if thats even a thing in programming, the assignment i had is to generate 10 random numbers from 6 to 10, where the random numbers are the grades of certain subjects, and than in the end to prompt the average of the student. I managed to get this code to prompt me a certain average, but the randomization of the numbers is not working i think, everytime i run the program it prompts out the same numbers, any help on that? and any tips on to organize my code better to make it look smoother i guess :) , thanks in advance

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
#include <iostream>
#include <cstdlib>
#include <random>

using namespace std;

int main()
{
    int grade[9];
    int average_grade;
    int random_grade;
    default_random_engine generator;

    for(int i=0; i < 10 ; ++i)
    {

    uniform_int_distribution<int> distribution(6,10);
    random_grade = distribution(generator) ;
    grade[i] = random_grade;


    cout << " Grade: " << grade[i] << "\n" ;
    }

    average_grade = (grade[0] + grade[1] + grade[2] + grade[3] + grade[4] + grade[5] + grade[6] + grade[7] + grade[8] + grade[9]) / 10 ;
    cout << "The average grade of the student is: " << average_grade ;
}


Now what i think i did, is created an array for grade[]; and than for every time the loop runs, the generated random number would be assigned to the array,in order from 0-9, which seems to have worked just fine, please correct me if i did something wrong.
0) You are definin your array as containing 9 elements but generating 10. change int grade[9];int grade[10];
1) Do not use default_random_engine. Explicitely use Mersenne Twister: std::mt19937
2) You need to seed your generator. If your compiler provides correct std::random_device, use that, else old std::time(nullptr) will do trick for you.
3) Do not recreate distribution each iteration. Create it once before loop.
4) random_grade is not needed. Just directly assign result of distribution to the grade[i]
5) Your average grade calculation looks messy. Consider use of std::accumulate: average_grade = std::accumulate(grade, grade + 10, 0) / 10;
6) You are doing integer division. Is this really what you want?
7) Consider getting rid of magic number 10 and replace it with constant.
Topic archived. No new replies allowed.