Array Issue

Hello all, new to the forums here, and yes I have searched around on the internet and on this forum for this topic.
So I have this program that's a homework assignment, and I started with some already written code:
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
#include <iostream>
using namespace std;

const int MAX = 10;

void getGrades(int grades[], int numOfGrades)
{
   for (int i = 0; i < numOfGrades; i++)
   {
      cout << "Grade " << (i + 1) << ": ";
      cin >> grades[i];
   }
}

int compAverageGrades(const int grades[], int numOfGrades)
{
   int totalGrade = 0;

   for (int i = 0; i < numOfGrades; i++)
      totalGrade += grades[i];

   return (totalGrade / numOfGrades);
}

int main()
{
{
   int grades[MAX];
   getGrades(grades, MAX);
   int averageGrade = compAverageGrades(grades, MAX);
   cout << "Average Grade: " << averageGrade << "%\n";

   return 0;
}


The professor said "Modify the function compAverageGrades() so that it does not take into account grades with the value of -1"

I am not really sure how to do this, the reading was on how to partially fill an array, how to search an array, and how to sort an array, and it wasn't even very clear either.
So any help would be great, suggestions, readings, that kind of thing, or anything.
I really want to understand what to do, not looking for a quick fix if you can explain it.

Thanks in advanced.
Last edited on
http://cplusplus.com/doc/tutorial/variables/

Read under "Declaration of variables". What you are looking for (variables that only use positive numbers?) is (/should be) here.
I know how to declare variables, and that page makes sense, I guess I need to be a little more clear.
Its a program that takes in 10 grades, ranging from 0% to 100%, and then returns there average.
Basically the teacher wants to input a -1 as if saying that the test was not taken, not that the student got a zero, but that it wasn't taken at all, and so shouldn't count toward the total average.
Does that make any sense?
Okay, not really, but I'll try:

You want the user to input a value for the percentage grade, but if the person did not take a test, you want the user to input "-1" to signify to the program that the student didn't take the test, and to use one less test for the overall average?

So if there are ten students in the class, but one was absent and didn't write the test, input the other nine grades as per normal, but for the absent student, input "-1" and the program would discard that student.

Ex:

Ten student, the one absent, input would be similar to:

98%
76%
87%
55% (this would be mine)
60%
81%
45%
82%
66%
-1

and the average would be 72% as opposed to 65% if a mark of 0% were given to the person who didn't take the test.

Sorry about causing so much confusion.
Yes, that is correct.
No worries about the confusion, very confused myself right now.
I think you can achieve this by using two separate variables and an if statement. What I am saying is, if the grade is valid, it will be stored in a variable, say totalgrade and then another variable, say gradecount will be incremented as well.

This would function as, you are adding up the total of the valid grades while also taking note of the number of valid ones. By the end of the list and with proper implementation, I would presume that it will add up the total, you can then divide it by the gradecount variable you used. And you would have the average.

That's how I understand the problem.
SithDagger, it sounds like you're on the same chapter I'm on.

I imagine you'd need to modify compAverageGrades(). First, declare another int variable, newSize = numOfGrades. Second, in the for loop, if grade[i] == -1, then newSize--, else totalGrade += grade[i]. Finally, return the totalGrade / newSize, noting the difference.
And the function might need to return a value of type double instead, which would require a couple more minor changes.
Audie, thank you so much! that solved almost all my problems! :D
Topic archived. No new replies allowed.