<代码收藏>矩阵相乘

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void main()
{
	srand(unsigned(time(NULL)));
	int row=rand()%10;
	int column=rand()%10;
	int three=rand()%10;

	int** p1=new int*[row];
    for(int i=0;i<row;++i)
    {
        p1[i]=new int[column];
    }
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<column;++j)
        {
            p1[i][j]=rand()%10;
        }
    }
    cout<<"p1 is"<<endl;
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<column;++j)
        {
            cout<<p1[i][j]<<"\t";
        }
        cout<<endl;
    }
	

	/////////////////////////////////////////////////////
	int** p2=new int*[column];
    for(int i=0;i<column;++i)
    {
        p2[i]=new int[three];
    }
    for(int i=0;i<column;++i)
    {
        for(int j=0;j<three;++j)
        {
            p2[i][j]=rand()%10;
        }
    }
	cout<<endl;
    cout<<"p2 is"<<endl;
    for(int i=0;i<column;++i)
    {
        for(int j=0;j<three;++j)
        {
            cout<<p2[i][j]<<"\t";
        }
        cout<<endl;
    }

	int** p3=new int*[row];
    for(int i=0;i<row;++i)
    {
        p3[i]=new int[three];
    }
	//初始化p3
	for(int i=0;i<row;++i)
    {
        for(int j=0;j<three;++j)
        {
            p3[i][j]=0;
        }
    }

	for(int i=0;i<row;i++)
	{
		for(int j=0;j<three;j++)
		{
			for(int k=0;k<column;k++)
			p3[i][j]=p3[i][j]+p1[i][k]*p2[k][j];
		}
	}
	cout<<"p3 is"<<endl;
	for(int i=0;i<row;++i)
    {
        for(int j=0;j<three;++j)
        {
            cout<<p3[i][j]<<"\t";
        }
		cout<<endl;
    }


	//delete p1
	for(int i=0;i<row;++i)
    {
        delete []p1[i];
    }
    delete []p1;

	//delete p2
	for(int i=0;i<column;++i)
    {
        delete []p2[i];
    }
    delete []p2;
	//delete p3
	for(int i=0;i<row;++i)
    {
        delete []p3[i];
    }
	int aa;
	cin>>aa;
}

你可能感兴趣的:(矩阵相乘)