Not take into account negative average

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
int main()
{
   int age[10];
   int i,sum=0, avg=0;
   int max=0,min=100;

   for(i=0;i<10;i++)
   {
      cout << "Grade " << i+1 << ": ";
      cin >> age[i];
   }
   for(i=0;i<10;i++)

   {

      sum=sum+age[i];

      if(age[i]>max)

      {

         max=age[i];

      }
     
     if(age[i]<min)

      {

         min=age[i];

      }

   }

   avg=sum/10;

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

   return 0;
}


How would I edit this program to not return a negative average

basically I would want it to
---%
if the average were negative.

Would I have to write another if statment in the for loop or would I have to change the for loop all together?
Couldn't find the verb ---% in my dictionary, but you're probably looking for abs:

cout << "Average Grade: " << abs(avg) << "%" << endl;
Last edited on
Oh ok that returns it with a positive percentage.

but if I wanted the
---%
output would I need to put an if statement somewhere and say if its a negative average then return "---%" I guess thats my question where would I put the if statement in the for loop or just on its own.
Last edited on
Ah. Well, you would put it where you're outputting the average, of course... it makes no sense whatsoever to put it inside the loop.

1
2
3
4
cout << "Average Grade: ";
if (avg<0)cout << "---";
else cout << avg;
cout << "%" << endl;

Topic archived. No new replies allowed.