题目1:有两个矩阵a[3][2],b[2][2],元素值由键盘输入,计算a与b的矩阵之和(两个矩阵循环中相加,结尾求和)
#include<stdio.h>
int main()
{int arr[3][2], brr[2][2],i,j,sum1=0,sum2=0;for (i = 0; i < 3; i++){for (j = 0; j < 2; j++){scanf_s("%d", &arr[i][j]);//每次输入一个元素sum1+= arr[i][j];//就累加给sum1}printf("\n"); //每输入一行就换行}printf("\n");for (int i = 0; i < 2; i++){for (j = 0; j < 2; j++){scanf_s("%d", &brr[i][j]);//每次输入一个元素sum2 += brr[i][j];//就累加给sum2;}printf("\n");}printf("两个矩阵之和为:%d",sum1+sum2);//最后把两个矩阵所有的值累加输出就行了return 0;
}
题目2:有NxN矩阵,以对角线为对称线,对称元素相加并将结果存放在左下角三角元素中,右上角三角元素置为0,N=3
#include<stdio.h>
#define N 3
int main()
{int arr[N][N] = { {1,2,3},{4,5,6},{7,8,9} };int i, j;for (i = 0; i < N; i++){for (j = 0; j < N; j++){printf("%d ",arr[i][j]);}printf("\n");}for (i = 1; i < N; i++){for (j = 0; j < i; j++){arr[i][j] = arr[i][j] + arr[j][i];//将对称元素相加存放在左下三角元素中,左下三角元素可表示为arr[i][j]arr[j][i] = 0; /*将右上三角元素置0,右上三角元素可表示为arr[j][i]*/}}for (i = 0; i < N; i++){for (j = 0; j < N; j++){printf("%d ", arr[i][j]);}printf("\n");}return 0;
}