1,判断n阶矩阵是否对称,对称返回1,不对称返回0
2,计算n阶矩阵上三角元素的和,并作为函数返回值
2,计算n阶矩阵两条对角线元素的和,并作为函数返回值
4,计算n阶矩阵的乘积,并作为函数返回值
#include<stdio.h>
#include<stdlib.h>
int Sym(int* p, int n);
int Func(int* p, int n);
int SymSum(int* p, int n);
int* Multiply(int* sum, int* p, int* q, int n);
void OutputMatrix(const int* p, int n);
int main() {
int a[4][4] = { {4,3,2,1},
{3,4,5,6},
{2,5,4,7},
{1,6,7,4} };
//int* pa = a[0];
int b[4][4] = { {2,7,8,6},
{4,2,7,7},
{2,1,3,5},
{5,0,9,2} };
//int* pb = b[0];
printf("矩阵a:\n");
OutputMatrix(a[0], 4);
printf("矩阵b:\n");
OutputMatrix(b[0], 4);
printf("矩阵a是否对称:%d\n", Sym(a[0], 4));
printf("矩阵b是否对称:%d\n", Sym(b[0], 4));
printf("矩阵a上三角元素和:%d\n", Func(a[0], 4));
printf("矩阵b上三角元素和:%d\n", Func(b[0], 4));
printf("矩阵a对角线元素和:%d\n", SymSum(a[0], 4));
printf("矩阵b对角线元素和:%d\n", SymSum(b[0], 4));
printf("矩阵a和b的乘积:\n");
int* sum;
sum = (int*)calloc(16, sizeof(int));//矩阵sum用来存储乘积
Multiply(sum, a[0], b[0], 4);
OutputMatrix(sum, 4);
free(sum);
return 0;
}
int Sym(int* p, int n) {
int i;
int j;
for (i = 1; i < n; i++)
for (j = 0; j < n; j++)
if (p[i * n + j] != p[j * n + i])
return 0;
return 1;
}
int Func(int* p, int n) {
int i;
int j;
int sum = 0;
for (i = 0; i < n; i++)
for (j = i; j < n; j++)
sum += p[i * n + j];
return sum;
}
int SymSum(int* p, int n) {
int i;
int j;
int sum = 0;
for (i = 0; i < n; i++)
for (j = i; j < n; j++)
if ((i == j) || ((i + j) == n))
sum += p[i * n + j];
return sum;
}
int* Multiply(int* sum, int* p, int* q, int n) {
int i;//sum元素的行索引
int j;//sum元素的列索引
int m;//用于改变q,p矩阵的相乘元素对
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (m = 0; m < n; m++)
sum[i * n + j] += p[i * n + m] * q[m * n + j];
}
void OutputMatrix(const int* p, int n) {
int i;
int j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%d\t", p[i * n + j]);
printf("\n");
}
}