C 语言实例 - 两个矩阵相加
C 语言教程
· 2019-02-21 07:28:30
使用多维数组将两个矩阵相加。
实例
#include <stdio.h>
int main(){
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("输入行数 ( 1 ~ 100): ");
scanf("%d", &r);
printf("输入列数 ( 1 ~ 100): ");
scanf("%d", &c);
printf("\n输入第一维数组的元素:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("输入元素 a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("输入第二维数组的元素:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("输入元素 a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
// 相加
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// 显示结果
printf("\n二维数组相加结果: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
return 0;
}
输出结果为:
输入行数 ( 1 ~ 100): 2 输入列数 ( 1 ~ 100): 3 输入第一维数组的元素: 输入元素 a11: 2 输入元素 a12: 3 输入元素 a13: 4 输入元素 a21: 5 输入元素 a22: 2 输入元素 a23: 3 输入第二维数组的元素: 输入元素 a11: -4 输入元素 a12: 5 输入元素 a13: 3 输入元素 a21: 5 输入元素 a22: 6 输入元素 a23: 3 二维数组相加结果: -2 8 7 10 8 6
点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html
Building Web Reputation Systems
Randy Farmer、Bryce Glass / Yahoo Press / 2010 / GBP 31.99
What do Amazon's product reviews, eBay's feedback score system, Slashdot's Karma System, and Xbox Live's Achievements have in common? They're all examples of successful reputation systems that enable ......一起来看看 《Building Web Reputation Systems》 这本书的介绍吧!