Advertisement
Lisaveta777

80% Reverse column with most zeros(комменты)

Dec 3rd, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #define ROWS 5
  3. #define COLS 5
  4.  
  5. //naiti col with most zeros, reverse that col
  6. //not happy about reverse_col. should use 1D array, tryed to do it, but was only able to use 2D array, not 1D.
  7. //have to figure out/learn syntaxis
  8. //в двумерном массиве найти колонку с наибольшим количеством 0, перевернуть эту колонку
  9. //решила, но плохо, что последняя функция берет аргументом не столбец, а 2Д массив - переделать!
  10.  
  11. void pop_arr(int r,int c,int a[r][c]);
  12. void pr_arr(int r,int c,int a[r][c]);
  13. int  col_zero(int r,int c,int a[r][c]);//finds row with most zeros
  14. void reverse_col(int r,int c,int a[r][c],int n);
  15.  
  16.  
  17. int main()
  18. {
  19.     int i,j,num,a[ROWS][COLS]={1,2,3,4,5,  6,7,0,8,9,  10,11,0,12,13,  14,15,0,16,17, 18,19,20,21,22};
  20.     //pop_arr(ROWS,COLS,a);
  21.     pr_arr(ROWS,COLS,a);
  22.     num= col_zero(ROWS,COLS,a);
  23.     printf("MOST zeros are in %d column, we will reverse it!\n",num);
  24.     reverse_col(ROWS,COLS,a,num);
  25.     pr_arr(ROWS,COLS,a);
  26.  
  27.     return 0;
  28. }
  29. void pop_arr(int r,int c,int a[r][c])
  30. {
  31.     int i,j;
  32.     for(i=0;i<r;i++)
  33.     {
  34.         for(j=0;j<c;j++)
  35.            a[i][j]= rand()%5-2;
  36.     }
  37. }
  38. void pr_arr(int r,int c,int a[r][c])
  39. {
  40.     int i,j;
  41.     for(i=0;i<r;i++)
  42.     {
  43.         for(j=0;j<c;j++)
  44.         printf("%d\t",a[i][j]);
  45.         printf("\n");
  46.     }
  47.     //printf("\n");
  48. }
  49.  
  50. int col_zero(int r,int c,int a[r][c])//all about zero counting in each column
  51. {
  52.     int i,j,current,total,index =0;
  53.     total = 0;
  54.  
  55.     for(j=0;j<c;j++)
  56.     {current = 0;//counts zeros in particular column
  57.         for(i=0;i<r;i++)
  58.         {
  59.             if(a[i][j]==0)
  60.                 current++;
  61.         }
  62.         if(current > total)
  63.             total = current,index=j;
  64.         //printf("current %d total %d index %d\n",current,total,index);
  65.     }
  66.  
  67.     return index;
  68.  
  69. }
  70. void reverse_col(int r,int c,int a[r][c],int n)
  71. {
  72.     int temp,i,j = 0;
  73.     for(i=0;i<r/2;i++)
  74.     {
  75.         temp = a[i][n];
  76.         a[i][n] = a[r-i-1][n];
  77.         a[r-i-1][n] = temp;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement