w3resource

C Exercises: Count all possible paths from top left to bottom right of a m X n matrix

C Array: Exercise-85 with Solution

Write a program in C to count all possible paths from top left to bottom right of a m X n matrix.

Sample Solution:

C Code:

#include <stdio.h>

int PathCounting(int m, int n)
{
    int ctr[m][n];
    for (int i = 0; i < m; i++)
        {
		ctr[i][0] = 1;
		}
    for (int j = 0; j < n; j++)
        {
		ctr[0][j] = 1;
		}
    for (int i = 1; i < m; i++)
    {
        for (int j = 1; j < n; j++)
            { 
			ctr[i][j] = ctr[i-1][j] + ctr[i][j-1];
			}
    }
    return ctr[m-1][n-1];
}
 
int main()
{
    int p,q;
    p=4;
    q=4;
	printf("The size of matrix is : %d, %d\n",p,q);
	printf("The all possible paths from top left to bottom right is: %d \n",PathCounting(p,q));
}

Sample Output:

The size of matrix is : 4 x 4
The all possible paths from top left to bottom right is: 20

Pictorial Presentation:

C Exercises: Count all possible paths from top left to bottom right of a m X n matrix

Flowchart:

Flowchart: Count all possible paths from top left to bottom right of a m X n matrix

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to find the minimum distance between two numbers in a given array.
Next: Write a program in C to find the equilibrium index of an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP