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

所属分类: 软件编程 / C 语言 阅读数: 93
收藏 0 赞 0 分享

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;

}

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多