USING C++ Write a function which has 3 integer input values parameters which are
ID: 649512 • Letter: U
Question
USING C++
Write a function which has 3 integer input values parameters which are 3 distinct integers unsorted.
The function determines which number is the smallest the middle value and the largest value and puts them in 3 output parameters smallest, middle, largest.
The function prototype is:
void sort_3_numbers(int num1, int num2, int num3, int &smallest, int &middle, int &largest);
Write a main program which
Declares variables for both input and output.
Asks the user for the 3 input values (3 distinct integers to sort)
Calls the function by pass by reference
Prints out the original integers and then
Prints them the smallest, middle and largest values in order.
Test with the following input values:
34 84 2012
56 2011 83
65 49 7
199 62 98
85 62 198
93 205 17
EXAMPLE OF PASS BY REFERENCE- 2 input parameters and 4 output parameters
#include<iostream>
#include<stdlib.h>
using namespace std;
void my_function(int num1, int num2, int &sum, int &product, int &divisor, int &mod_answer);
int
main()
{
int num1, num2, sum, product, divisor, mod_answer;
cout<<"Please enter an integer> ";
cin>>num1;
cout<<"Please enter an integer> ";
cin>>num2;
my_function(num1,num2,sum,product,divisor,mod_answer);
cout<<"The sum of "<<num1<<" and "<<num2<<" is "<<sum<<"."<<endl;
cout<<"The product of "<<num1<<" and "<<num2<<" is "<<product<<"."<<endl;
cout<<"The divisor of "<<num1<<" and "<<num2<<" is "<<divisor<<"."<<endl;
cout<<"The remainder of "<<num1<<" divided by "<<num2<<" is "<<mod_answer<<"."<<endl;
system("Pause");
return 0;
}
void my_function(int num1, int num2, int &sum, int &product, int &divisor, int &mod_answer)
{
sum = num1 + num2;
product = num1 * num2;
divisor = num1/num2;
mod_answer= num1%num2;
}
Explanation / Answer
include<iostream>
#include<stdlib.h>
using namespace std;
void my_function(int num1, int num2,int num3, int &smallest, int &middle, int &largest);
int
main()
{
int num1, num2, num3, smallest, middle,largest;
cout<<"Please enter an integer> ";
cin>>num1;
cout<<"Please enter an integer> ";
cin>>num2;
cout<<"Please enter an integer> ";
cin>>num3;
my_function(num1,num2,num3,smallest,largest,middle);
cout<<num1<<" is largest from number 1 number2 and number 3 ";
cout<<"The largest num1"<< largest;
cout<<"The middle number "<<middle;
cout<<"The smallest number "<<smallest;
cout<<endl;
//system("Pause");
return 0;
}
void my_function(int num1, int num2, int num3,int &smallest,int &largest, int &middle)
{
/////////////////////////
if(num1>num2&&num1>num3&&num2>num3)
{
largest=num1;
middle=num2;
smallest=num3;
}
else if(num1>num2&&num1>num3&&num3>num2)
{
largest=num1;
middle=num3;
smallest=num2;
}
else if(num2>num1&&num2>num3&&num1>num3)
{
largest=num2;
middle=num1;
smallest=num3;
}
else if(num2>num1&&num2>num3&&num3>num1)
{
largest=num2;
middle=num3;
smallest=num1;
}
else if(num3>num1&&num3>num2&&num2>num1)
{
largest=num3;
middle=num2;
smallest=num1;
}
else if(num3>num1&&num3>num2&&num1>num2)
{
largest=num3;
middle=num1;
smallest=num2;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.