Assume that a function named swapdoubles has been defined and is available for u
ID: 3653826 • Letter: A
Question
Assume that a function named swapdoubles has been defined and is available for use in this exercise: that function receives two variables of type double and exchanges their values. Write the definition of a function named sort3 that is passed three double variables. The function returns nothing but modifies the values of these variables so they are in sorted order. So, if a , b and c have (respectively) the values 3.14, 2.71, and 3.04, and the invocation sort3(a,b,c) is made, then upon return, the values of a , b and c will be 2.71, 3.04, and 3.14 respectively.Explanation / Answer
#include<iostream>
using namespace std;
void swapdoubles(double* a , double* b)
{
double temp = *a;
*a = *b;
*b = temp;
}
void sort3(double *a, double* b, double *c)
{
if( *b < *a )
swapdoubles(a,b);
if( *c < *b )
swapdoubles(b,c);
if( *b < *a )
swapdoubles(a,b);
}
int main()
{
double a = 3.14,b=2.71,c = 3.04;
cout << " before sorting" <<endl;
cout << a << " " << b << " " << c << endl;
sort3(&a,&b,&c);
cout << " after sorting" <<endl;
cout << a << " " << b << " " << c << endl;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.