Question

In: Computer Science

In C++ Having call-by-reference allows one to use storeback parameters, i.e., parameters whose values are transmitted...

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:

  • Accept two integers and return their minimum
  • Accept three parameters: two integers, and a third integer storeback set to the minimum of the first two. Return a boolean set to true if the inputs are not equal, and false otherwise.
  • Accept three parameters: two integers, and a boolean storeback set to true if they are not equal and false otherwise. Return an integer set to the minimum of the two inputs.
  • Accept four parameters: two integers, a boolean storeback set to true if they are not equal and false otherwise, and another integer storeback set to the minimum of the two.

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?

Solutions

Expert Solution

#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;

}

=======


Related Solutions

C# Discussion question Why can reference parameters not be optional parameters? What are the conditions for...
C# Discussion question Why can reference parameters not be optional parameters? What are the conditions for generating ambiguous methods? What arethe differences between mandatory parameters and optional parameters? Explain the use of arguments in reference parameters, output parameters, and parameter arrays. Expalin overloading.
For C++ Make a function that swaps the values that change the values between the parameters...
For C++ Make a function that swaps the values that change the values between the parameters as the following rule. The functions swap takes 2, 3 or 4 parameters and these functions are implemented as the overloading functions. swap(int &, int &) : change the values each other. swap(int &, int &, int &) : change the value with the order. [min, medium, max] swap(int &, int &, int &, int &) : change the value like circular-shift-right [A, B,...
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
C language Modify the getAvg() function to support passing an array by reference. The parameters of...
C language Modify the getAvg() function to support passing an array by reference. The parameters of this function should be pointer to a float array, and the size of the array. Returns the average of the array. Build a function called getScoreFromUser() with the following properties: Asks the user to enter the score scans into a local float variable returns the float value that the user entered. Build and present a menu to a user like the following and enclose...
a) What does the nurse use in the evaluation for the parameters for dialysis? (i.e. electrolyte...
a) What does the nurse use in the evaluation for the parameters for dialysis? (i.e. electrolyte remover, Transmembrane pressure) Provide two specific, definable, and measureable evaluation parameters. b) Describe the documentation that is required during dialysis. Provide examples of the required documentation pieces during dialysis: patient condition pre, during, and post, and one example of machine documentation pre and post. Also include the process of de-accessing the site. c) Overtime, dialysis causes the heart to become sluggish. Describe how the...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters 2 ) ask the user for 2 numbers (should be in main) 3 ) Calculate the sum and product of the numbers using reference parameters (should be in getAnswer function) 4 ) Print the sum and product of the numbers in the following format: (should be in printmessage function) Here is the function you need        Function name | Parameters                       | Return Type    heading |...
What does the nurse use in the evaluation for the parameters for dialysis? (i.e. electrolyte remover,...
What does the nurse use in the evaluation for the parameters for dialysis? (i.e. electrolyte remover, Transmembrane pressure) Provide two specific, definable, and measurable evaluation parameters.
- A strip footing is founded on sand having strength parameters (Φ =40⁰ and c =...
- A strip footing is founded on sand having strength parameters (Φ =40⁰ and c = 0 kPa). Dry and saturated unit weights of the soil are 17.8 and 21 kN/m³ respectively. The foundation is designed to to carry a load of 4.8MN/m at a Factor of safety against general shear failure of 3. The footing depth is 1.5 m and the water table is 5 m from the surface. a) Determine the minimum footing width (m) to satisfy the...
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
We observe a random sample of n values from the beta distribution with parameters c and...
We observe a random sample of n values from the beta distribution with parameters c and 2. (a) Find a general expression for the method of moments (MOM) estimate of c, and calculate this MOM estimate for the case where we observe two values, 0.96 and 0.84. (b) Find the bias of the MOM estimator of c for the case where c = 1 and n = 1. (c) Find a general expression for the maximum likelihood estimate (MLE) of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT