C/C++ 矩阵乘法 简单 难 两种

文章目录

        • 矩阵乘法
          • demo
          • 结构体
          • 结构体初始化
          • 矩阵check
          • 矩阵相乘
          • 总代码

矩阵乘法

三个for就可以,来个简单的demo

demo
//this is a demo
int task(){
        char first_matrix[10]="Imissyou!";
        int second_matrix[9]={1,0,1,0,1,1,0,1,0};
        int third_matrix[9]={0};
        int tmp;
        for(int i=0;i<3;i++){
                for (int j=0;j<3;j++){
                        tmp=0;
                        for(int k=0;k<3;k++){
                                tmp+=first_matrix[3*i+k]*second_matrix[3*k+j];
                        }
                        third_matrix[3*i+j]=tmp;
                }
        }
        for(int i=0;i<9;i++){
                cout<

想要简单实现的,可以参考这个demo,之后要强化的化,可以从demo改进,也可参考我下面的
之后尝试做个大的,尽量自动化,所有的矩阵
先定义个结构体

结构体
typedef struct Matrix{
        int rowlen;
        int columnlen;
        int value[101];//max num 10
}Matrix;

这个最大能装10介矩阵
多个1保险,也可以不要

其实很简单的,自己写个if就足够了

结构体初始化

这个可要可不要,你可以自己在的别的地方写赋值,不必要专门写一个初始化的函数
我这个,考虑的比较多,但是没设计的很好,可以参考一下

void Initialization (Matrix &j,int k[],int object=1){
        /*
        j is the matrix you want to Initialization.
        k is the list what you put in j.value . usually first_matrix and second_matrix should use it
        object usually you can ignore it. but if you hope j.value all 0,you can put first_matrix or second_matrix in k
        than put object = 0 ,then we will put k value to 0
        */
        cout<<"Enter rowlen and columnlen of matrix"<>j.rowlen>>j.columnlen;

        //j.rowlen=j.columnlen=3;
        if (object!=1){
                for(int i=0;i
矩阵check

既然是矩阵乘法,要满足矩阵乘法的规则,写一个check

bool check_matrix_multiplication (Matrix first ,Matrix second, Matrix third){
        /* check if first_matrix's row length equal second_matrix's column length
           check if third_matrix's row length equal second_matrix's row length and third_matrix's column length equiv
           first_matrix's column length */

        if (first.rowlen != second.columnlen) return false;
        if (first.columnlen != third.columnlen || second.rowlen != third.rowlen) return false;

        return true;
}
矩阵相乘
void Matrix_multiplication(Matrix &first,Matrix &second,Matrix &third){
        //first_matrix in left,second in right
        if (check_matrix_multiplication(first,second,third)!=true){
                cout<<"Matrix not allow"<
总代码

包括了demo的代码

#include 
using namespace std;

typedef struct Matrix{
        int rowlen;
        int columnlen;
        int value[101];//max num 10
}Matrix;
bool check_matrix_multiplication (Matrix first ,Matrix second, Matrix third){
        /* check if first_matrix's row length equal second_matrix's column length
           check if third_matrix's row length equal second_matrix's row length and third_matrix's column length equiv
           first_matrix's column length */

        if (first.rowlen != second.columnlen) return false;
        if (first.columnlen != third.columnlen || second.rowlen != third.rowlen) return false;

        return true;
}

void Matrix_multiplication(Matrix &first,Matrix &second,Matrix &third){
        //first_matrix in left,second in right
        if (check_matrix_multiplication(first,second,third)!=true){
                cout<<"Matrix not allow"<>j.rowlen>>j.columnlen;

        //j.rowlen=j.columnlen=3;
        if (object!=1){
                for(int i=0;i

你可能感兴趣的:(c)