高分求答案

The function fun(int**,int) below is called by main() to produce a

matrix according to the input number m(2<=m<=0).Please study examples

below and correct the error in the program.

Note: main() is not allowed to be madified.And do not add or remove any
line of the program.

Example of expected Input and Output:

input is :2 | If input is :4
Output is: | Output is:
1 2 | 1 2 3 4
2 4 | 2 4 6 8
| 3 6 9 12
| 4 8 12 16

#include<stdio.h>
#include<conio.h>
#define M 10
int a[M][M]={0};

fun(int**a,int m)
{ int j,k;
for(j=0;j<m;j++)
a[j][k]=k*j;
}
main()
{int i,j,n;
printf("Enter n:");scanf("%d",&n);
fun(a,n);
for(i=0;i<n;i++)
{ for j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}

找错误

#include<stdio.h>
#include<conio.h>
#define M 10
int a[M][M]={0};

fun(int m)
{ int j,k;
for(j=0;j<m;j++)
for (k = 0; k < m; k++)
a[j][k]=(k + 1)* (j + 1);
}
main()
{int i,j,n;
printf("Enter n:");scanf("%d",&n);
fun(n);
for(i=0;i<n;i++)
{ for (j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-09-12
错误有三

其一:

第6行的函数定义有问题,此处不应该用**a,它并不等价于a[][],改为如下形式为好

void fun(int a[][M],int m)

由于数组a定义成了全局变量 ,所以此处也可以去掉int a[][M]而变成

void fun(int m)

其二:

第9行,此处缺少一个循环,因此k值无法递变,应改为:

for (k=0; k<m; k++)
a[j][k]=(k+1)*(j+1);

其三:
第16行,此处缺少一个小括号,改为:

{ for (j=0;j<n;j++)

修改后的完整程序为:

////////////////////////////////////////////////

#include<stdio.h>
#include<conio.h>
#define M 10
int a[M][M]={0};

void fun(int a[][M],int m)
{ int j,k;
for(j=0;j<m;j++)
for (k=0; k<m; k++)
a[j][k]=(k+1)*(j+1);
}
main()
{int i,j,n;
printf("Enter n:");scanf("%d",&n);
fun(a,n);
for(i=0;i<n;i++)
{ for (j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
相似回答