简体   繁体   English

使用数组乘法矩阵?

[英]Multiplying matrices using arrays?

This is an assignment I have been struggling with. 这是我一直在努力的一项任务。 Basically I have the core idea and the code in working order. 基本上,我有工作的核心思想和代码。 But the problem is that I did it in a way different from the instructions. 但是问题是我以与指示不同的方式进行了操作。

So, to multiply the matrices I asked for the dimensions of the array beforehand (rows,columns). 因此,要乘以矩阵,我事先要求数组的尺寸(行,列)。 Then I would ask again for the values of the of the array. 然后,我会再次询问该数组的值。

But would what I would like to do is simply enter the values of my array and automatically find the dimensions of the array by the number of integers that are input. 但是我想做的就是简单地输入数组的值,并根据输入的整数数量自动找到数组的维数。 But I am not sure how to do this because I thought my instructor said something about not being able to set arrays to variable values or something like that. 但是我不确定如何执行此操作,因为我认为我的讲师说了一些关于无法将数组设置为变量值的信息或类似的信息。

//what I'd like to be able to do
Enter the first matrix:
1 2 3
4 5 6

Enter the second matrix:
5 6
7 8
9 0

// what I am currently doing
#include<iostream>
using namespace std;
int main()
{
int l,m,z,n;
int matrixA[10][10];
int matrixB[10][10];
int matrixC[10][10];

cout<<"enter the dimension of the first matrix"<<endl;
cin>>l>>m;
cout<<"enter the dimension of the second matrix"<<endl;
cin>>z>>n;
if(m!=z||z!=m){
cout<<"error in the multiblication enter new dimensions"<<endl;
cout<<"enter the dimension of the first matrix"<<endl;
cin>>l>>m;
cout<<"enter the dimension of the second matrix"<<endl;
cin>>z>>n;
}

else{
cout<<"enter the first matrix"<<endl;
for(int i=0;i<l;i++){
for(int j=0;j<m;j++){
     cin>>matrixA[i][j];
     }
     }
cout<<"enter the second matrix"<<endl;
for(int i=0;i<z;i++){
for(int j=0;j<n;j++){
    cin>>matrixB[i][j];
}
}
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
        matrixC[i][j]=0;
        for(int k=0;k<m;k++){
matrixC[i][j]=matrixC[i][j]+(matrixA[i][k] * matrixB[k][j]);
}
}
}

cout<<"your matrix is"<<endl;
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
cout<<matrixC[i][j]<<" ";
}
cout<<endl;
}
}
//system("pause");
return 0;
}

You cannot declare an array with a runtime dimension (known as a variable-length array in C, where they are allowed), the dimension must be known at compile time. 您不能声明具有运行时维度的数组(在C中称为可变长度数组,允许使用它们),必须在编译时知道该维度。 The solution is to either use a standard container like std::vector 解决方案是使用标准容器,例如std::vector

std::vector<std::vector<int>> matrix(M, std::vector<int>(N)); // M x N 

or use dynamic arrays, 或使用动态数组

int** matrix = new int*[M]; // allocate pointers for rows
for(size_t i = 0; i < N; ++i)
    matrix[i] = new int[N]; // allocate each row

then don't forget to delete them at the end of your program 然后别忘了在程序结尾删除它们

for(size_t i = 0; i < N; ++i)
    delete[] matrix[i];
delete[] matrix;

You should prefer the std::vector approach, as you don't have to deal with memory allocation etc. 您应该首选std::vector方法,因为您不必处理内存分配等问题。

From your lines of code, it seems that you want to ask the user to enter the correct values of column of matrix 1 and row of matrix 2. what you have used is an if statement which will only run a single time. 从您的代码行看来,您似乎想让用户输入矩阵1的列和矩阵2的行的正确值,所使用的是if语句,该语句只能运行一次。 if your value of "m" and "z" are not equal at the very first input , then you would never be able to enter the else part of your code where you have declared rest of your code to enter and multiply the matrices. 如果您在第一个输入处的值“ m”和“ z”不相等,那么您将永远无法在声明了其余代码的情况下输入代码的else部分来输入和相乘矩阵。

In order to check whether the values of "m" and "z" are equal or not, you can use a while loop or a do while loop. 为了检查“ m”和“ z”的值是否相等,可以使用while循环或do while循环。

while(m!=z)
{cout<<"enter the value m and z";`
cin>>m>>z;
}

besides this , it would be good if you use l,m,z,n as static members to save the memory. 除此之外,如果将l,m,z,n用作静态成员来节省内存,那将是很好的。

if you want to declare a matrix of your desired dimensions, then you can do it like this. 如果要声明所需尺寸的矩阵,则可以这样做。

firstly, you ask the user to enter the dimension of the matrix . 首先,您要求用户输入矩阵的维度。 then after when correct dimension has been entered, then you should be able to make a matrix of your required dimensions. 然后,在输入正确的尺寸后,您应该能够对所需尺寸进行矩阵处理。 Here is what i am trying to say: 这是我想说的:

cout<<"enter the values of l and m";
cin>>l>>m;
cout<<"enter the values of z and n";
cin>>z>>n;
int array1[l][m];
int array2[z][n];

Thus , you can enter the dimensions of your desired matrix easily. 因此,您可以轻松输入所需矩阵的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM