Answer to Question #219153 in C++ for Onai Maxwell

Question #219153
a. Critically explain why you would consider the concept of using functions in your program again running a couple of if else statements.

b. Declare a function with three arguments all of type int. The function should return the greatest of the three arguments, however if any two or all three are the same, you function should return that value.

c. Embed you function 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-20T17:02:41-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