0
#include <iostream>
#include <iomanip>

#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;


double meanOf3(double value1, double value2, double value3)
{
	
	double mean;
	mean = (value1 + value2 + value3)/3;
	return (mean);
}

int main()
{
	float value1, value2, value3;
	double mean;
	mean=0;
	

	cout <<"Enter the first integer whose square root will be included in the mean: ";
	cin  >>value1;
	cout <<"Enter the second integer whose square root will be included in the mean: ";
	cin  >>value2;
	cout <<"Enter the third integer whose square root will be included in the mean: ";
	cin  >>value3;
	
	value1 = sqrt(value1);
	value2 = sqrt(value2);
	value3 = sqrt(value3);

	meanOf3(value1, value2, value3);
	cout <<mean;


}

What I want to do is have 2 functions. One called meanOf3 which will calculate the mean of 3 numbers. The main function will prompt the user for 3 numbers and square root 3 numbers. After that, i would call the meanOf3 to figure out the mean of the squarerooted numbers.

However from what I wrote, I am getting totally wrong results. Can any1 help me out?

5
Contributors
4
Replies
8
Views
9 Years
Discussion Span
Last Post by Sky Diploma
0

why is that function not returning the mean ? You don't need that last parametewr

double meanOf3(double value1, double value2, double value3)
{
	return  (value1 + value2 + value3)/3;
}

int main()
{
    <snip>
    double mean = meanOf3(value1, value2, value3);

}
0

Simply combine line 36 and 37 will solve your problem:

[INDENT]cout << meanOf3(value1, value2, value3);[/INDENT]
0
mean3(value1, value2, value3);

Whats happening here is that your returned value has nothing to come back to. In other words, the function is executing but then the returned value (mean) is flying off into fairy world.

Do what Denniz said, just adding a "cout<<" to the front, or you could also just write:

mean = meanOf3(value1, value2, value3);

Both work fine

0

Though it is not preffered. You can declare mean as a global variable. So that you can get your program to work Just by snipping out the declarations and the return statement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.