Advertisement
Idanref

Max To Min With Pointers | C

Dec 7th, 2020
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void min_to_max(int *num1Ptr, int *num2Ptr, int *num3Ptr)
  4. {
  5.     // p1 - 6 >>> 9
  6.     // p2 - 9 >>> 6
  7.     // p3 - 1 >>> 1
  8.  
  9.     // [p1, p2, p3] >> [6, 9, 1]  >> [9, 6, 1]
  10.  
  11.     int arr[3] = {*num1Ptr, *num2Ptr, *num3Ptr};
  12.  
  13.     int max = arr[0];
  14.     int mid = arr[0];
  15.     int min = arr[0];
  16.  
  17.     for(int i = 0; i <= 2; i++)
  18.     {
  19.         if(max <= arr[i])
  20.             max = arr[i];
  21.  
  22.         if(min >= arr[i])
  23.             min = arr[i];
  24.     }
  25.  
  26.     for(int j = 0; j <= 2; j++)
  27.     {
  28.         if(arr[j] != max && arr[j] != min)
  29.                 mid = arr[j];
  30.     }
  31.  
  32.     *num1Ptr = max;
  33.     *num2Ptr = mid;
  34.     *num3Ptr = min;
  35. }
  36.  
  37. void main()
  38. {
  39.     int num1 = 1;
  40.     int num2 = 3;
  41.     int num3 = 9;
  42.  
  43.     min_to_max(&num1, &num2, &num3);
  44.  
  45.     printf("Num1: %d\n", num1);
  46.     printf("Num2: %d\n", num2);
  47.     printf("Num3: %d\n", num3);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement