Using C++ to realize matrix addition and proportionality and transpose and saddle point
- 2020-04-02 01:56:58
- OfStack
1. Matrix addition
If you add two homomorphic matrices, you just add the corresponding entries.
#include<iostream>
using namespace std;
int main(){
int a[3][3]={{1,2,3},{6,5,4},{4,3,2}};
int b[3][3]={{4,3,2},{6,5,4},{1,2,3}};
int c[3][3]={0,0,0,0,0,0,0,0,0};
int i,j;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]+=a[i][j];//Implement add operation 1
cout<<"t"<<a[i][j];//Output matrix A
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]+=b[i][j];//Implement matrix operation 2
cout<<"t"<<b[i][j];//Output matrix B
}
cout<<endl;
}
cout<<endl;
cout<<"Array C:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<"t"<<c[i][j];//Output matrix C
}
cout<<endl;
}
cout<<endl;
return 0;
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310210834492.jpg ">
2. Realize matrix transpose
#include<iostream>
using namespace std;
int main(){
int a[3][2]={{4,3},{6,5},{1,2}};
int b[2][3]={0,0,0,0,0,0};
int i,j;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<"t"<<a[i][j];//Output matrix A
b[j][i]=a[i][j];//Take the transpose
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"t"<<b[i][j];
}
cout<<endl;
}
cout<<endl;
return 0;
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310210834533.jpg "> 3. Multiply matrices
A matrix of m rows and n columns can be multiplied by a matrix of n rows and k rows to produce a matrix of m rows and k columns
#include<iostream>
using namespace std;
int main(){
int a[3][2]={{4,3},{6,5},{1,2}};
int b[2][3]={{1,2,3},{6,5,4}};
int c[3][3]={0,0,0,0,0,0,0,0,0};
int i,j,k,l;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<"t"<<a[i][j];//Output matrix A
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"t"<<b[i][j];//Output matrix B
}
cout<<endl;
}
cout<<endl;
cout<<"Array C:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<2;k++){
c[i][j]+=a[i][k]*b[k][j];//To multiply
}
cout<<"t"<<c[i][j];//Output matrix C
}
cout<<endl;
}
cout<<endl;
return 0;
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310210834534.jpg ">
4. Find the saddle points in the matrix
The largest element in the row, the smallest element in the column is the saddle point
#include<iostream>
using namespace std;
int main(){
int a[3][4]={{3,2,13,1},{8,7,10,5},{12,11,14,9}};
int i,j,k,ad,q=0;
bool tag;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
cout<<"t"<<a[i][j];
}
cout<<endl;
}
cout<<endl;
for(i=0;i<3;i++){
ad=a[i][0];
tag=true;
for(j=1;j<4;j++){
if(ad<a[i][j]){
k=j;
}//So let's pick the largest row
}
for(j=0;j<3;j++){
if(a[i][k]>a[j][k]){
tag=false;
};//I'm going to pick the smallest column
}
cout<<endl;
if(tag==true){
cout<<" The saddle point is the first "<<(i+1)<<" Ok, the first "<<(k+1)<<" The column "<<a[i][k]<<endl;
q++;
}
}
if(q==0){
cout<<" There is no saddle point ~"<<endl;
}
cout<<endl;
return 0;
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310210834535.jpg ">