Write a C program that includes a function called moveEm tha
Write a C program that includes a function called moveEm() that accepts the ADDRESSES of three double variables as input. The function should move the value of the smallest variable into the first variable, the middle value into the middle variable, and the largest value into the third variable. Call your function from within your program using the arguments 7.8, 3.5, and 1.1 in order to demonstrate that it works (make sure your logic works for any order of largest, smallest, middle however). Comment your code, make sure it looks professional, and use meaningful variable names. (NOTE: This function does something similar to swap3.c--I recommend using it as a model.) For example: if I had three double variables as follows: x=7.8, y=3.5, z=1.1 then I would call moveEm like so: moveEm(&x, &y, &z); After moveEm() returned x would contain 1.1, y would contain 3.5, and z would contain 7.8.
Solution
// C code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// function to move the value of the smallest variable into the first variable, the middle value
// into the middle variable, and the largest value into the third variable
void moveEm(double *a, double *b, double *c)
{
int temp;
// case 1
if(*a > *b && *a > *c && *b > *c)
{
temp = *a;
*a = *c;
*c = temp;
}
//case 2
else if(*a > *b && *a > *c && *b < *c)
{
temp = *a;
*a = *b;
*b = *c;
*c = temp;
}
// case 3
else if(*a < *b && *a < *c && *b > *c)
{
temp = *b;
*b = *c;
*c = temp;
}
// case 4
else if(*a > *b && *a < *c && *b < *c)
{
temp = *a;
*a = *b;
*b = temp;
}
// case 5
else if(*a < *b && *a > *c && *b > *c)
{
temp = *a;
*a = *c;
*c = *b;
*b = temp;
}
}
int main()
{
double a,b,c,d;
printf(\"Enter value of a: \");
scanf(\"%lf\",&a);
printf(\"Enter value of b: \");
scanf(\"%lf\",&b);
printf(\"Enter value of c: \");
scanf(\"%lf\",&c);
printf(\"\ \");
moveEm(&a,&b,&c);
printf(\"After swapping:\ \");
printf(\"a = %lf\ \",a);
printf(\"b = %lf\ \",b);
printf(\"c = %lf\ \",c);
return 0;
}
/*
output:
Enter value of a: 2
Enter value of b: 4
Enter value of c: 3
After swapping:
a = 2.000000
b = 3.000000
c = 4.000000
Enter value of a: 4
Enter value of b: 1
Enter value of c: 2
After swapping:
a = 1.000000
b = 2.000000
c = 4.000000
Enter value of a: 4
Enter value of b: 2
Enter value of c: 1
After swapping:
a = 1.000000
b = 2.000000
c = 4.000000
Enter value of a: 1
Enter value of b: 4
Enter value of c: 3
After swapping:
a = 1.000000
b = 3.000000
c = 4.000000
Enter value of a: 1
Enter value of b: 3
Enter value of c: 4
After swapping:
a = 1.000000
b = 3.000000
c = 4.000000
Enter value of a: 7.8
Enter value of b: 3.5
Enter value of c: 1.1
After swapping:
a = 1.100000
b = 3.500000
c = 7.000000
*/


