C++实现矩阵的相加/相称/转置/求鞍点

1.矩阵相加

两个同型矩阵做加法,就是对应的元素相加。

#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];//实现相加操作1 
			cout<<"\t"<<a[i][j];//输出矩阵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];//实现矩阵操作2 
			cout<<"\t"<<b[i][j];//输出矩阵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];//输出矩阵C 
		}
		cout<<endl;
	}
	cout<<endl; 
	return 0; 
	
} 

2.实现矩阵的转置

#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];//输出矩阵A 
	    	b[j][i]=a[i][j];//进行转置操作 
	    }
	    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; 
	
} 

3.实现矩阵的相乘

一个m行n列的矩阵可以和n列k行的矩阵相乘,得到一个m行k列的矩阵

#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];//输出矩阵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];//输出矩阵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];//实现相乘操作
		   }
		   cout<<"\t"<<c[i][j];//输出矩阵C 
		}
		cout<<endl;
	}
	cout<<endl; 
	return 0; 
	
} 

4.求矩阵中的鞍点

在矩阵中行中最大,列中最小的元素就是我们要求的鞍点

#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;
			}//先选出行中最大 
		}
		for(j=0;j<3;j++){
			if(a[i][k]>a[j][k]){
				tag=false;
			};//再选出列中最小 
		}
		cout<<endl;
		if(tag==true){
			cout<<"鞍点是第"<<(i+1)<<"行,第"<<(k+1)<<"列的"<<a[i][k]<<endl; 
			q++;
		}
	} 
	if(q==0){
		cout<<"没有一个鞍点~"<<endl;
	}
	cout<<endl;
	return 0; 
	
} 


posted @ 2013-07-13 16:36  千手宇智波  阅读(1028)  评论(0编辑  收藏  举报