介绍
在本实验中,你将学习如何使用 C++ 编程对给定的二维数组执行各种矩阵操作,如加法、减法和乘法。你还将学习如何有效地操作和使用数组。
在本实验中,你将学习如何使用 C++ 编程对给定的二维数组执行各种矩阵操作,如加法、减法和乘法。你还将学习如何有效地操作和使用数组。
在 ~/project
目录下创建一个名为 matrix_operations.cpp
的新文件。可以通过在终端中运行以下命令来完成。
touch ~/project/matrix_operations.cpp
在我们的 C++ 程序中,第一步是包含用于输入/输出操作和清除控制台的库。之后,为你的 C++ 程序设置主函数。你可以使用以下代码作为模板。
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
// 你的代码写在这里
getch();
return 0;
}
在这一步中,我们将创建一个用于执行两个矩阵加法的案例。C++ 程序会要求用户输入两个矩阵的阶数或维度(必须是 m*n
和 o*p
)。
如果矩阵的维度不相同,程序将返回错误信息。然后输入矩阵的元素并进行加法运算。最终的代码应如下所示。
//ADDITION
case 1:
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cin>>m;
cout<<"* \n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"* \n";
cin>>p;
if (m==o&&n==p)
{
cout<<"Addition possible "<<endl;
}
else
{
cout<<"Addition not possible ";
l=0;
}
if(l)
{
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
}
cout<<"\nEnter the elements of matrix 2: "<<endl;
for (i=1;i<=o;i++)
{
for (j=1;j<=p;j++)
cin>>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nAddition:\nc=a+b=";
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<c[i][j]<<" ";
}
}
break;
在这一步中,我们将创建一个用于执行两个矩阵减法的案例。C++ 程序会要求用户输入两个矩阵的阶数或维度(必须是 m*n
和 o*p
)。
如果矩阵的维度不相同,程序将返回错误信息。然后输入矩阵的元素并进行减法运算。最终的代码应如下所示。
//SUBTRACTION
case 2:
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cin>>m;
cout<<"* \n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"* \n";
cin>>p;
if (m==o&&n==p)
{
cout<<"Subtracion possible "<<endl;
}
else
{
cout<<"Subtraction not possible ";
l=0;
}
if(l)
{
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
}
cout<<"\nEnter the elements of matrix 2: "<<endl;
for (i=1;i<=o;i++)
{
for (j=1;j<=p;j++)
cin>>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nSubtraction:\nc=a-b=";
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<c[i][j]<<" ";
}
}
break;
在这一步中,我们将创建一个用于执行两个矩阵乘法的案例。C++ 程序会要求用户输入两个矩阵的阶数或维度(必须是 m*n
和 o*p
)。
如果矩阵的维度不兼容,程序将返回错误信息。然后输入矩阵的元素并进行乘法运算。最终的代码应如下所示。
//MULTIPLICATION
case 3:
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cin>>m;
cout<<"* \n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"* \n";
cin>>p;
if (n==o)
{
cout<<"Multiplication possible "<<endl;
}
else
{
cout<<"Multiplication not possible ";
l=0;
}
if(l)
{
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
}
cout<<"\nEnter the elements of matrix 2: "<<endl;
for (i=1;i<=o;i++)
{
for (j=1;j<=p;j++)
cin>>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nMultiplication:\nc=aXb=";
for (i=1;i<=m;i++)
{
for (j=1;j<=p;j++)
{
c[i][j]=0;
for (int k=1;k<=n;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<c[i][j]<<" ";
}
}
break;
你可以通过在终端中使用以下命令编译并执行 matrix_operations.cpp
文件:
g++ ~/project/matrix_operations.cpp -o matrix && ./matrix
你可能需要将 matrix_operations.cpp
替换为你为 C++ 程序文件使用的名称。
假设你的代码输出了预期的结果,你应该能够看到一个提示,要求输入案例编号(1 表示加法,2 表示减法,3 表示乘法)。在你输入选择后,程序将根据所选操作要求你输入矩阵。
以下是 matrix_operations.cpp
程序文件的完整代码。
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a[50][50],b[50][50],c[50][50],i,j,m,n,o,p,l=1,r;
cout<<"Enter case:\n";
cin>>r;
switch(r)
{
//ADDITION
case 1:
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cin>>m;
cout<<"*\n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"*\n";
cin>>p;
if (m==o&&n==p)
{
cout<<"Addition possible "<<endl;
}
else
{
cout<<"Addition not possible ";
l=0;
}
if(l)
{
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
}
cout<<"\nEnter the elements of matrix 2: "<<endl;
for (i=1;i<=o;i++)
{
for (j=1;j<=p;j++)
cin>>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nAddition:\nc=a+b=";
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<c[i][j]<<" ";
}
}
break;
//SUBTRACTION
case 2:
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cin>>m;
cout<<"*\n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"*\n";
cin>>p;
if (m==o&&n==p)
{
cout<<"Subtracion possible "<<endl;
}
else
{
cout<<"Subtraction not possible ";
l=0;
}
if(l)
{
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
}
cout<<"\nEnter the elements of matrix 2: "<<endl;
for (i=1;i<=o;i++)
{
for (j=1;j<=p;j++)
cin>>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nSubtraction:\nc=a-b=";
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<c[i][j]<<" ";
}
}
break;
//MULTIPLICATION
case 3:
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cin>>m;
cout<<"*\n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"*\n";
cin>>p;
if (n==o)
{
cout<<"Multiplication possible "<<endl;
}
else
{
cout<<"Multiplication not possible ";
l=0;
}
if(l)
{
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
}
cout<<"\nEnter the elements of matrix 2: "<<endl;
for (i=1;i<=o;i++)
{
for (j=1;j<=p;j++)
cin>>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nMultiplication:\nc=aXb=";
for (i=1;i<=m;i++)
{
for (j=1;j<=p;j++)
{
c[i][j]=0;
for (int k=1;k<=n;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for (i=1;i<=m;i++)
{
cout<<endl;
for (j=1;j<=p;j++)
cout<<c[i][j]<<" ";
}
}
break;
// default case
default:
cout<<"Wrong choice";
}
getch();
return 0;
}
在本实验中,你学习了如何使用 C++ 编程对给定的二维数组执行各种矩阵操作。你还学习了如何操作和使用数组。现在你应该对 C++ 编程中矩阵的工作原理有了很好的理解。