### Forum

How can I create two functions that has the same name GetAve. The first function should have 3 parameters for whole numbers and the second one should have 3 parameters for numbers with decimal points.

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; int main() { GetAve(20, 60, 100); GetAve(2.41, 3.333, 12.25); int x,y,z; cout<<"x = "; cin>>x; cout<<"y = "; cin>>y; cout<<"z = "; cin>>z; GetAve(x,y,z); float a,b,c; cout<<"a = "; cin>>a; cout<<"b = "; cin>>b; cout<<"c = "; cin>>c; GetAve(a,b,c); double u,v,w; cout<<"u = "; cin>>u; cout<<"v = "; cin>>v; cout<<"w = "; cin>>w; GetAve(u,v,w); return 0; }``````

Sample output:

 ```Average of 20, 60, and 100 is 60 Average of 2.41, 3.333, ad 12.25 is 5.99767 x = 2 y = 6 z = 9 Average of 2, 6, and 9, is 5 a = 2 b = 6 c = 9 Average of 2, 6, and 9 is 5.66667 u = 1.2 v = 2.4 w = 3.6 Average of 1.2, 2.4, and 3.6 is 2.4 ```
Last edited on
How to make the function with 3 parameters? I still can't understand it

Last edited on
No need to create 2 functions.
 ``1234567891011121314`` ``````#include //requires c++20 auto GetAverage(auto a, auto b, auto c) { return (a + b + c) / 3; } int main() { std::cout << "Average of 20, 60 and 100 is: " << GetAverage(20, 60, 100)<< "\n"; std::cout << "Average of 2.41, 3.333, ad 12.25 is: " << GetAverage(2.41, 3.333, 12.25); }``````
Last edited on
So this is my code but the output that i get is wrong.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include using namespace std; void GetAve (int ave1, int ave2, int ave3) { int average; average = ((ave1, ave2, ave3)/3); cout << "Average of " << ave1 << ", " << ave2 << ", and " << ave3 << " is " << average << "\n" << endl; } void GetAve (double ave1, float ave2, double ave3) { float average; average = ((ave1, ave2, ave3)/3); cout << "Average of " << ave1 << ", " << ave2 << ", and " << ave3 << " is " << average << "\n" << endl; } int main() { GetAve(20, 60, 100); GetAve(2.41, 3.333, 12.25); int x,y,z; cout<<"x = "; cin>>x; cout<<"y = "; cin>>y; cout<<"z = "; cin>>z; GetAve(x,y,z); float a,b,c; cout<<"a = "; cin>>a; cout<<"b = "; cin>>b; cout<<"c = "; cin>>c; GetAve(a,b,c); double u,v,w; cout<<"u = "; cin>>u; cout<<"v = "; cin>>v; cout<<"w = "; cin>>w; GetAve(u,v,w); return 0; }``````

output:

 ```Average of 20, 60, and 100 is 33 Average of 2.41, 3.333, and 12.25 is 4.08333 x = 2 y = 6 z = 9 Average of 2, 6, and 9 is 3```

20, 60, and 100 should be 60

2, 6, and 9 should be 5.66667
Last edited on
@thmm it should be two functions and the main function should be like this:

 ``12345678910111213141516171819202122232425`` ``````int main() { GetAve(20, 60, 100); GetAve(2.41, 3.333, 12.25); int x,y,z; cout<<"x = "; cin>>x; cout<<"y = "; cin>>y; cout<<"z = "; cin>>z; GetAve(x,y,z); float a,b,c; cout<<"a = "; cin>>a; cout<<"b = "; cin>>b; cout<<"c = "; cin>>c; GetAve(a,b,c); double u,v,w; cout<<"u = "; cin>>u; cout<<"v = "; cin>>v; cout<<"w = "; cin>>w; GetAve(u,v,w); return 0; }``````

or else it will not get accepted.
The comma operator is a binary operator that first evaluates the first operand and discards the result, then it evaluates the second operand and uses that as the result of the expression.

That means
 `` `` ``average = (ave1, ave2, ave3)/3;``
has the same meaning as
 `` `` ``average = ave3/3;``
Last edited on

You will also have a few ambiguity problems if the input variables are any type of whole number other than an int (long or long long or unsigned, for example). (As would the following code). This isn't a great function to be overloading.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include using namespace std; void GetAve (double ave1, double ave2, double ave3) { double average = ( ave1 + ave2 + ave3 ) / 3.0; cout << "Average of " << ave1 << ", " << ave2 << ", and " << ave3 << " is " << average << "\n\n"; } void GetAve (int ave1, int ave2, int ave3) { GetAve( double(ave1), double(ave2), double(ave3) ); } int main() { GetAve(20, 60, 100); GetAve(2.41, 3.333, 12.25); int x,y,z; cout<<"x = "; cin>>x; cout<<"y = "; cin>>y; cout<<"z = "; cin>>z; GetAve(x,y,z); float a,b,c; cout<<"a = "; cin>>a; cout<<"b = "; cin>>b; cout<<"c = "; cin>>c; GetAve(a,b,c); double u,v,w; cout<<"u = "; cin>>u; cout<<"v = "; cin>>v; cout<<"w = "; cin>>w; GetAve(u,v,w); }``````
Last edited on
It looks like the requirement for the whole number version is for a whole number answer and of that for the decimal point version is for a decimal point answer. In that case, the result for the whole number version could be misleading. But if that is what is wanted...

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; void GetAve(int a, int b, int c) { cout << "\nAverage of " << a << ", " << b << " and " << c << " is " << (a + b + c) / 3 << '\n'; } void GetAve(double a, double b, double c) { cout << "\nAverage of " << a << ", " << b << " and " << c << " is " << (a + b + c) / 3.0 << '\n'; } int main() { GetAve(20, 60, 100); GetAve(2.41, 3.333, 12.25); int x, y, z; cout << "x = "; cin >> x; cout << "y = "; cin >> y; cout << "z = "; cin >> z; GetAve(x, y, z); float a, b, c; cout << "a = "; cin >> a; cout << "b = "; cin >> b; cout << "c = "; cin >> c; GetAve(a, b, c); double u, v, w; cout << "u = "; cin >> u; cout << "v = "; cin >> v; cout << "w = "; cin >> w; GetAve(u, v, w); }``````

 ``` Average of 20, 60 and 100 is 60 Average of 2.41, 3.333 and 12.25 is 5.99767 x = 2 y = 6 z = 9 Average of 2, 6 and 9 is 5 a = 2 b = 6 c = 9 Average of 2, 6 and 9 is 5.66667 u = 1.2 v = 2.4 w = 3.6 Average of 1.2, 2.4 and 3.6 is 2.4 ```

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include // https://en.cppreference.com/w/cpp/concepts // average of three integer values double average( std::integral auto a, std::integral auto b, std::integral auto c ) { return ( double(a) + b + c ) / 3 ; // avoid integer division and potential integer overflow } // average of three floating point values double average( std::floating_point auto a, std::floating_point auto b, std::floating_point auto c ) { return (a+b+c) / 3 ; } // average of three numbers, where some are integers and others are floating point template < typename T > concept number = std::integral || std::floating_point ; double average( number auto a, number auto b, number auto c ) // less specialised than the two earlier functions { return (a+b+c) / 3 ; } int main() { std::cout << average( 1, 2ULL, char(52) ) << '\n' // 18.3333 (three integer values) << average( 1.2, 3.4f, 4.5L ) << '\n' // 3.0333 (three floating point values) << average( 1, 3.4, 5 ) << '\n' ; // 3.1333 (mixed integer and floating point values) // average( nullptr, "abcd", 8 ) ; // *** error *** : no matching function (constraints not satisfied) }``````

http://coliru.stacked-crooked.com/a/61b021e5bb75092d
Topic archived. No new replies allowed.