In: Computer Science
In C++
Having call-by-reference allows one to use storeback parameters, i.e., parameters whose values are transmitted back to their arguments. In this exercise, you are to write a program that provides several variations of a function that calculates the minimum of two values. Several of the functions also report whether the two values passed in were equal. The functions are all named min — they differ in their arguments and return values:
Call the file min.cpp. In the same file, write a main function demonstrating use of the above functions. The app should accept pairs of integers from the console (until eof), call each of the function sand print out the results using the format illustrated below:
Sample test run:
first number? 3 second number? 4 int min(x, y): The minimum of 3 and 4 is 3 bool min(x, y, min): The minimum of 3 and 4 is 3 int min(x, y, ok): The minimum of 3 and 4 is 3 void min(x, y, ok, min): The minimum of 3 and 4 is 3 first number? 5 second number? 2 int min(x, y): The minimum of 5 and 2 is 2 bool min(x, y, min): The minimum of 5 and 2 is 2 int min(x, y, ok): The minimum of 5 and 2 is 2 void min(x, y, ok, min): The minimum of 5 and 2 is 2 first number?
#include <iostream>
using namespace std;
int min(int a, int b){
if(a < b)
return a;
return b;
}
bool min(int a, int b, int &min){
if(a == b){
min = a;
return false;
}
if(a > b)
min = b;
min = a;
return true;
}
int min(int a, int b, bool &min){
if(a == b){
min = false;
return a;
}
if(a > b){
min = true;
return b;
}else{
min = true;
return a;
}
}
void min(int a, int b, bool ok, int &min){
if(a == b){
ok = false;
min = a;
}
if(a > b){
ok = true;
min = b;
}else{
ok = true;
min = a;
}
}
int main() {
int a, b, minimum;
bool ok;
cout<<"first number? ";
cin>>a;
cout<<"second number? ";
cin>>b;
cout<<"int min(x, y): The minimum of "<<a<<" and "<<b <<" is "<<min(a,b)<<endl;
min(a, b, minimum);
cout<<"bool min(x, y, min): The minimum of "<<a<<" and "<<b <<" is "<<minimum<<endl;
cout<<"int min(x, y, ok): The minimum of "<<a<<" and "<<b <<" is "<<min(a, b, ok)<<endl;
min(a, b, ok, minimum);
cout<<"void min(x, y, ok, min): The minimum of "<<a<<" and "<<b <<" is "<<minimum<<endl;
}
=======