Class Score/ Average

So i built a simple loop/while program that asks for the amount of students in the class, and takes the individual score of each student, then averages them. It works as necessary, however for Line 12, I have originally put 'double avg;' while my professor put 'double avg = 0;' both seem to come out with the same result. Does anyone know if it is necessary or there's a gain in efficiency by using 'double avg = 0;' in comparison to 'double avg;'?

Thanks for any help ya'll

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
#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;

void aveg(int stu)
{
    int i=1;
    int sco;
    double total = 0;
    double avg=0;
    while (stu>=i)
    {
        cout << "What is the score of student "<<i<<" ? ";
        cin>>sco;
        i=i+1;

        total = total + sco;



    }
  avg = (total/(double)stu);
  cout<< "avg is "<<avg<<endl;
}

int main()
{
   int stu;
   cout<<"How many students are in the class?"<<endl;
   cin>>stu;
   aveg(stu);

}
It is considered best practice to always initialize your variables. There is no gain in efficiency.
Topic archived. No new replies allowed.