Advertisement
Lisaveta777

?How to pass row/col of 2D array into function

Dec 9th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #define ROWS 5
  3. #define COLS 10
  4. void pop_arr(int r,int c,int a[r][c]);
  5. void pr_arr(int r,int c,int a[r][c]);
  6. void manipulate_arr(int r,int c,int a[r][c]);
  7. int is_it_neven(int *val);
  8. //https://otvet.mail.ru/question/211934162
  9. //more logical, maybe, just to compare two neighboring rows by noneven elements
  10. //counter, and switch them, if order is wrong
  11. //i don't know syntaxys of passing row(out of 2D array)into function
  12. //if i to calculate neven elements in all rows instead, once i switch two
  13. //rows, that calulation stops making sense, as order will be different
  14. //otherwise i need different containers to keep in every row of 2D array
  15. //additional element, which contains counter of non even elements in row
  16. //THAT zadacha is quite misleading, it forces me to think in wrong direction first!
  17. //SOMETIME, LIKE 10 days ago i had the same dilemma, just left it as it was
  18. //and totally forgot about it, can;t even think about what was that zadacha i got
  19. //stuck with
  20.  
  21. //poka proga beret po 1 elementu stroki, proverjaet, esli on nechetnyj - summiruet!
  22. //xochu, htob brala vsju stroku, i odnim maxom proverjala!
  23.  
  24. int main()
  25. {
  26.     int i,j,k,l, arr[ROWS][COLS],counter[ROWS];
  27.     pop_arr(ROWS,COLS,arr);
  28.     pr_arr(ROWS,COLS,arr);
  29.  
  30.     if(is_it_neven(&arr[0][3]))
  31.         printf("it's even number!");
  32.  
  33.     //calculates how many non even numbers contains each row
  34.     for(i=0;i<ROWS;i++)
  35.     {
  36.         counter[i]=0;
  37.         for(j=0;j<COLS;j++)
  38.         {
  39.             if(is_it_neven(&arr[i][j]))//esli nechetnoe?
  40.                (counter[i])++;
  41.         }
  42.     }
  43.     //print how many non even numbers contains each row
  44.     for(i=0;i<ROWS;i++)
  45.     {
  46.         printf("%d st row contains %d non even numbers\n",i,counter[i]);
  47.     }
  48.  
  49.     return 0;
  50. }
  51. void pop_arr(int r,int c,int a[r][c])
  52. {
  53.     int i,j;
  54.     for(i=0;i<r;i++)
  55.     {
  56.         for(j=0;j<c;j++)
  57.         {
  58.             a[i][j]= rand()%20;
  59.         }
  60.     }
  61. }
  62. void pr_arr(int r,int c,int a[r][c])
  63. {
  64.     int i,j;
  65.     for(i=0;i<r;i++)
  66.     {
  67.         for(j=0;j<c;j++)
  68.         {
  69.             printf("%d\t",a[i][j]);
  70.         }
  71.         printf("\n");
  72.     }
  73. }
  74. int is_it_neven(int *val)
  75. {
  76.     return (*val%2)? 1:0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement