①自定义矩阵阶数,函数间传递数组
main()
{
int i,j,n,s;
printf("请输入矩阵阶数:");
scanf("%d",&n);
int fun(int n,int a[][n]);
int a[n][n];
printf("请输入矩阵元素:\n");
for(i=0;i
s=fun(n,a);
printf("该矩阵主对角线和为:%d\n",s);
}
int fun(int n,int a[][n])
{
int s=0,i;
for(i=0;i
return s;
}
②自定义矩阵阶数,函数间传递指针
main()
{
int i,j,n,s;
printf("请输入矩阵阶数:");
scanf("%d",&n);
int fun(int n,int (*a)[n]);
int a[n][n];
printf("请输入矩阵元素:\n");
for(i=0;i
s=fun(n,a);
printf("该矩阵主对角线和为:%d\n",s);
}
int fun(int n,int (*a)[n])
{
int s=0,i;
for(i=0;i
return s;
}
③规定函数阶数,函数间传递数组
#define N 4
int fun(int a[][N]);
main()
{
int a[N][N],i,j,s;
printf("矩阵阶数:%d\n",N);
printf("请输入矩阵元素:\n");
for(i=0;i
s=fun(a);
printf("该矩阵主对角线和为:%d\n",s);
}
int fun(int a[][N])
{
int s=0,i;
for(i=0;i
return s;
}
④ 规定函数阶数,函数间传递指针
#define N 4
int fun(int (*a)[]);
main()
{
int a[N][N],i,j,s;
printf("矩阵阶数:%d\n",N);
printf("请输入矩阵元素:\n");
for(i=0;i
s=fun(a);
printf("该矩阵主对角线和为:%d\n",s);
}
int fun(int (*a)[N])
{
int s=0,i;
for(i=0;i
return s;
}
以4阶为例,运行结果为: