先将方法告诉你:
a[n][n]
这是一个n*n的方阵
for(int i=0;i
s为第x行的和;
for(int i=0;i
s为第x列的和;
for(int j=0;j
s为对角线的和;
for(int j=0;j
s为次对角线的和;
下面是代码:
// #include"stdio.h"
// #include"stdlib.h"
// #include"time.h"
// #include"conio.h"
#include
#include
#include
int main()
{
int n,i,j,s;
int **a;
printf("请输入n值: ");
scanf("%d",&n);
fflush(stdin);
a=(int **)malloc(n*n*sizeof(int*));
for(i=0;i
a[i]=(int *)malloc(n*sizeof(int));
}
printf("请输入矩阵成员值: \n");
for(i=0;i
printf("第%d行: \n",i);
for(j=0;j
scanf("%d",&a[i][j]);
fflush(stdin);
}
}
printf("\n");
printf("输入的矩阵为: \n");
for(i=0;i
for(j=0;j
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i
s=0;
for(j=0;j
}
printf("\n");
for(i=0;i
s=0;
for(j=0;j
}
printf("\n");
s=0;
for(i=0;i
s+=a[i][i];
}
printf("主对角线元素之和为: %d\n",s);
printf("\n");
s=0;
for(i=0;i
s+=a[i][n-1-i];
}
printf("从对角线元素之和为: %d\n",s);
for(i=0;i
getch();
return 1;
}
//根本不用那么复杂,看你们累不累,你们做的题,都超出本章知识点了
#include
#define N 4
int main()
{int i,j,sum=0,sun=0,su=0,su1=0,arr[N][N]={{1,2,3,4},
{ 4,5,6,7},
{ 7,8,9,10},
{11,12,13,14},};
printf(" 0 ");
for(i=0;i
printf("%3d",arr[i][j]);
if(j==N-1)
{printf("%3d\n",sum);
printf(" 0 ");
sum=0;
}
}
printf("\b\b");
for(i=0,j=N-1;i
printf("%d",su1);
for(i=0;i
if(j==N-1)
{printf("%3d",sun);
sun=0;
}
}
for(i=0,j=0;i
printf("%3d",su);
return 0;
}