# Thread: Get rid off negative averages

1. ## Get rid off negative averages

So I pulled this from the C++ tutorial here and played around with it to make it into a program that averages ten grades. How ever when I input all negative grades it gives a negative average. How would I and where would I input an if statement(not sure if this is what I would use) that would say basically if the average is less than zero than display ---%.

Thanks for your help I'm pretty new at all of this.
Code:
```int main()
{
int i,sum=0, avg=0;
int max=0,min=100;

for(i=0;i<10;i++)
{
cout << "Grade " << i+1 << ": ";
}
for(i=0;i<10;i++)

{

{

}

{

}

}

avg=sum/10;

cout << "Average Grade: " << avg << "%" << endl;

return 0;
}```

2. Example.
Code:
```if(avg < 0){
}
else{
}```

3. Code:
```cout << "Average Grade: ";
if (avg<0)cout << "---";
else cout << avg;
cout << "%" << endl;```
would I do something like this?

4. Minus that being a bit ugly the concept is good.
Code:
```cout<<"Average Grade: ";
if(avg < 0){
cout<<"---";
}//if
else{
cout<<avg;
}//else

cout<<"%"<<endl;```

5. Ok great I'll edit that, I also have another question if I still have your attention. How would I split this program up into functions? I tried this but when I enter all my grades I always get 0. Did I split them up right?
Code:
```int getGrades()

{
int i,sum=0, avg=0;
int max=0,min=100;

for(i=0;i<10;i++)
{
cout << "Grade " << i+1 << ": ";
}
for(i=0;i<10;i++)

{

{

}

{

}

}
}

{

avg=sum/10;

if (avg<0)
cout << "---";
else
cout << avg;

cout << "%" << endl;
}

int main()
{

return 0;
}```

6. I'm surprised that compiles. The concept you are missing is that variables declared in one function do not exist in another function, even if you use the same names. If you declare two variables with the same name in two separate functions, they will reside in two separate memory locations; i.e., they will not be the same variable.

You have to either 1) use global variables (which is generally frowned upon) or 2) learn to pass relevant data between your functions.

7. I thought I copied the latest of what I had but I declared the variables in both functions thus thats why I was getting zeros, so how do I pass variables then? It goes in the () right?

8. Read some documentation on it. It will explain things more thoroughly than I will right now (a little busy). Something like:
Cprogramming.com Tutorial: Functions

And I'm sure you can use Google to find other literature on function use. If you have questions after that, feel free to ask.

9. The average of a bunch of negative numbers is a negative number. What's the problem?

If you want to report that something is wrong, report that the individual grades are negative, not that the overall average is negative.

10. You know I was thinking about that, how would I do that though? In other words how would I make the program not take into account the value of -1?

And I will read up on it thanks!

11. Check the value before you process it; if it's negative, skip it, or do whatever kind of error processing you would like.

Since you are using for loops, it might be a good chance to look into the 'continue' keyword and try using that.

12. So from what I read about passing variables it needs to be in the () of the function that I am passing it too. So in my program would it be
Code:
`averageGrades(int grade[], int i)`
edit: Actually I need to pass the avg and the sum right?
Grade is an array so do I include the brackets?

also when I do this I get an "arguement too few compiler" error so that must not be right

13. so when I pass grade, avg, sum I get a compile error saying expected primary expression before int

14. When calling a function, you put the names of the variables you want to pass inside the (). Don't include type or other stuff.

15. I don't declare the type? As in the "int" are you sure? When I do this it just gives me a not declared compiler error. Then when I delcare it in the actual function and in main it still gives me the not delcared in this cope error.