介绍
在本实验中,你将学习如何在 C 编程语言中执行矩阵加法和减法。该程序将要求用户输入两个相同大小的矩阵,然后执行加法和减法操作,并打印结果矩阵。
注意:你需要自己创建文件
~/project/main.c来练习编码,并学习如何使用 gcc 编译和运行它。
cd ~/project
## 创建 main.c
touch main.c
## 编译 main.c
gcc main.c -o main
## 运行 main
./main
在本实验中,你将学习如何在 C 编程语言中执行矩阵加法和减法。该程序将要求用户输入两个相同大小的矩阵,然后执行加法和减法操作,并打印结果矩阵。
注意:你需要自己创建文件
~/project/main.c来练习编码,并学习如何使用 gcc 编译和运行它。
cd ~/project
## 创建 main.c
touch main.c
## 编译 main.c
gcc main.c -o main
## 运行 main
./main
首先,通过包含必要的库并定义主函数来设置程序。
#include<stdio.h>
int main()
{
// 在此处添加代码
}
在主函数中声明所需的变量。我们需要 2 个矩阵,分别用于存储输入,以及 2 个矩阵用于存储加法和减法操作的结果。
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
n 和 m 是矩阵的行数和列数。
first 和 second 是输入的矩阵。
sum 和 diff 是分别用于存储加法和减法操作结果的矩阵。
使用 scanf() 函数向用户询问矩阵的行数和列数,然后要求用户输入矩阵的元素。
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++) // 遍历行
for(d = 0; d < n; d++) // 遍历列
scanf("%d", &first[c][d]);
printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++) // 遍历行
for(d = 0; d < n; d++) // 遍历列
scanf("%d", &second[c][d]);
使用 printf() 函数显示输入的矩阵。
/*
打印第一个矩阵
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // 遍历行
{
for(d = 0; d < n; d++) // 遍历列
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
/*
打印第二个矩阵
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // 遍历行
{
for(d = 0; d < n; d++) // 遍历列
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
将两个矩阵的对应元素相加,并将结果存储在 sum 矩阵中。
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
使用 printf() 函数显示 sum 矩阵。
// 打印 sum 矩阵的元素
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}
将两个矩阵的对应元素相减,并将结果存储在 diff 矩阵中。
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
使用 printf() 函数显示 diff 矩阵。
// 打印 diff 矩阵的元素
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}
以下是程序的最终代码:
#include<stdio.h>
int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++) // 遍历行
for(d = 0; d < n; d++) // 遍历列
scanf("%d", &first[c][d]);
printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++) // 遍历行
for(d = 0; d < n; d++) // 遍历列
scanf("%d", &second[c][d]);
/*
打印第一个矩阵
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // 遍历行
{
for(d = 0; d < n; d++) // 遍历列
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
/*
打印第二个矩阵
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // 遍历行
{
for(d = 0; d < n; d++) // 遍历列
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
/*
计算两个矩阵的和
并将结果存储在相同大小的 sum 矩阵中
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
// 打印 sum 矩阵的元素
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}
/*
计算两个矩阵的差
并将结果存储在相同大小的 diff 矩阵中
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
// 打印 diff 矩阵的元素
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}
return 0;
}
在本实验中,你学习了如何在 C 语言中实现矩阵的加法和减法操作。你创建了一个程序,要求用户输入两个相同大小的矩阵,然后执行加法和减法操作,并将结果显示在屏幕上。