average

PROBLEM that so difficult

creating a program that calculates the average of the three test scores. The program should contain two value-returning functions(main and calcave)and two void function(getScore and displayAve).The main function should call the void getScore function to get the three test score . The test score may contain a decimal place. The main function then should call the value-returning calcave funtion to calculate and return the average of the three test scores. When the calcave function has completed its task, the main function should call the void displayAve funtion to display the average of the three test scores on the screen. Dislay the average with one decimal place. Use a sentinelvalue to end the program.
My problem is how do i loop it here is my 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
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;
void gscores(double s1,double s2,double s3);
void disave(double s1,double s2,double s3);
void  calave(double s1,double s2,double s3);
int main()
{
    float ts1=0.0;
    float ts2=0.0;
    float ts3=0.0;
    
    cout<<"Enter test score 1:   ";
    cin>>ts1;
    cout<<"Enter test score 2:   ";
    cin>>ts2;
    cout<<"Enter test score 3:   ";
    cin>>ts3;
   
    cout<<endl<<endl;
    disave(ts1,ts2,ts3) ;
    system("PAUSE");
}
 void gscores(double s1,double s2,double s3)
 {
    cout<<"Enter test score 1:   ";
    cin>>ts1;
    cout<<"Enter test score 2:   ";
    cin>>ts2;
    cout<<"Enter test score 3:   ";
    cin>>ts3;
  }

            
 
 void  disave(double s1,double s2,double s3)

 {   
     float getscores=0.0;
     float ave=0.0;
          
     getscores=s1+s2+s3;
     ave =getscores/3;
     cout<<"The average of " <<getscores <<" is "<<ave<<endl;
 }
void  calave(double s1,double s2,double s3);
return ave;    

I really need help
Thanks for a good heart for helping me and really appriciate who will help me







Last edited on
Well, I believe the problem here is that you're not using calcave as you should- that is what your main should call first, and that is what should calculate the average, not the disave proc. The main function would call the calcave proc, passing to it ts1, ts2, and ts3. It would then calculate the average, and return that new value which you would store in a variable. You would then call the disave proc, passing to it the average that you just stored to a variable. That wold then simply display the average. As for your main, you shouldn't be asking for scores in it. It should simply say something along the lines "Please input scores" and then call the proc getscores() to set ts1, ts2, and ts3 to their respective values (there's more to it than simply using cin, but it's not too advanced, so it shouldn't be too hard to figure out). So all the main would do in the end is call getscores, call calcave while passing ts1, ts2, and ts3, and then call disave while passing the resultant value from calcave.

I hope that helps.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

// function prototypes
void getscore(int &);         //: refrence variable
double calcaverage(double &);

int main ()
{	int score;
	
	double averagescore;
	double totalscore = 0;

	cout <<"Please enter five scores.\n\n";
	
	getscore(score);
	totalscore += score;
	getscore(score);
	totalscore += score;
	getscore(score);
	totalscore += score;
	getscore(score);
	totalscore += score;
	getscore(score);
    totalscore += score;
	
	
	
	averagescore = calcaverage();
	

	cout <<"The total is "<< totalscore << endl;
	cout <<"The average of the scores is " << averagescore << endl;

system ("pause");

return 0;
}
//==========================================================================================//
double calcaverage(double &totalscore)
{	double average ;
	average = totalscore / 5;
	return totalscore;


}


void getscore(int &score)
{  cin >> score; 
    
  while (score < 1 || score > 100)
   {cout<<"Scores must be between 1 and 100, re-enter the score" << endl;
	cin >> score;
   }
}

averagescore = calcaverage();

double calcaverage(double &totalscore)
{	double average ;
	average = totalscore / 5;
	return average;


}

average=calcaverage(totalscore);
Yes. That looks much better. However, when you used the calcaverage function, you didn't pass to it the current value of totalscore. Since totalscore isn't a global variable, it won't work when you try it. Also, your calcaverage function returns totalscore instead of average. Your getscore function should actually be using score <=0 and score >= 100, since it should include 0.1 and 100. Also, for this assignment, are you allowed to use pointers? If not, you will need a different method than what you did with getscore.
no , we are not yet in pointers
Last edited on
Topic archived. No new replies allowed.