Advertisement
silentkiler029

Reverse a 2D string using function - ariful islam shanto

Jun 5th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. /*  BISMILLAHIR-RAHMANIR-RAHIM
  2.  ____________________________________
  3. |                                    |
  4. |       SHANTO_SUST_SWE-19_029       |
  5. |____________________________________|
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. #define debug 0
  15.  
  16. int r, c;
  17.  
  18. void reverse_array(int x[r][c], int **y, int r, int c)
  19. {
  20.     int i, j, k;
  21.     for(i = 0; i < r; i++){
  22.         for(j = 0, k = c - 1; j < c; j++, k--){
  23.             y[i][k] = x[i][j];
  24.         }
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     scanf("%d %d", &r, &c);
  31.     int ara[r][c];
  32.     int i, j;
  33.  
  34.     /// (Line no 36 & 37) :: used them for using random number
  35.  
  36.     time_t t;
  37.     srand((unsigned) time(&t));
  38.  
  39.     for(i = 0; i < r; i++){
  40.         for(j = 0; j < c; j++){
  41.             ara[i][j] = rand() % 10;    ///taking random number instead of scanning, you can custom your input by scanf()
  42.         }
  43.     }
  44.  
  45.     /// (Line no 47-53) - by these lines i have declared the memory needed for rev_ara
  46.  
  47.     int **rev_ara;
  48.  
  49.     rev_ara = (int **) malloc(sizeof(int) * r); //using this line, we are declaring how many rows to be needed
  50.  
  51.     for(i = 0; i < r; i++){
  52.         rev_ara[i] = (int *) malloc(sizeof(int) * c); // using this loop, we are declaring how many columns will be in every row
  53.     }
  54.  
  55.  
  56.     printf("Random Array:\n");
  57.  
  58.     for(i = 0; i < r; i++){
  59.         for(j = 0; j < c; j++){
  60.             printf("%d ", ara[i][j]);
  61.         }
  62.         printf("\n");
  63.     }
  64.  
  65.     printf("\n");
  66.  
  67.     reverse_array(ara, rev_ara, r, c);
  68.  
  69.     printf("Reversed Random Array:\n");
  70.  
  71.     for(i = 0; i < r; i++){
  72.         for(j = 0; j < c; j++){
  73.             printf("%d ", rev_ara[i][j]);
  74.         }
  75.         printf("\n");
  76.     }
  77.  
  78.  
  79.     return 0;
  80. }
  81.  
  82. //ALHAMDULILLAH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement