I am trying to make a program that orders three double numbers by increasing value using pointers.
I am able to print the double values; however, they're not in the right order for most orders.
This is what I have:
#include <stdio.h>
void sort3(double *x, double *y, double *z);
int main()
{
double x,y,z;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &x,&y,&z);
sort3(&x,&y,&z);
return 0;
}
void sort3(double *x, double *y, double *z)
{
double temp, temp2, temp3, temp4, temp5, temp6, temp7;
if (y<x && y<z && z>x) // MSL
{
temp = *x;
*x = *y;
*y = temp;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if (z<x && (x>y) && (y>z)){ // LMS
temp2 = *z;
*z = *x;
*x = temp2;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(z>y && y<x && x>z ) { // LSM
temp3 = *z;
*z = *x;
*x = temp3;
temp4 = *x;
*x = *y;
*y = temp4;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(z>x && y>z && y>x ) { // SLM
temp5 = *z;
*z = *y;
*y = temp5;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(x>z && y>z && y>x ){ // MLS
temp6 = *x;
*x = *y;
*y = temp6;
temp7 = *y;
*y = *x;
*x = temp7;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else{
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
} //SML
}
I am not sure where the problems are and how and how to fix them.