Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 10
- //PASTEBIN OR ALREADY THERE?
- //FIXED:SWAP PASSES THE WHOLE ARRAY, INSTEAD OF JUST 2 ELEMENTS.
- //FIGURE OUT:KAK OPISAT ARGUMENTY SWAP IN DECL, DEF, CALL? AND THE WHOLE OF
- //SWAP-BODY - WHAT'S GOING ON! I CAN CODE IT, BUT CAN'T EXPLAIN IT!
- //https://otvet.mail.ru/question/211822869
- void pop_arr(int,int *);
- void pr_arr(int,int *);
- void even_neven(int,int *);//puts all even els into first part of array, all non-even into second
- void sort(int,int *);//puts all even into ascending, all non-even into descending order
- //void swap_2_els(int,int *,int,int);//bad realisation, argument the whole array!
- void swap_2_els(int *,int *);
- int main()
- {
- 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};
- pop_arr(SIZE,a);
- pr_arr(SIZE,a);
- even_neven(SIZE,a);
- pr_arr(SIZE,a);
- sort(SIZE,a);
- pr_arr(SIZE,a);
- return 0;
- }
- void pop_arr(int s,int *a)
- {
- int i;
- for(i=0;i<s;i++)
- {
- a[i]= rand()%10;
- }
- }
- void pr_arr(int s,int *a)
- {
- int i;
- for(i=0;i<s;i++)
- {
- printf("%d\t",a[i]);
- }
- }
- void even_neven(int s,int *a)
- {
- int i,j,temp;
- for(i=0;i<s;i++)
- {
- if(a[i]%2)
- {
- for(j=0;j<s;j++)
- {
- if( !(a[j]%2))//a[i]-even,a[j] -neven
- {
- swap_2_els(&a[i],&a[j]);//a[i] with a[j]
- j=s;
- }//end of if
- }//end of for j
- }//end of if
- }
- }
- void sort(int s, int *a)//sorts first half in ascending, second-descending order
- {
- int i,j,start,temp;
- for(i=0;i<s;i++)//finds start-index of first neven number
- {
- if(!(a[i]%2))
- start=i,i=s;
- }
- for(i=0;i<s;i++)
- {
- for(j=i;j<s;j++)
- {
- if( (a[i]>a[j]&&i<start-1&& j<start) ||
- (a[i]<a[j]&&i>=start-1&&j>=start))
- swap_2_els(&a[i],&a[j]);
- }
- }
- /*for(i=0;i<start-1;i++)//first half of array, sort in ascending
- {
- for(j=i;j<start;j++)
- {
- if( a[i]>a[j])//
- {
- swap_2_els(a[i],a[j]);
- }
- }
- }*/
- /*for(i=start;i<s-1;i++)//second half of array, sort in descending
- {
- for(j=i;j<s;j++)
- {
- if( a[i]<a[j] )//second part of array
- {
- swap_2_els(a[i],a[j]);
- }
- }
- }*/
- }
- void swap_2_els(int *el1,int *el2)//how
- {
- int temp = *el1;
- *el1 = *el2;
- *el2 = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement