Function to return largest of three numbers along with all same check – CSKC
Sign In

Search

Solved Problem #CPP#2951    siteicon   siteicon  

Problem Statement - Function to return largest of three numbers along with all same check

Write a function to return the largest of three integer numbers passed to it as value. Also pass a variable as reference which will be set to 1 if all the numbers passed are same. Show implementation of this function in the main routine with appropriate messages.

Solution

TC++ #2951

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Alternate (?) :

int largest(int a,int b,int c,int &same)
This function definition gets three integers as pass by value parameter and a parameter &same passed as reference.
{
if(a>b && a>c) return a; else if(b>a && b>c) return b; else if(c>a && c>b) return c;
This three conditions returns appropriate largest number.
else { same=1; return a;}
This condition sets the variable same (passed as reference) to 1. This also returns the number in a which will anyway be same as b and/or c if the all same condition is met.
}

int a,b,c; cout<<“Enter three numbers :”; cin>>a>>b>>c;
Suitable data input is collected from the user.
int same=0; //This variable is set as default to 0, which can be made 1 by the function.

int big=largest(a,b,c,same);
This is function call. The largest value is returned in big variable. Also if all the numbers are same, the variable same which is passed by reference will be updated to 1.

if(same) cout<<“All numbers are same”<<endl; else cout<<big<<” is largest”<<endl;
Suitable messages based on condition of same is printed here.

Notes

  • The way this program has been written is that it allows the optional use of variable same. If you just want to return the number even if all numbers are same you can still do so by not checking for condition of variable same.

Common Errors

  • int big=largest(a,b,c,&same); This is incorrect. Do as the program has suggested.


Share

Created: 23-Jan-2018 15:53 | Updated: 23-Jan-2018 16:07|