Grades Average

Hello, i am having some trouble with this program i am working on. The objective of it is for the person to enter how many grades they are going to put in, then determining by that number its suppose to ask them what are the grades then going to a function doing the mult. and returning to main(). I don't know what im doing wrong.

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

double Average(int,int,int);



main(){
int answer,grades;
int average,final,count=0;
cout<<"How many grades are you going to enter?";
cin>>answer;
while (count<answer)
{
cout<<"What are your grades?";
cin>>grades;
final=Average(answer,grades,average);

}

return 0;
}

double Average(int answer,int grades,int average)
{
int final;

}
Last edited on
There are a lot of problems here.

First, you declare an integer variable called count which you initialize to zero. Then you enter a loop which checks the count against the answer, which is fine. But you never decrement the answer, so the loop goes on forever. Instead of using a while loop, consider using a for loop instead:

1
2
3
4
5
6
7
//...
cin>>answer;
for(unsigned int i = 0; i < answer; i++)
{
   cin>>grades;
//...
}


The average of a set of length n is given by the sum of all elements of the set divided by n. Here you divide only one element by the length of the set. To fix this you'll need to create a dynamic array that gets populated by the users choice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int* grades = 0;
cout<<"How many grades are you going to enter?";
cin>>answer;
grades = new int[answer];

for(unsigned int i = 0; i < answer; i++)
{
    cout<<"What are your grades?";
    cin>>grades[i];
}

final = Average(answer, grades);

cout<<"Your average is "<<final<<endl;    

//delete[] grades; 


here we are using the 'new' keyword to set aside a group of memory the size of int.

Then we loop the number of grades the user has and assign it to the array at position i.

once we get all the grades, we send the array to the Average function, which calculates the average of all the grades entered and returns the result.

Then we output the result.

You are going to need to modify your average function so that it takes an array:

 
float Average(int, int[]);


Try to write the average function yourself. You have enough information here to do so.

If you have questions, post them. The people here are nice and like to help.
Topic archived. No new replies allowed.