0

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.

Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

4 Answers4

1
void swap(double *x, double *y){
    double t = *x;
    *x = *y;
    *y = t;
}

void sort3(double *x, double *y, double *z){
    if(*x > *y)
        swap(x, y);
    if(*x > *z)
        swap(x, z);
    if(*y > *z)
        swap(y, z);
    printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

You are comparing pointers in your ifs. Change your comparison to use *x, *y, *z. Since they initially come from the stack(declared x,y,z in order), the order of the pointers value is always the same(already sorted).

However in your last case, you are swapping x and y 2 times so you end up not sorting.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
0

Not directly an answer to the original question, but a simpler way to implement sort3():

void
sort3 (int *a, int *b, int *c)
{
    int min, mid, max;

    if (*a <= *b) {
        if (*a <= *c) {
            min = *a;
            if (*b <= *c) {
                mid = *b;
                max = *c;
            }
            else {
                mid = *c;
                max = *b;
            }
        }
    }
    else
        sort3(b, a, c);

    *a = min;
    *b = mid;
    *c = max;
}

Here is the full code:

#include <stdio.h>
#include <stdlib.h>

void
sort3 (int *a, int *b, int *c)
{
    int min, mid, max;

    if (*a <= *b) {
        if (*a <= *c) {
            min = *a;
            if (*b <= *c) {
                mid = *b;
                max = *c;
            }
            else {
                mid = *c;
                max = *b;
            }
        }
    }
    else
        sort3(b, a, c);

    *a = min;
    *b = mid;
    *c = max;
}

void
sort_print(int a, int b, int c)
{
    printf("Before: %d, %d, %d\n", a, b, c);
    sort3(&a, &b, &c);
    printf("After:  %d, %d, %d\n", a, b, c);
}

int
main(void)
{
    sort_print(1, 2, 3);
    sort_print(1, 3, 2);
    sort_print(2, 1, 3);
    sort_print(2, 3, 1);
    sort_print(3, 1, 2);
    sort_print(3, 2, 1);
    exit(EXIT_SUCCESS);
}

And output:

$ ./a.out 
Before: 1, 2, 3
After:  1, 2, 3
Before: 1, 3, 2
After:  1, 2, 3
Before: 2, 1, 3
After:  1, 2, 3
Before: 2, 3, 1
After:  1, 2, 3
Before: 3, 1, 2
After:  1, 2, 3
Before: 3, 2, 1
After:  1, 2, 3
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
0

Are you looking for something like this

void d_swap(double *a, double *b)
{
   double tmp = *a;
   *a = *b;
   *b = tmp;
}

void sort3(double *x, double *y, double *z)
{
  if (*x > *y) {
    if (*x < *z) {
      if (*y > *z) {
        d_swap(y, z);
      }
    } else {
        d_swap (x,z);
        d_swap (y,z);
    }
  } else if (*y < *z) {
    if (*x < *z) {
        d_swap(x, y);
    } else {
        d_swap (x,y);
        d_swap (y,z);
    }
  } else {
        d_swap (x, z);
  }
  printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}

Not pretty but easy to understand :-p

Sundar
  • 469
  • 3
  • 8