当前位置:网站首页>1. C language matrix addition and subtraction method
1. C language matrix addition and subtraction method
2022-07-06 13:26:00 【It's Wang Jiujiu】
Catalog
2. Function writing ( Array form and pointer form )
1. principle
The conditions that matrix addition needs to meet : Two added matrices need to keep the same dimension , Matrix A+B,A The line of 、 Column =B The line of 、 Column .
Principle of addition and subtraction : The principle of matrix addition is very simple , Add or subtract the corresponding position .
for example :
2. Function writing ( Array form and pointer form )
Because the addition and subtraction method is relatively simple , So just use two for Loop through the entire array , Add or subtract the corresponding bit , Here is an example of addition .
At the beginning of the function, you need to use assert Assert whether the condition holds , If not, the system will prompt an error ,assert Need to include header file <assert.h>, If there is no judgment , After passing in the wrong parameter , The access of two-dimensional array will overflow .
Because the array parameter passes the address , So the type of the function is set to void that will do .
Array form :
// Define the rows and columns of the matrix
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 3
#include<stdio.h>
#include<assert.h>
// Array form
void Matrix_addition(double arr1[][COL1], double arr2[][COL2], double arr3[][COL1],int row1, int col1,int row2,int col2)
{
assert(row1 == row2 && col1 == col2);// The dimension of the judgment matrix is consistent
int i = 0;
int j = 0;
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
arr3[i][j] = arr1[i][j] + arr2[i][j];
}
}
}
int main()
{
double arr1[ROW1][COL2] = {0};
double arr2[ROW1][COL2] = {9,8,7,6,5,4,3,2,1};
double arr3[ROW1][COL1] = { 0 };
Matrix_addition(arr1, arr2, arr3, ROW1, COL1,ROW2,COL2);
return 0;
}
Pointer form :
// Define the rows and columns of the matrix
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 3
#include<stdio.h>
#include<assert.h>
// Pointer form
void Matrix_addition(double (*arr1)[COL1], double (*arr2)[COL2], double (*arr3)[COL1], int row1, int col1, int row2, int col2)
{
assert(row1 == row2 && col1 == col2);// The dimension of the judgment matrix is consistent
int i = 0;
int j = 0;
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
*(*(arr3 + i) + j) = *(*(arr1 + i) + j) + *(*(arr2 + i) + j);
}
}
}
int main()
{
double arr1[ROW1][COL2] = {0};
double arr2[ROW1][COL2] = {9,8,7,6,5,4,3,2,1};
double arr3[ROW1][COL1] = { 0 };
Matrix_addition(arr1, arr2, arr3, ROW1, COL1,ROW2,COL2);
return 0;
}
Both forms can , There's no difference in nature , Use it according to your preferences . use define Define the rows and columns of the array , The advantage is flexibility , Easy to use , Subsequently, you only need to change the relevant parameters of the input matrix to perform the operation .
Matrix multiplication is slightly more difficult than addition ,C Linguistic matrix multiplication There is a detailed explanation of .
边栏推荐
- Introduction and use of redis
- [Topic terminator]
- MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
- 凡人修仙学指针-2
- 12 excel charts and arrays
- Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
- Smart classroom solution and mobile teaching concept description
- Redis介绍与使用
- 2.C语言初阶练习题(2)
- 面试必备:聊聊分布式锁的多种实现!
猜你喜欢
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
如何保障 MySQL 和 Redis 的数据一致性?
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
IPv6 experiment
6.函数的递归
Inheritance and polymorphism (Part 2)
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
View UI plus released version 1.3.0, adding space and $imagepreview components
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
随机推荐
Data manipulation language (DML)
There is always one of the eight computer operations that you can't learn programming
一文搞定 UDP 和 TCP 高频面试题!
Aurora system model of learning database
【话题终结者】
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
How do architects draw system architecture blueprints?
Rich Shenzhen people and renting Shenzhen people
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
如何保障 MySQL 和 Redis 的数据一致性?
更改VS主题及设置背景图片
分支语句和循环语句
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
TYUT太原理工大学2022数据库大题之概念模型设计
凡人修仙学指针-1
TYUT太原理工大学往年数据库简述题
View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L