Answer to Question #220140 in C++ for Rolly

Question #220140


a. Critically explain why you would consider the concept of using methods in your 

program again running a couple of if else statements.


b. Declare a method with three arguments all of type int. The method should return the 

greatest of the three arguments, however if any two or all three are the same, you 

method should return that value ?

c. Embed you method in a complete program that requests for three variable all of type 

int and display the conditions explained in “b” above. 

d. Critically explain the logic behind the code?


1
Expert's answer
2021-07-24T07:07:53-0400

a. Functions reduces space and they than be recalled several times rather than writing the codes again and again.


b.

int greatest(int n1,int n2,int n3){
    if(n1 >= n2 && n1 >= n3)
        return n1;


    if(n2 >= n1 && n2 >= n3)
        return n2;
    
    if(n3 >= n1 && n3 >= n2)
        return n3;


}



c.


#include <iostream>


using namespace std;
int greatest(int n1,int n2,int n3){
    if(n1 >= n2 && n1 >= n3)
        return n1;


    if(n2 >= n1 && n2 >= n3)
        return n2;
    
    if(n3 >= n1 && n3 >= n2)
        return n3;


}
int main()
{
    int a,b,c;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;
    cout<<"The greatest number is "<<greatest(a,b,c);
    return 0;
}





d. The code takes three numbers, compares then and return the greatest number among them.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS