# Get rid off negative averages

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 06-08-2010
Barkley
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 grade[10];   int i,sum=0, avg=0;   int max=0,min=100;   for(i=0;i<10;i++)   {       cout << "Grade " << i+1 << ": ";       cin >> grade[i];   }   for(i=0;i<10;i++)   {       sum=sum+grade[i];       if(grade[i]>max)       {         max=grade[i];       }       if(grade[i]<min)       {         min=grade[i];       }   }   avg=sum/10;     cout << "Average Grade: " << avg << "%" << endl;   return 0; }```
• 06-08-2010
prog-bman
Example.
Code:

```if(avg < 0){ } else{ }```
• 06-08-2010
Barkley
Code:

```cout << "Average Grade: "; if (avg<0)cout << "---"; else cout << avg; cout << "%" << endl;```
would I do something like this?
• 06-08-2010
prog-bman
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;```
• 06-08-2010
Barkley
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 grade[10];       int i,sum=0, avg=0;       int max=0,min=100;       for(i=0;i<10;i++)       {         cout << "Grade " << i+1 << ": ";         cin >> grade[i];       }       for(i=0;i<10;i++)       {         sum=sum+grade[i];         if(grade[i]>max)         {             max=grade[i];         }         if(grade[i]<min)         {             min=grade[i];         }       } } int averageGrades() {   avg=sum/10;   cout << "Average Grade: ";   if (avg<0)       cout << "---";   else       cout << avg;     cout << "%" << endl; } int main() {   getGrades();   averageGrades();   return 0; }```
• 06-08-2010
syzygy
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.
• 06-08-2010
Barkley
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?
• 06-08-2010
syzygy
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.
• 06-08-2010
brewbuck
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.
• 06-08-2010
Barkley
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!
• 06-08-2010
syzygy
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.
• 06-08-2010
Barkley
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
• 06-08-2010
Barkley
so when I pass grade, avg, sum I get a compile error saying expected primary expression before int
• 06-09-2010
Elysia
When calling a function, you put the names of the variables you want to pass inside the (). Don't include type or other stuff.
• 06-09-2010
Barkley
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.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last