为什么么结果出不来min的值啊?说Null pointer assignment.

来源:百度知道 编辑:UC知道 时间:2024/06/02 02:01:52
#include<stdio.h>
main()
{
int a[4][4];
int i,j;
int min=3000;
int x,y;
printf("Now there are 4 points:0,1,2,3.\n");
printf("You should Enter their paths'longth for each of them.\n");
for(i=0,j=0;i<4;i++,j++){
if(i==j)
a[i][j]=0;}
printf("Enter point 0 to other(1,2,3) points' path longth:\n");
for(i=0,j=1;j<4;j++){
scanf("%d",a[i][j]);
}
printf("Enter point 1 to other(2,3) points' path longth:\n");
for(i=1,j=2;j<4;j++){
scanf("%d",a[i][j]);
}
printf("Enter point 2 to other(3) points' path longth:\n");
for(i=2,j=3;j<4;j++){
scanf("%d",a[i][j]);
}

for(i=3,j=0;j<3;j++){
a[i][j]=a[j][i];
}
for(i=2,j=0;j<2;j++){
a[i][j]=a[j][i];

地址啊,大哥

scanf函数第二参数是地址啊,一个都没写对。

scanf("%d", &a[i][j]);
或者
scanf("%d", a+i+j);

最后的参数也够怪的,算了半天变量min没使用,最后输出的是a[i][j]?如果说最小值的话,应该是0吧?所有a[n][n]是不是全都是0啊?

1. 如楼上所说,scanf的问题

2. printf("The shortest path is: Point %d to Point %d %d",x,y,a[i][j]); 应输出a[x][y]。因为你用x,y记录了最短路径的端点;i,j只是用来遍历数组的变量

3. 程序优化:不用延对角线复制这个矩阵,用
for(i = 0; i < 4; i++) {
for(j = 3; j > i; j--) {
遍历这个矩阵的“上三角”即可

4. 未检测输入的负数