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

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?
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...
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
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...
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
in C++ You will overload a function named printArray. The function will assign values to each...
in C++ You will overload a function named printArray. The function will assign values to each element and print out a single dimension array of integers and an array of characters. The main body of the program will call each function. (6 points) Using the player switch portion of the tic tac toe game, create a function that does the switch. This is a practice in applying local variables, parameters, and global variables. (4 points)
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
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++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT