动态分配2d阵列 [英] Dynamically allocated 2d array
问题描述
我正在尝试创建一个2d数组,特别是有向图的邻接矩阵。
我从来没有尝试使用动态内存分配,之前我打了一个陷阱。
这是代码:
int n,i;
printf(节点数为:);
scanf(%d,& n);
int ** array = malloc(n * sizeof(int *));
for(i = 0; i array [i] = malloc(n * sizeof(int));
printf(边数为:);
scanf(%d,& m);
int x,y; (i = 0; i< m; i ++)
{
scanf(%d%d,& x,& y)
array [x] [y] = 1;
}
一旦完成了所有边缘的输入,程序将停止工作并抛出
编辑:houssam发现我的错误。第一个for应该从1到n。当我输入1 6作为边缘,程序崩溃,因为只有节点0-5。感谢您的发现。这样一个简单的错误.....
您可能输入错误的价值 x
或 y
,其值应小于 n
,大于或等于 0
:
#include< stdlib.h>
#include< stdio.h>
int main()
{
int m,n,i;
printf(节点数为:);
scanf(%d,& n);
int ** array = malloc(n * sizeof(int *));
for(i = 0; i array [i] = malloc(n * sizeof(int));
printf(边数为:);
scanf(%d,& m);
int x,y; (i = 0; i< m; i ++)
{
scanf(%d%d,& x,& y)
if(x> = 0&&&> = 0&& x< n&& y< n)//这里
array [x] ] = 1;
else
printf(你的值中的错误x =%d y =%d\\\
,x,y);
}
return 0;
}
编辑#1:
基于用户的评论: M Oehm ,以检查 scanf
,请参阅:
scanf失败为什么?,我可以重构代码如:
#include< stdlib.h>
#include< stdio.h>
int main()
{
int m,n,i;
printf(节点数为:);
scanf(%d,& n);
int ** array = malloc(n * sizeof(int *));
for(i = 0; i array [i] = malloc(n * sizeof(int));
printf(边数为:);
scanf(%d,& m);
int x,y; (i = 0; i< m; i ++)
{
// int num_read = scanf(%d%d,& x,& y)
if(scanf(%d%d,& x,& y)< 2)
{
printf(请输入有效的两个整数... \\ );
while(getchar()!='\\\
'); //读取输入流中的所有字符。
}
else if(x> = 0&&&> = 0&& x< n&&< n)// here
{
array [x] [y] = 1;
printf(array [%d] [%d] = 1; \\\
,x,y);
}
else
printf(你的值中的错误x =%d y =%d\\\
,x,y);
}
return 0;
}
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.....
you may entered wrong values for x
or y
, their values should be less than n
and greater than or equal to 0
:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int m ,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);
if (x >=0 && y >= 0 && x < n && y < n) // here
array[x][y]=1;
else
printf("error in your values x=%d y=%d\n" , x , y);
}
return 0;
}
EDIT #1:
based on comment from user : M Oehm to check the return value of scanf
, see:
scanf fails why? , I can refactor the code to be like:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int m ,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++)
{
//int num_read = scanf("%d %d", &x, &y);
if(scanf("%d %d", &x, &y) < 2)
{
printf("please enter valid two integers..\n");
while (getchar() != '\n'); // read all characters in the input stream.
}
else if (x >=0 && y >= 0 && x < n && y < n) // here
{
array[x][y]=1;
printf("array[%d][%d]=1;\n" , x, y);
}
else
printf("error in your values x=%d y=%d\n" , x , y);
}
return 0;
}
这篇关于动态分配2d阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!