dmilicev

different_size_of_matrix.c

Oct 2nd, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. /*
  2.  
  3.     different_size_of_matrix.c
  4.  
  5.     Input matrices of different sizes.
  6.  
  7.     We declare a large matrix M[MAX_SIZE][MAX_SIZE]
  8.     and use as many rows and columns as we need.
  9.  
  10.  
  11.     You can find all my C programs at Dragan Milicev's pastebin:
  12.  
  13.     https://pastebin.com/u/dmilicev
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18.  
  19. #define MAX_SIZE 1000
  20.  
  21. // Displays characters of the matrix M[r][c] that has r rows and c columns
  22. void print_matrix( char *text, char M[][MAX_SIZE], int r, int c )
  23. {
  24.     int i, j;
  25.  
  26.     printf("%s", text );
  27.  
  28.     for(i=0;i<r;i++) {
  29.         for(j=0;j<c;j++)
  30.             printf(" %c ", M[i][j] );
  31.  
  32.         printf("\n\n");
  33.     }
  34.     printf("\n");
  35. }
  36.  
  37. // set all matrix characters M[r][c] that has r rows and c columns to '\0';
  38. void set_all_matrix_elements_to_zero(char M[][MAX_SIZE], int r, int c )
  39. {
  40.     int i, j;
  41.  
  42.     for(i=0; i<MAX_SIZE; i++)
  43.         for(j=0; j<MAX_SIZE; j++)
  44.             M[i][j] = '\0';
  45. }
  46.  
  47. // input characters of matrix M[r][c] that has r rows and c columns
  48. void set_matrix_elements(char M[][MAX_SIZE], int r, int c )
  49. {
  50.     int i, j;
  51.     char ch = 'A';
  52.  
  53.     for(i=0; i<r; i++)
  54.         for(j=0; j<c; j++)
  55.         {
  56.             M[i][j] = ch++;
  57.             //printf(" M[%d][%d] = %c \n", i, j, M[i][j] );
  58.  
  59.             if( ch > 'Z' )      // reset fill pattern 'A' to 'Z'
  60.                 ch = 'A';
  61.         }
  62. }
  63.  
  64.  
  65. int main(void)
  66. {
  67.     int r, c;                   // number of rows r and columns c of matrix M[r][c]
  68.     char M[MAX_SIZE][MAX_SIZE]; // matrix of characters M[r][c]
  69.  
  70.     r = 3;
  71.     c = 5;
  72.     set_matrix_elements(M,r,c);
  73.     print_matrix("\n Matrix is: \n\n", M, r, c );
  74.  
  75.     r = 5;
  76.     c = 8;
  77.     set_matrix_elements(M,r,c);
  78.     print_matrix("\n Matrix is: \n\n", M, r, c );
  79.  
  80.     return 0;
  81.  
  82. } // main()
  83.  
Add Comment
Please, Sign In to add comment