calculating average

Mar 19, 2013 at 12:44am
#include<iostream>
#include<iomanip>



how to calclate the average. average always =0
pls help me
Last edited on Mar 20, 2013 at 11:48pm
Mar 19, 2013 at 12:51am
Why wouldn't it be you set it to zero just before you try to print it. Maybe if you didn't do the assignment it might work.

1
2
3
4
5
6
void displayAverage(double av)
{

av =0.0; //???????????????????
cout << "Average is: " << av;
}
Mar 19, 2013 at 12:54am
I doubt it will given that the parameters for getTestScores are passed by value...
Mar 19, 2013 at 1:13am
so iw would like this
1
2
3
4
5
6
void displayAverage(double av)
{

av =;
cout << "Average is: " << av;
}
Mar 19, 2013 at 7:17am
what will i do to compute the average??
Mar 19, 2013 at 7:51am
Hi Justin123, the problem you get zero is because of void getTestScores(double s1, double s2, double s3). Simply change it to void getTestScores(double &s1, double &s2, double &s3).

Then for your void displayAverage() function, remove av =0.0;. Because it will display zero, not matter what your average is.

Hope this helps :)
Mar 20, 2013 at 6:44am
[code][



void displayAverage()
{

cout << "Average is: " ;
}
/code]
it doent work
Last edited on Mar 20, 2013 at 11:48pm
Mar 20, 2013 at 6:58am
You need to use reference for your getTestScores(double s1, double s2, double s3) to. So use the & there just like you do on calcAverege.
The problem is:
In your getTestScores you read the 3 variables, but they are only used there. They can't bee used in any other place. The variables a, b, c don't get the values you need so you can compute the averege.
Mar 20, 2013 at 8:16am
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
#include<iostream>
#include<iomanip>
using namespace std;

void getTestScores(double &s1, double &s2, double &s3);
double calcAverage(double &score1, double &score2, double &score3);
void displayAverage(double av);

int main()
{
double a, b, c;
double x, y, z;

double ave = 0;
getTestScores(a,b,c);
ave = calcAverage(a,b,c);
displayAverage(ave);
cout << endl;
return 0;
}

void getTestScores(double &s1, double &s2, double &s3)
{
cout << "Enter a score: ";
cin >> s1;
cout << "Enter another score: ";
cin >> s2;
cout << "Enter another score: ";
cin >> s3;
cout << endl;

}

double calcAverage(double &score1, double &score2, double &score3)
{
double average = 0;
average = (score1 + score2 + score3)/3;
return average;
}

void displayAverage(double av)
{


cout << "Average is: " << av;
}



first thing is to remove the statementav =0.0;because doing so would assign 0.0 to av which would subsequently get displayed

also pass your a,b,c by reference as in your function getTestscores you feed the values to s1, s2 , s3 which wont affect a,b,c unless you pass them by reference

the above code should work
Topic archived. No new replies allowed.