Advertisement
Lisaveta777

?to do in one step

Nov 10th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define SIZE 16
  4. //самый большой эл массива - на 1 место, чуть менее - на посл, чуть менее - на 2,
  5. //чуть менее - на предпоследнее...
  6. //пока делаю промежуточную задачу, все равно не получается
  7. void pop_arr(int s,int *a);
  8. void small_big_arr(int s,int *a);
  9.  
  10. int main()
  11. {
  12.     int arr[SIZE]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  13.     //int arr[SIZE] = {2,4,6,8,10,12,14,16,15,13,11,9,7,5,3,1};
  14.     pr_arr(SIZE,arr);
  15.     //small_big_arr(SIZE,arr);
  16.     sort_arr(SIZE,arr);
  17.     pr_arr(SIZE,arr);
  18.  
  19.     return 0;
  20.  
  21. }
  22. void pop_arr(int s,int *a)
  23. {
  24.     int i,j;
  25.     for(i=0;i<s;i++)
  26.     {
  27.             a[i]= rand()%10;
  28.  
  29.     }
  30. }
  31. void pr_arr(int s,int *a)
  32. {
  33.     int i,j;
  34.     for(i=0;i<s;i++)
  35.     {
  36.         printf("%d\t",a[i]);
  37.  
  38.     }
  39.     printf("\n\n");
  40. }
  41. void sort_arr(int s,int *a)
  42. {
  43.     int i,j,temp_index,target_index,max_index,counter = 0;
  44.     target_index=0;
  45.     for(i=0;i<s;i++)
  46.  
  47.     {
  48.         printf("from index %d,to %d\n",i,target_index);
  49.  
  50.         //target_index = (i%2)? i/2+1: s - i/2-1;
  51.         target_index = (!(i%2))?  s - i/2-1:i/2+1;
  52.         max_index = s-i;
  53.  
  54.         for(j=i;j<s-i;j++)//find max_index for specific i
  55.         {
  56.             if(a[j]>a[max_index])
  57.             {
  58.                 max_index = j;
  59.             }
  60.             //swap a[max_index] & a[target_index]
  61.             {
  62.                 a[temp_index] = a[max_index];
  63.                 a[max_index] = a[target_index];
  64.                 a[target_index] = a[temp_index];
  65.  
  66.             }
  67.         }//end for j
  68.     }
  69. }
  70. void small_big_arr(int s,int *a)
  71. {
  72.     int i,j,temp;
  73.     for(i=0;i<s-1;i++)
  74.     {
  75.         for(j=i+1;j<s;j++)
  76.         {
  77.             if(a[i]>a[j])
  78.             {
  79.                 temp = a[i];
  80.                 a[i] = a[j];
  81.                 a[j] = temp;
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement