Advertisement
dmilicev

count_all_possible_paths_in_matrix.c

Nov 3rd, 2023 (edited)
541
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 1 0
  1. /*
  2.  
  3.     count_all_possible_paths_in_matrix.c
  4.  
  5. Count all possible paths from top left to bottom right of a r x c matrix.
  6.  
  7. https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-85.php
  8.  
  9. First, we set the matrix elements in the first column and first row to 1.
  10. Each internal element of the matrix can be reached from the top or from the left.
  11. That's why for each internal element we write the sum of its upper and its left element.
  12. Then the sum of all possible paths from top left to bottom right element is
  13. in the bottom right element.
  14.  
  15.     You can find all my C programs at Dragan Milicev's pastebin:
  16.  
  17.     https://pastebin.com/u/dmilicev
  18.  
  19. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #define MAX_SIZE 100    // maximum size of matrix M[100][100]
  24.  
  25. // Displays a matrix M[rows][columns] of integers with text before and after the matrix
  26. void PrintMatrix(char *text_before, int M[][MAX_SIZE], int rows, int columns, char *text_after){
  27.     printf("%s", text_before);
  28.     for(int r=0;r<rows;r++){
  29.         for(int c=0;c<columns;c++)
  30.             printf("%3d",M[r][c]);
  31.         printf("\n\n");
  32.     }
  33.     printf("%s", text_after);
  34. }
  35.  
  36. // Displays a matrix M[rows][columns] of integers
  37. void DisplayMatrix(int M[][MAX_SIZE], int rows, int columns){
  38.     for (int r = 0; r < rows; r++){
  39.         for (int c = 0; c < columns; c++)
  40.             printf("%3d",M[r][c]);
  41.         printf("\n\n");
  42.     }
  43. }
  44.  
  45. int PathCounting(int r, int c){
  46.     int M[MAX_SIZE][MAX_SIZE];
  47.  
  48. // First, we set the matrix elements in the first column and first row to 1.
  49.     printf("\n Assign the value 1 to the elements of the first column: \n");
  50.     printf("\n begin M[i][0] = 1; \n\n");
  51.     for (int i = 0; i < r; i++){
  52.         M[i][0] = 1;
  53.         printf("\t * i=%d , j=0, M[%d][0] = %d \n", i, i, M[i][0]);
  54.         PrintMatrix("\n",M,r,c,"\n");
  55.     }
  56.     printf(" end M[i][0] = 1; \n");
  57.  
  58.     printf("\n Assign the value 1 to the elements of the first row: \n");
  59.     printf("\n begin M[0][j] = 1; \n\n");
  60.     for (int j = 0; j < c; j++){
  61.         M[0][j] = 1;
  62.         printf("\t * i=0 , j=%d, M[0][%d] = %d \n", j, j, M[0][j]);
  63.         PrintMatrix("\n",M,r,c,"\n");
  64.     }
  65.     printf(" end M[0][j] = 1; \n");
  66.  
  67. // Each internal element of the matrix can be reached from the top or from the left.
  68. // That's why for each internal element we write the sum of its upper and its left element.
  69.     printf("\n Assign the values to the inner elements: \n");
  70.     printf("\n begin M[i][j] = M[i-1][j] + M[i][j-1]; \n\n");
  71.     for (int i = 1; i < r; i++)
  72.         for (int j = 1; j < c; j++){
  73.             M[i][j] = M[i-1][j] + M[i][j-1];
  74.  
  75.             printf("\t * i=%d , j=%d, M[%d][%d] = M[%d-1][%d] + M[%d][%d-1] = \n\t\t\t        M[%3d][%d] + M[%d][%3d] = %d + %d = %d \n",
  76.                     i, j, i, j, i, j, i, j,  i-1, j,  i, j-1,  M[i-1][j], M[i][j-1], M[i][j]);
  77.  
  78.             PrintMatrix("\n",M,r,c,"\n");
  79.         }
  80.     printf(" end M[i][j] = M[i-1][j] + M[i][j-1]; \n");
  81.  
  82. // Then the sum of all possible paths from top left to bottom right element is
  83. // in the bottom right element.
  84.     return M[r-1][c-1];
  85. }
  86.  
  87. int main(void)
  88. {
  89.     int r=4, c=4;   // Matrix of 4 rows and 4 columns.
  90.  
  91.     printf("\n\n The all possible paths of matrix %d x %d \n from top left to bottom right is: %d \n\n",
  92.             r, c, PathCounting(r,c) );
  93.  
  94.     return 0;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement