Advertisement
Lisaveta777

0 Puts even in asc,neven in desc order

Dec 3rd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 10
  3. //PASTEBIN OR ALREADY THERE?
  4. //FIXED:SWAP PASSES THE WHOLE ARRAY, INSTEAD OF JUST 2 ELEMENTS.
  5. //FIGURE OUT:KAK OPISAT ARGUMENTY SWAP IN DECL, DEF, CALL? AND THE WHOLE OF
  6. //SWAP-BODY - WHAT'S GOING ON! I CAN CODE IT, BUT CAN'T EXPLAIN IT!
  7.  
  8. //https://otvet.mail.ru/question/211822869
  9.  
  10. void pop_arr(int,int *);
  11. void pr_arr(int,int *);
  12. void even_neven(int,int *);//puts all even els into first part of array, all non-even into second
  13. void sort(int,int *);//puts all even into ascending, all non-even into descending order
  14. //void swap_2_els(int,int *,int,int);//bad realisation, argument the whole array!
  15. void swap_2_els(int *,int *);
  16.  
  17. int main()
  18. {
  19.     int i,j,k,a[SIZE]={2,4,6,8,0,5,7,3,9,1};//{8,2,5,9,1,3,7,5,3,4};
  20.     pop_arr(SIZE,a);
  21.     pr_arr(SIZE,a);
  22.     even_neven(SIZE,a);
  23.     pr_arr(SIZE,a);
  24.     sort(SIZE,a);
  25.     pr_arr(SIZE,a);
  26.  
  27.     return 0;
  28. }
  29. void pop_arr(int s,int *a)
  30. {
  31.     int i;
  32.     for(i=0;i<s;i++)
  33.     {
  34.         a[i]= rand()%10;
  35.     }
  36. }
  37. void pr_arr(int s,int *a)
  38. {
  39.     int i;
  40.     for(i=0;i<s;i++)
  41.     {
  42.         printf("%d\t",a[i]);
  43.     }
  44. }
  45. void even_neven(int s,int *a)
  46. {
  47.     int i,j,temp;
  48.     for(i=0;i<s;i++)
  49.     {
  50.         if(a[i]%2)
  51.         {
  52.             for(j=0;j<s;j++)
  53.             {
  54.                 if( !(a[j]%2))//a[i]-even,a[j] -neven
  55.                 {
  56.                     swap_2_els(&a[i],&a[j]);//a[i] with a[j]
  57.                     j=s;
  58.                 }//end of if
  59.             }//end of for j
  60.  
  61.         }//end of if
  62.  
  63.     }
  64. }
  65. void sort(int s, int *a)//sorts first half in ascending, second-descending order
  66. {
  67.     int i,j,start,temp;
  68.     for(i=0;i<s;i++)//finds start-index of first neven number
  69.     {
  70.         if(!(a[i]%2))
  71.             start=i,i=s;
  72.     }
  73.  
  74.     for(i=0;i<s;i++)
  75.     {
  76.         for(j=i;j<s;j++)
  77.         {
  78.             if( (a[i]>a[j]&&i<start-1&& j<start) ||
  79.                 (a[i]<a[j]&&i>=start-1&&j>=start))
  80.                 swap_2_els(&a[i],&a[j]);
  81.         }
  82.     }
  83.  
  84.     /*for(i=0;i<start-1;i++)//first half of array, sort in ascending
  85.     {
  86.         for(j=i;j<start;j++)
  87.         {
  88.             if( a[i]>a[j])//
  89.             {
  90.                 swap_2_els(a[i],a[j]);
  91.             }
  92.         }
  93.     }*/
  94.     /*for(i=start;i<s-1;i++)//second half of array, sort in descending
  95.     {
  96.         for(j=i;j<s;j++)
  97.         {
  98.             if( a[i]<a[j] )//second part of array
  99.             {
  100.                 swap_2_els(a[i],a[j]);
  101.             }
  102.         }
  103.     }*/
  104. }
  105. void swap_2_els(int *el1,int *el2)//how
  106. {
  107.     int temp = *el1;
  108.     *el1 = *el2;
  109.     *el2 = temp;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement