Part I Write a program that uses a function that takes in tw

Part I: Write a program that uses a function that takes in two integers as inputand returns a minimum value (using pass-by-value). (HINT: the prototype lookslike: int minimum(int num1, int num2);
Part II: Modify your original program to include a function that take in fourintegers; two of which are passed-by-value, and the other two are passed-by-reference. The pass-by-reference integers are the min and max of the two pass-by-value integers. Find the min and max and \"return\" the values back to main.The prototype here is: void minmax(int &min, int &max, int num1, int num2);

Solution

Question1:

#include <iostream>

using namespace std;
int minimum(int num1, int num2);
int main()
{
int num1, num2, min;
cout<<\"Enter first number: \";
cin >> num1;
cout<<\"Enter second number: \";
cin >> num2;

min = minimum(num1, num2);
cout<<\"The minimum number is \"<< min<<endl;
  

return 0;
}

int minimum(int num1, int num2){
if(num1 < num2){
return num1;
}
else{
return num2;
}
return num1;
}

Output:

Enter first number: 5                                                                                                                                                                                                            

Enter second number: 4                                                                                                                                                                                                           

The minimum number is 4

Question 2:

#include <iostream>

using namespace std;
void minmax(int &min, int &max, int num1, int num2);
int main()
{
int num1, num2, min, max;
cout<<\"Enter first number: \";
cin >> num1;
cout<<\"Enter second number: \";
cin >> num2;
minmax(min, max, num1, num2);
cout<<\"The minimum number is \"<< min<<endl;
cout<<\"The maximum number is \"<<max<<endl;

return 0;
}

void minmax(int &min, int &max, int num1, int num2){
if(num1 < num2){
min = num1;
max = num2;
}
else if(num1 > num2){
  
min = num2;
max = num1;
}
else{
  
min = num1;
max = num1;
}
}

Output:

Enter first number: 6                                                                                                                                                                                                            

Enter second number: 5                                                                                                                                                                                                           

The minimum number is 5                                                                                                                                                                                                          

The maximum number is 6                                                                                                                                                                                                          

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                             

sh-4.3$ main                                                                                                                                                                                                                     

Enter first number: 4                                                                                                                                                                                                            

Enter second number: 5                                                                                                                                                                                                           

The minimum number is 4

Part I: Write a program that uses a function that takes in two integers as inputand returns a minimum value (using pass-by-value). (HINT: the prototype lookslik
Part I: Write a program that uses a function that takes in two integers as inputand returns a minimum value (using pass-by-value). (HINT: the prototype lookslik

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site