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 asks the user to enter any two intger numbers and each...
write a c program that asks the user to enter any two intger numbers and each number consist of four digits. your program should check whether the numbers are four digits or not and in case they are not a four digit number the program should print a message and exit otherwise it should do the following print a mnew as follows    select what you want to do with the number 1-3: 1-print grreatest common divisor (gcd)of the two...
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 java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT