I'm trying to create a 2d array, specifically an adjacency matrix for directed graphs. I've never tried this with dynamic memory allocation before and I've hit a snag. Here's the code:
int n, i;
printf("Number of nodes is: ");
scanf("%d", &n);
int ** array = malloc(n * sizeof(int*));
for(i = 0; i < n; i++)
array[i] = malloc(n * sizeof(int));
printf("Number of edges is: ");
scanf("%d", &m);
int x, y;
for(i=0;i<m;i++)
{
scanf("%d %d", &x, &y);
array[x][y]=1;
}
As soon as I finish entering all the edges, the program stops working and throws the usual "exe has stopped working".
Where's the problem?
EDIT: houssam spotted my error. The first "for" should have gone from 1 to n. When I entered 1 6 as an edge, the program crashed because there were only nodes 0-5. Thanks for spotting that. Such a simple mistake.....