Average function is not working.

My average and total in getaverage is always 3276. I have everything else working. Can you help me figure out why it is doing this?
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
   #include <iostream>
20 using namespace std;
21
22 int getgrades()
23 {
24    int grades[10];
25    int count = 0;
26    int  total = 0;
27    for (int i = 0; i < 10; i++)
28    {
29       count++;
30       cout << "Grade " << count << ": ";
31
32       cin  >> grades[i];
33       total += grades[i];
34 }
35    cerr << total;
36    return total;
37 }
38
39 int averagegrades(int i, int count, int total)
40 {
41    getgrades();
42
43    int average = (total /= 10 );
44    cout << "Average Grade: " << average << "%" << endl;
45    cout << total;
46 }
47
48 /**********************************************************************
49  * Add text here to describe what the function "main" does. Also don't forget
50  * to fill this out with meaningful text or YOU WILL LOSE POINTS.
51  ***********************************************************************/
52 int main()
53 {
54    int total;
55    int grades[10];
56    int count;
57    int i;
58    averagegrades(count, grades[i], total);
59    return 0;
60 }
average = total/10;

the statement you have modifies total, which it shouldn't, and may not give the result you want.

also, local variables are local.

void foo()
{
int x = 10;
}

void bar()
{
int x;
foo();
cout << x; //x could be *anything* its not the same x from foo!
}

the total in average grades is the total passed in from main which is not initialized nor the total you want which was up in get-grades.

-- as a rule of thumb, I highly recommend you never have 2 variables with the same name. This solves the problem of mixing them up out of scopes.

you need a way to get the value from one function to another... do you know how?

Last edited on
Thats what I thought. I just am not sure how to get that value over. I know it has to do with parameters I dont think I have a strong grasp on it though
Hints:
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
#include <iostream>
using namespace std;

int getgrades()
{
    // ***********************************************************************
    // These are local variables: they only exist inside this function.
    // Other variables with the same name which are in other functions are
    // different variables. You can think that there could be ten John Smith
    // who have the same name, but are different persons.
    // The rule is there can only be one of them in a specific function,
    // otherwise the compiler can't understand which one you want to use.
    int grades[10];     // <-- you don't need this one
    int count = 0;      // <-- you don't need this one
    int  total = 0;
    // ***********************************************************************

    // Just to clarify the situation to the user
    cout << "Please, give me ten integer values.\n";

    // The following is a similar, but slightly different situation:
    // the variable "i" is an integer variable as the ones above, but its life
    // will be even shorter: it only exists inside the for-loop. As soon as
    // you exit the loop, it disappears.
    for (int i = 0; i < 10; i++)
    {
        // You don't need "count". You've already got a variable which starts
        // from 0 and increases at each iteration: it's "i".
        // You can substitute the expression "i+1" for "count" inside
        // the "cout" statement. It won't modify the value of "i".
        count++;
        cout << "Grade " << count << ": ";

        // You don't need an array. You just need an integer variable, which
        // will be filled with the value given by the user at any iteration.
        // You can also define it here and its life will be the same as "i":
        // it will disappear as soon as the for-loop finishes.
        cin  >> grades[i];      // you can use a simple integer variables here...
        total += grades[i];     // ...and here
    }
    
    // Now you've get out from the for-loop.
    // "i" doesn't exist any more. It means you could define a new "i"
    // int i;
    // and use it without conflicts.
    // If you got rid of the array "grades" and substituted an integer variable
    // for it, that one would be disappeared as well. Pretty useful, isn't it?
    // Whereas "total" is still there, because it was defined before entering
    // the for-loop (better to say: outside the for-loop).

    cerr << total;  // <-- do you need this for debug?

    return total;   // <-- well done: the value of total is sent back to the caller
}

// You don't need any of these arguments
// As it works now, averagegrades() doesn't need to return any value, so it
// shouldn't be declared "int".
// Do you want to modify it so that it returns the average value to main() in 
// order to have main() print it out?
int averagegrades(int i, int count, int total)
{
    // This is the first function called by main(). In this situation main()
    // doesn't have any sensible value to pass to this function; therefore you
    // don't need any of the declared arguments.
    
    // The following instruction is formally correct, but fairly useless.
    getgrades();
    // Let's have a look at it together. You've called the function getgrades(),
    // which asks the user the values you need. That's fine, but at the end
    // getgrades() tries to send the total back to the called, i.e. this function.
    // Now, where has that value got to? :-)
    // Solution: you need an integer variable that receives the value returned
    // by getgrades().
    
    // You can call that variable "total", if you wish, even if there's already
    // one variable named "total" inside getgrades(). That's a different function,
    // therefore the compiler won't mix them up (it's important that *you* don't
    // get confused by this: two variables with the same name declared inside 
    // two different functions are different variables which happen to share
    // the same name, as well as their could be a namesake of yours somewhere 
    // who's not you).
    // Note: jonnin's suggestion (not to have two variables with the same name)
    //       is a good piece of advice, but in the future you'll write such large
    //       programs you'll even not be able to remember if you had already
    //       used that name or not. So I think it would be better if you
    //       get used, by trials and errors, to have more than one variable
    //       with the same name, but in different "scopes" (functions, loops...).
    // Now, if you have read up to here, please get back and correct the last
    // statement so as it stores the value returned by getgrades() inside a
    // local integer variable.
    
    // Granted you've decided to call the variable which stores the value
    // returned by getgrades() "total", the following line presents an error.
    // if you write
    // total /= 10;
    // you tell the compiler: divide the value of "total" by ten, and then
    // **store it again** inside "total".
    // You don't need to store it again inside "total", because it'll be stored
    // inside "average". You only need to divide it by 10.
    int average = (total /= 10 );
    
    // Again: let's make things clear for the user.
    cout << "Total: " << total << '\n';
    // Is it a percentage? I'm terrible at math, but I'm pretty sure it's not
    // a percentage...
    cout << "Average Grade: " << average << "%" << endl;
}

/**********************************************************************
* Add text here to describe what the function "main" does. Also don't forget
* to fill this out with meaningful text or YOU WILL LOSE POINTS.
***********************************************************************/
int main()
{
    // ***********************************************************************
    // You don't need any of these variables.
    // Values will be obtained by the user in the function getgrades();
    int total;
    int grades[10];
    int count;
    int i;
    // ***********************************************************************

    // You don't need to pass any argument to averagegrades().
    averagegrades(count, grades[i], total);
    return 0;
}
Last edited on
Topic archived. No new replies allowed.