waltermilner

Untitled

Mar 28th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. void sort(int array[], const int LENGTH)
  5. {
  6.     for (int i=0; i<LENGTH; i++)
  7.         for (int j=0; j<LENGTH-1; j++)
  8.         {
  9.             if (array[j]>array[j+1])
  10.             {
  11.                 int temp=array[j];
  12.                 array[j]=array[j+1];
  13.                 array[j+1]=temp;
  14.             }
  15.         }
  16.     return;
  17. }
  18.  
  19.  
  20. int main()
  21. {
  22.     int data[]= {9,8,7,10};
  23.     const int LENGTH=4;
  24.     sort(data, LENGTH);
  25.     for (int i=0; i<LENGTH; i++)
  26.         printf("%d %d\n",i, data[i]);
  27.     if (LENGTH%2) // length is odd
  28.         printf("%d\n", data[LENGTH/2]);
  29.     else // length is even - take average of middle two
  30.         printf("%f\n", (data[LENGTH/2-1]+data[LENGTH/2])/2.0);
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment