Across Apps and Sites.

For my class work assignment, I had to write a code that has two functions and an array that does the gathering of the grades and averages them. I have done everything but I cannot go beyond gathering of the average as the program is doing something else instead of working out the average that we require after placing in two numbers.
Let me drop the code below for your insight. Thanks!.


#include <iostream>

using namespace std;
// function and array declaration

void getData();
void findAverage();
const int SIZE = 10;
float allGrades[SIZE];

//main begins -------------------------------------------------
int main()
{
cout << "here we will find the average of your grades \n";
cout << "please enter your grades below" << endl;
getData();

cout << endl << "the average grade is: ";
findAverage;
return 0;
}
// main ends --------------------------------------------------
void getData()
{
int i = 0; //increment int

for (int i = 0; i < 10; i++)
{
cout << "enter a grade: ";
cin >> allGrades[i];
}
}


void findAverage()
{
int i = 0; // increment int
double sum = 0, avg;
for (int i = 0; i < 10; i++)
{
sum += allGrades[i];
}
avg = sum / 10;
cout << avg;
}.
welcome! That code looks pretty good and should work. Please use code tags (<> on the side editor).
it reads 10 numbers, not two.
find avg should use SIZE not 10. everything should, replace all those 10s with SIZE. Then you can change it once and fix the whole program if you need to do so.

you should try to not use global variables, and pass parameters to the functions, if you have learned that yet? Global variables cause lots of problems in larger programs and are a very bad habit.

you can, and should, put the loop variable in the for loop usually:
for(int i= 0; ...) instead of putting it outside the loop. It does not hurt anything, but this is the way most people expect to see it most of the time (only if you use it after the loop is over should it be kept).


---- what, then, is the something else the code is doing? If you have a typo and it did not compile, or if you forgot to compile, it could be running a previous version of the executable.

Last edited on
Please use code tags when posting code so that the code is readable!


[code]
// The code goes here
[/code]


 
findAverage;


That doesn't call the function. You need:

 
findAverage();

Last edited on
good eyes. ^^
Topic archived. No new replies allowed.