# Thread: Get rid off negative averages

1. Declaration:
Code:
`int AverageGrades(int grade[], int i);`
Calling:
Code:
`AverageGrades(grade, i);`

2. This is what I have now and I still get the undeclared in main. When I declare them then I get another totally different error.

also would it be(something along the lines of this)

Code:
```if (grade[i] > 100 || grade[i] < 0)
cout << "---"```
To get the program to not take into account negative grades in the average?

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++)

{

{

}

{

}

}
}

{
int avg;
int sum;

avg=sum/10;

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

cout << "%" << endl;
}

int main()
{

return 0;
}```

3. grade and i doesn't exist in main (obviously). They're declared in getGrades. Think about that for a while.
Also, are you coming from C or something? You obviously seem to love declaring variables at top. In C++, this is not necessary.
For example,
Code:
` for(i=0;i<10;i++)`
Can be written as:
Code:
` for(int i = 0; i < 10; i++)`

4. Originally Posted by Elysia
Also, are you coming from C or something? You obviously seem to love declaring variables at top. In C++, this is not necessary.
...
That's the least of his problems. In fact it's barely even a problem.

Why does GetGrades sum the series of grades, and why does it find the minimum and maximum? It seems like this function can't do one thing well, which is bad. If he is from a C background, he didn't stay long.

Try implementing functions that do one thing:

double GetAverage (int grades[] , size_t size);
void FindMinMax (int grades[] , size_t size , size_t *minPos , size_t *maxPos);
.. etc ....

If in GetGrades you ensure there are no negative marks, then there wont be negative averages. A good procedural design can isolate all sorts of bugs.

5. I have no background at all in anything hence why I pulled this from the tutorial here and tried to modify it haha. I have been trying to fool around with this language for about 3 weeks now and not getting too far hah. Sorry for the dumb thought process I just really haven't much of a clue what I'm doing still as you can tell by now. Each thing you post really helps though so thank you and please keep it coming haha.

6. So I still don't have a clue I've switched the variables all the ways I can think of and it still won't compile, can I get a major hint? maybe in the most simple form possible

7. What is your current code, and how does this updated code not work?

8. You can see functions are individuals performing work. They have to give them the information they need to carry out the task. And they have to hand that back when they're finished.
You can't just expect individual B to know whatever information individual A carries. And information being variables in this case.

9. So from getGrades the variables int grade and int sum need to be passed into averageGrades right? Then when I call it in main I just put grade, sum in the () of averageGrades.

10. this is what I have now, now I get an arguments too many compile error.

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++)

{

{

}

{

}

}
}

{

int avg;

avg=sum/10;

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

cout << "%" << endl;
}

int main()
{
int sum;

return 0;
}```

11. But GetGrades doesn't take any arguments. You haven't told the compiler that it does.
Also, you seem to not be fully understanding C++ syntax. You might want to read up on functions.

12. Originally Posted by Barkley
So from getGrades the variables int grade and int sum need to be passed into averageGrades right? Then when I call it in main I just put grade, sum in the () of averageGrades.
Well that's not good enough, obv. Problem is I don't think you understand exactly how a computer executes a program, so I will explain in simple terms with respect to C .

When the computer starts a program it calls main first. Programming folk call main the entry point because that is where the program starts. Now think of main as a task master. The job of main is going to be interact with the user and provide the output he wants. The programmer explains what he wants through writing a series of instructions to be followed in top down order, for the most part. When you have a working program you will have functions that do separate things --
Code:
```void GetGrades (int grades[] , size_t size);
double GetAverage (int grades[] , size_t size);
void FindMinMax (int grades[] , size_t size , size_t *minPos , size_t *maxPos);```
main calls these helper functions to do its job. In this program, main might have an average variable to present to the user, which is actually a reduction of the grades array.

The first helper main will have to hand control over to is here at GetGrades. GetGrades promises that grades will be filled when its done. main is a pretty lazy function, and doesn't want to do this itself, so this promise makes everything pretty copacetic. main gives its helper a pointer to grade's first element and a size variable, and then GetGrades is given the time to perform the task. main has to sit around while GetGrades is working.

After that, main decides it's time to find an average. main, being lazy, decides to wait on GetAverage to work out the answer:

Code:
`average = GetAverage (grades , size);`
main pretty much doesn't care how this answer comes about, but knows that GetAverage will need the grades array and its size, and knows that GetAverage returns an answer that fits in main's variable called average. main gives GetAverage time to complete its work, while main waits to assign the answer.

Then main dispatches the final task to FindMinMax. That function wants memory locations to store its answers, but promises that when it returns the answers will be correct. Thus explains our signature of FindMinMax:
Code:
`void FindMinMax (int grades[] , size_t size , size_t *minPos , size_t *maxPos)`
Because functions can't return more than one thing of a certain type. The main function is also responsible for these locations when it makes the call:
Code:
`FindMinMax(grades , size , &mindex , &maxdex);`
After this, main should have all the information it needs to present to the user in average, mindex, maxdex, and grade. These are all variables defined by (initialized in) and local to (belonging to, in the scope of) main. Here main actually does its own work formatting output before it ends the program, or it could dispatch the task again, depending on how you write main. You're the boss!

Actual programs have a lot more complicated flow charts, but it may be a while before you write anything more complicated than "main calls the foo function". It's all part of learning to know why these designs make sense, and basically how simple you can make it so it's free of bugs AND does what you need. There are plenty of tools at hand to implement a procedural program in C++.

13. GetAverage does not want the size of the array. That's C thinking. GetAverage can easily acquire the size of the array, if it needs it. That's why we use std::tr1::array, not native C arrays.
FindMinMax does not want memory locations to store its answers - it wants variables to store its answers. We use references instead. Plus GetAverage and FindMinMax will not modify the grades; thus the should be const.
More proper prototypes would be:
Code:
```void GetGrades (std::tr1::array<int, 10>& grades);
double GetAverage (const std::tr1::array<int, 10>& grades);
void FindMinMax (const std::tr1::array<int, 10>& grades, size_t& MinPos, size_t& MaxPos);```
I'd say you're thinking way too much C. This is the C++ forum. Let's not teach newbies C ways. Let's teach them modern C++ ways instead.