Question

In: Computer Science

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.

Solutions

Expert Solution

Code:

#include <iostream>
using namespace std;

void getinput(int &a,int &b,int &c){
  //get value of a,b,c from user
  cout<<"Enter value of a : ";
  cin>>a;

  cout<<"Enter value of b : ";
  cin>>b;

  cout<<"Enter value of c : ";
  cin>>c;
}
void swap(int &a,int &b,int &c)
{ 
    // Store sum of all in a 
    a = a + b + c;
    // After this, b has value of a 
    b = a - (b+c);
    // After this, c has value of b 
    c = a - (b+c);  
    // After this, a has value of c 
    a = a - (b+c);
} 

int main() {
  int a,b,c; 

  getinput(a,b,c);
  swap(a,b,c);

  //after swaping three variable
  printf("\n---After swap function called---\n\n");
  
  //print variable
  printf("Value of a : %d\n",a);
  printf("Value of b : %d\n",b);
  printf("Value of c : %d\n",c);
}

Output:


Related Solutions

c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
In C ++, Design and implement a program that reads from the user four integer values...
In C ++, Design and implement a program that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:    95, 80, 100, 70 Highest grade: 100 Lowest grade:   70 Average grade: 86.25
1. Make the flow diagram and the C ++ program that receives three integer values ​​R,...
1. Make the flow diagram and the C ++ program that receives three integer values ​​R, T and Q, determine if they satisfy the expression below, and if so, show the corresponding values ​​of R, T and Q. R'- T '+ 4 * Q? <820 2. Construct a flow chart and the corresponding program in C which, when receiving Y as data, calculates the result of the following function and prints the values ​​of X and Y.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values,...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the sentinel loop). The program should ignore any negative input and should continue to run. Hint: to remember/save entered (good) values, you can concatenate them into a string (separated by spaces) that you can output later on. Sample runs showing input prompts and...
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]...
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
Write a program that asks the user to type in two integer values. Test these two...
Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display the appropriate message to the terminal. Objective C
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT