Question

In: Computer Science

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] = > [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

Below is the swapping program in c# with given requirement. Added comments for each method for better understanding

using System;

namespace SwapNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            //default values given in the question are as below
            int num1 = 15;
            int num2 = 5;
            int num3 = 30;
            int num4 = 40;

            //call overloaded swap functions
            swap(num1, num2);
            swap(num1, num2, num3);
            swap(num1, num2, num3, num4);

            Console.ReadLine();
        }

        static void swap(int num1, int num2)
        {
            int temp;
            Console.WriteLine("Before swapping the numbers, num1 is {0} and num2 is {1}", num1, num2);
            temp = num1;
            num1 = num2;
            num2 = temp;
            Console.WriteLine("After swapping the numbers, num1 is {0} and num2 is {1}", num1, num2);
            Console.WriteLine("*********************************************************************");
        }

        //This will print number in [min, medium, max] order
        static void swap(int num1, int num2, int num3)
        {
            int min, medium, max;
            Console.WriteLine("Before swapping the numbers, num1 is {0}, num2 is {1} and num3 is {2}", num1, num2, num3);
            //Get samallest number among three numbers by calling smallestNumber method
            min = smallestNumber(num1, num2, num3);
            //Get medium number among three numbers by calling mediumNumber method
            medium = mediumNumber(num1, num2, num3);
            //Get larget number among three numbers by calling largestNumber method
            max = largestNumber(num1, num2, num3);
            Console.WriteLine("After swapping the numbers, num1 is {0}, num2 is {1} and num3 is {2}", min, medium, max);
            Console.WriteLine("*********************************************************************");
        }

        //This will print number in circular-shift-right [A, B, C, D] = > [B, C, D, A]
        static void swap(int num1, int num2, int num3, int num4)
        {
            int temp;
            Console.WriteLine("Before swapping the numbers, num1 is {0}, num2 is {1}, num3 is {2} and num4 is {3}", num1, num2, num3, num4);
            temp = num1;
            num1 = num2;
            num2 = num3;
            num3 = num4;
            num4 = temp;
            Console.WriteLine("After swapping the numbers, num1 is {0}, num2 is {1}, num3 is {2} and num4 is {3}", num1, num2, num3, num4);
            Console.WriteLine("*********************************************************************");
        }

        public static int smallestNumber(int num1, int num2, int num3)
        {
            int min = num1;
            if (num2 < min)
                min = num2;
            if (num3 < min)
                min = num3;
            return min;
        }

        public static int largestNumber(int num1, int num2, int num3)
        {
            int max = num1;
            if (num2 > max)
                max = num2;
            if (num3 > max)
                max = num3;
            return max;
        }

        public static int mediumNumber(int a, int b, int c)
        {
            if ((a < b && b < c) || (c < b && b < a))
                return b;
            else if ((b < a && a < c) || (c < a && a < b))
                return a;
            else
                return c;
        }
    }
}

Below is the output screenshot


Related Solutions

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,...
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?
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.
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
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use of your prior two functions to create a 2D array filled with a default parameter. var twoD = Init2D(<width>, <height>, <fill val>); Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(<min>, <max>); Challenge 5 – Random Int 2D Array Use your prior functions to provide a function...
Discuss the concept of parameters. What are parameters for? What is the difference between formal parameters...
Discuss the concept of parameters. What are parameters for? What is the difference between formal parameters and actual parameters? Give an example in Java code that illustrates formal parameters and actual parameters.
Create all necessary code to make this main function work. It is not allowed to change...
Create all necessary code to make this main function work. It is not allowed to change the main function. int main() {        int ListDataSample1[] = { 1, 1, 1 };        int ListDataSample2[] = { 2, 2, 2 };        List<int> List1 = List<int>(ListDataSample2, 3);        List<int> List2 = List<int>(ListDataSample2, 3);               cout << "List1 :" << List1 << endl;        cout << "List2 :" << List2 << endl << endl;        List1 += List2;               cout...
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
Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
- Design and implement a function with no input parameters. The function keeps receiving a number...
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT