Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- different_size_of_matrix.c
- Input matrices of different sizes.
- We declare a large matrix M[MAX_SIZE][MAX_SIZE]
- and use as many rows and columns as we need.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #define MAX_SIZE 1000
- // Displays characters of the matrix M[r][c] that has r rows and c columns
- void print_matrix( char *text, char M[][MAX_SIZE], int r, int c )
- {
- int i, j;
- printf("%s", text );
- for(i=0;i<r;i++) {
- for(j=0;j<c;j++)
- printf(" %c ", M[i][j] );
- printf("\n\n");
- }
- printf("\n");
- }
- // set all matrix characters M[r][c] that has r rows and c columns to '\0';
- void set_all_matrix_elements_to_zero(char M[][MAX_SIZE], int r, int c )
- {
- int i, j;
- for(i=0; i<MAX_SIZE; i++)
- for(j=0; j<MAX_SIZE; j++)
- M[i][j] = '\0';
- }
- // input characters of matrix M[r][c] that has r rows and c columns
- void set_matrix_elements(char M[][MAX_SIZE], int r, int c )
- {
- int i, j;
- char ch = 'A';
- for(i=0; i<r; i++)
- for(j=0; j<c; j++)
- {
- M[i][j] = ch++;
- //printf(" M[%d][%d] = %c \n", i, j, M[i][j] );
- if( ch > 'Z' ) // reset fill pattern 'A' to 'Z'
- ch = 'A';
- }
- }
- int main(void)
- {
- int r, c; // number of rows r and columns c of matrix M[r][c]
- char M[MAX_SIZE][MAX_SIZE]; // matrix of characters M[r][c]
- r = 3;
- c = 5;
- set_matrix_elements(M,r,c);
- print_matrix("\n Matrix is: \n\n", M, r, c );
- r = 5;
- c = 8;
- set_matrix_elements(M,r,c);
- print_matrix("\n Matrix is: \n\n", M, r, c );
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment