Question

In: Computer Science

write a C++ program that uses function swap (a,b) to enter two real numbers from the...

write a C++ program that uses function swap (a,b) to enter two real numbers from the keyboard and uses function min_max(a,b) to return a to be the smaller of the two numbers entered always.

Solutions

Expert Solution

Program in C++:

#include <iostream>
using namespace std;
void swap(int *a,int *b){// swap function 
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
int min_max(int a,int b){
    if(a>b){// if a is greater than b swap value of a and b, and return a(Smaller value).
        swap(&a,&b);
        return a;// return a 
    }
    else{// if a is Smaller then b return a
        return a;// return a
    }
}

int main()
{
    int a,b,min;
    cout<<"Enter the value of a"<<endl;
    cin>>a;// input a
    cout<<"Enter the value of b"<<endl;
    cin>>b; // input b
    min=min_max(a,b);// call min_max function
    cout<<"Smaller value is : "<<min;// print min value

    return 0;
}

Output 1:

Output 2:

Program Screenshot:

Summary:

Program print smaller of the two numbers and function min_max(a,b) always return value of a. So when value of a is greater then b then we swap the values of a and b using swap function.


Related Solutions

write a C++ program that uses function swap to enter three real value from the keyboard...
write a C++ program that uses function swap to enter three real value from the keyboard and outputs there in order from minimum to the maximum. For example 10,100,30 if were entered then the output would be 10,30,100. The program should use function get_data(a,b,c) and print_data(a,b,c)
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to...
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to lowest. I already have this code finding the maximum. Need to just add the sorting code onto it. int A, B, C; A = 4; B=3; C=7; Max = A If (B > max) { Max = B; } Else if (c > max) { Max = c; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT