Takes the addresses of three double variables as arguments and switch values within the three values. - C Pointer

C examples for Pointer:Pointer Variable

Introduction

  • moves the value of the smallest variable into the first variable,
  • moves the middle value to the second variable,
  • moves the largest value into the third variable.

Demo Code

#include <stdio.h>

void sort_variables(double *x, double *y, double *z);

int main(void)
{
  double x, y, z;

  printf("Enter three numbers x, y and z:\n");
  while(scanf("%lf %lf %lf", &x, &y, &z) == 3)
  {/*from  w w w .  ja  va  2s  . c  o m*/
    putchar('\n');
    printf("Before calling sort_variables:\n");
    printf("x = %f, y = %f, z = %f\n", x, y, z);

    sort_variables(&x, &y, &z);

    putchar('\n');
    printf("After calling sort_variables:\n");
    printf("x = %f, y = %f, z = %f\n", x, y, z);

    putchar('\n');

    printf("Enter three numbers x, y and z:\n");
  }

  return 0;
}

void sort_variables(double *x, double *y, double *z)
{
  double tmp;

  if (*x > *y)
  {
    // switch x and y
    tmp = *y;
    *y = *x;
    *x = tmp;
  }

  if (*y > *z)
  {
    // switch y and z
    tmp = *z;
    *z = *y;
    *y = tmp;

    if (*x > *y)
    {
      // switch x and y
      tmp = *y;
      *y = *x;
      *x = tmp;
    }
  }
}

Result


Related Tutorials