Array Help (again)

With this program, I am trying to make a program that has the user input 10 grades to be averaged. That is the easy part. I got that working, and now I am trying to make it so if the user inputs "-1", then that assignment has not been completed, and therefore will not factor into the average. I can't figure out how to do this. With what I have so far, I keep getting an error saying:
"hw15.cpp:46: error: name lookup of âiâ changed for ISO âforâ scoping
hw15.cpp:46: note: (if you use â-fpermissiveâ G++ will accept your code)" If anyone knows what that means, can they be of some assistance? Here is my code so far.

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
int getGrades(float grades[], int num)
{
   for(int i = 0; i <= num; i++)
   {
      cout << "Grade " << i + 1 << ": ";
      cin >> grades[i];
   }
}

/*********************************************8
 *AVERAGEGRADES
 *averages the grades the user has input
 ************************************************/

float averageGrades(float grades[], int num)
{
   float sum = 0;
   float avg;

   for(int i = 0; i <= num; i++)
   {
      sum += grades[i];
   }
   if (grades[i] = -1)
   {
      return false;
   }
   avg = sum / (num+1);
   return avg;
}

/*******************************************************
 *MAIN
 *calls getGrades and averageGrades and displays results
 *******************************************************/

int main()
{
   // declare array
   float grades[9];
   //fill them
   getGrades (grades, 9/*numGrades*/);
   float avg = averageGrades(grades, 9/*numGrades*/);
   //display results
   if (avg >= 0)
      cout << "Average Grade: " << avg
        << "%" << endl;
   else
      cout << "Average Grade: ---%" << endl;

   return 0;
}

Thank you.
You're using i outside of its scope (the for loop) on line 24. You are also using the wrong operator. = is for assignment and == is for comparison.

Is the return false; on line 26 just a placeholder? Because the function is supposed to return float. Dumb question, it must be.

Also, you are going out of the bounds of the arrays on lines 3 and 20.
float grades[9]; produces an array with 9 elements, indexed from 0-8. It does not produce an array of 10 elements.

http://cplusplus.com/doc/tutorial/arrays/
Last edited on
See, the reason i did [9] is because when i ran the program with grades[10] it asked for 11 grades instead of 10. So i changed it to [9] and it works. I'm just trying to make it so -1 is counted as an ungraded assignment, not counting toward the average.
it works

If you don't mind having segfaults or otherwise undefined behavior, then yes, I suppose it does.

it asked for 11 grades instead of 10

That's because you need to re-evaluate your for loops. Use < instead of <= to avoid that. Handling an array properly looks like this:
1
2
3
4
const int amount = 32;
int things[amount]; // has range 0-31 (32 indices)
for(int i = 0; i < amount; ++i) // loops i from 0-31
    things[i] = 0;


To ignore a score:
Check if the score is -1. If it's not, add it to the sum. If it is, don't. Keep track of how many scores you skip, because to get an average, you need to divide by the number of items summed.

avg = (s1 + s2 + s3 + ...) / (total - skipped)
Last edited on
How can i keep track of the scores I skipped?

EDIT: This is what I came up with. The average still comes up with 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

float averageGrades(float grades[], int num)
{
   float sum = 0;
   float avg;
   int score = 0;
   for(int i = 0; i < num; i++)
   {

      if (grades[i] == -1)
      {
         score += grades[i];
         return score;
      }
      else
         sum += grades[i];
      avg = sum / num - score;
      return avg;
   }
}


Any suggestions?
Last edited on
What are you trying to achieve with:
1
2
3
4
5
      if (grades[i] == -1)
      {
         score += grades[i];
         return score;
      }


At the moment, you initialise score to 0, and then when grades[i] is -1, you set score to -1 and then return -1.

In other words, if any grade is -1, your method will return a value of -1. I assume that isn't what you intended.

Also, even if grades[i] is never -1, how many times do you think your loop is going to execute?
Last edited on
Well, if there was more than one -1, wouldnt they add up to be -n? (n being how many -1's there are)
And if there is no -1, wouldn't the loop just skip the if statement?
There are some major problems with what you have here, which is what MikeyBoy was trying to point out, but you didn't see what he's talking about so I'll just say it.

Your loop will only run once, because of line 18. Take both lines 17 and 18 out of the loop.

Now let's go through what your loop does (once you move lines 17 and 18).
Check if the grade is -1. If it isn't, add it to the sum. If it is, add -1 to "score." Then return -1 from the function.

I'm not sure what "score" is or why you are using return. return exits from the function immediately.

Might I suggest instead of using whatever "score" is, use a variable that counts the number of grades that you have skipped (perhaps int skipped = 0;?). Then if the grade is -1, increment "skipped" by 1. After the for loop ends, divide the sum by (num - skipped). Note that the parenthesis are important, or else the order of operations will be incorrect, and your average will not be right.
Last edited on
Oh man! Thank you! I get it now. Ill fix some things up and try the program again. Hopefully it'll work. Thanks for everything ill do my best!
Okay, so the program itself works wonderfully, EXCEPT when the user inputs -1 for every number. I understand what happens when that happens, because after the loop is run, the formula avg = sum / (num - skipped) is then put into play. When -1 is put into every grade, the sum = 0 and skipped = 10 so that means that the formula that is run is avg = 0 / (10-10) . Since the final answer for that is undefined, how could I tell the program that if the answer is undefined then to have the output be
 
"Average Grade: ---%"
?
Here's the average grades function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
float averageGrades(float grades[], int num)
{
   int sum = 0;
   int avg = 0;
   int skipped = 0;
   for(int i = 0; i < num; i++)
   {

      if (grades[i] == -1)
         skipped += 1;
      else
         sum += grades[i];
   }
   avg = sum / (num - skipped);
   return avg;

}


and the main is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
   // declare array
   float grades[10];
   //fill them
   getGrades (grades, 10/*numGrades*/);
   float avg = averageGrades(grades, 10/*numGrades*/);
   //display results
   if (avg > 0)
      cout << "Average Grade: " << avg
           << "%" << endl;
   else
      cout << "Average Grade: ---%" << endl;

   return 0;
}

I understand that the if function won't work since it's undefined, but how could I tell the function to make it work? Any ideas?
Last edited on
Before you carry out the calculation on line 14 of averageGrades, make sure that num != skipped. If that's not the case, don't do the calculation and return a number that signifies that there is no average (maybe a negative number?).

Then handle the return value of averageGrades accordingly in main. You've already done this part, though.
Do you go to BYU-Idaho? I feel like you do....


anyways try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int getAverage(int grades[10], int num)
{

   int sum = 0;

   for (int i = 0; i < 10; i++)
   {
      if (grades[i] >= 0)
      {
         sum += grades[i];
         num ++;
      }
   }

   if (sum <= 0)
     cout << "Average Grade: ---%\n";

   else
       cout << "Average Grade: " << sum / num << "%" << endl;
   return 0;
}

I ended up having the program send the main -1 and it worked out great. Thank y'all so much for the help. This had been bugging me. I really appreciate it!
Topic archived. No new replies allowed.