Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could I please get help with this question: Assume that a function named swapdou

ID: 3529266 • Letter: C

Question

Could I please get help with this question: Assume that a function named swapdoubles has been defined and is available for use in this exercise: that function recieves two variables of type double and exchanges their values. Write the definition of a function named sort3 that is passed tjree 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;
}