Question

In: Computer Science

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, C, D] = > [B, C, D, A]
    • For Example,
      • num1 = 15; num2 = 5; num3 = 30; num4 =40;
      • swap(num1, num2);
        • num1 will be 5 and num2 will be 15
      • swap(num1, num2, num3)
        • num1 will be 5, num2 will be 15, and num3 is 30
      • swap(num1, num2, num3, num4)
        • num1 is 5, num2 is 30, num3 is 40 and num4 is 15

Solutions

Expert Solution

#include <iostream>
using namespace std;
void swap(int &a, int &b) {
// Switching the given variables with the help of a temporary variable.
    int temp = a;
    a = b;
    b = temp;
}
void swap(int &a, int &b, int &c) {
    int mini, medi, maxi;

    // Finding the smallest, intermediate and largest element.
    if (a > b && a > c) {
        maxi = a;    // A will be the largest number.
// The ternary operator first checks the condition
// If the condition is true, the code before the colon (:) gets into the consideration
        medi = (b>c ? b : c);   // Since a is already the largest, if b > c, then b will be the second largest
        mini = (b > c ? c : b); // Similarly this operates
    }
// Similarly the below cases operate.
    else if (b > c) {
        maxi = b;
        medi = (a > c ? a : c);
        mini = (a > c ? c : a);
    }
    else {
        maxi = c;
        medi = (a > b ? a : b);
        mini = (a > b ? b : a);
    }
    a = mini;
    b = medi;
    c = maxi;
}

void swap(int &a, int &b, int &c, int &d) {
   // Switching the elements one by one.
    int temp = a;
    a = b;
    b = c;
    c = d;
    d = temp;
   
}

int main() {
   // Call the main as per the requirement.
}

The above program has three overloading swap functions, first one takes two parameters , second takes three parameters and third takes four parameters.


Related Solutions

Make a function that swaps the values that change the values between the parameters as the...
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, C, D]...
Which ways can you change the values of parameters inside the function in C programming?
Which ways can you change the values of parameters inside the function in C programming?
Create a void function named swap that accepts two int parameters and swaps their values. Provide...
Create a void function named swap that accepts two int parameters and swaps their values. Provide a main function demonstrating a call to the swap function. Document the dataflow of the function.
Make a program that swaps the three values. Three integer values are taken from the user...
Make a program that swaps the three values. Three integer values are taken from the user input. This work will be done by calling the function "getinput(num1, num2, num3)" The input values are swapped each other by calling the function swap(num1, num2, num3) example: values (a, b, c) will be (c, a, b) after the swap. a = 10; b = 20; c = 30; after function call, a has 30, b has 10, and c has 20.
C++ Write the definition of a function minMax that has five parameters. The first three parameters...
C++ Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */ **ONLY THE FUNCTION
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
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...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything
When are values passed as parameters? a. When parameters are primitives or wrappers b. When parameters...
When are values passed as parameters? a. When parameters are primitives or wrappers b. When parameters are user-defined objects c. When parameters are objects What is the result of a == b? public static void main(String[] args) { int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; } a. True b. False c. Compile Error
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