Printing paths segment faulting
I wrote a problem to print all the paths from the top left corner to the
bottom left corner for a square matrix. The code is pasted below. For some
reason it is segment faulting. I use online C compiler so cannot debug and
also I don't see anything obviously wrong with the code. Can you guys have
a look and check?
#include <stdio.h>
#include <stdlib.h>
void PrintPossiblePath(int sizeX, int sizeY, int startX, int startY, int
**path, int count)
{
path[count][0] = startX;
path[count][1] = startY;
if((startX == sizeX) && (startY == sizeY))
{
int i;
for(i = 0; i <= count; i++)
printf(" (%d, %d) ", path[i][0], path[i][1]);
printf("\n");
return;
}
if(startX < sizeX)
PrintPossiblePath(sizeX, sizeY, startX + 1, startY, path, count + 1);
if(startY < sizeY)
PrintPossiblePath(sizeX, sizeY, startX, startY + 1, path, count + 1);
}
int main()
{
int matrix_size = 2;
int rows = matrix_size * 2;
int **paths;
paths = (int **)malloc(rows * sizeof(int *));
if(paths)
{
int i;
for(i = 0; i < rows; i++);
paths[i] = (int *)(malloc(2 * sizeof(int)));
}
// paths[3][0] = 10;
// paths[3][1] = 3;
// printf("\n Paths: %d,%d \n", paths[3][0],paths[3][1]);
PrintPossiblePath(matrix_size - 1, matrix_size - 1, 0, 0, paths, 0);
return 0;
}
No comments:
Post a Comment