0

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;
}
5
  • Do you want help storing the matrices, or detecting their dimensions? Commented May 19, 2015 at 2:33
  • I want to be able to detect the dimensions when I do: cin>>matrixA[i][j]. So basically I am trying to get the values of the two dimensional array and set them at the same time, if something like that is possible. Commented May 19, 2015 at 2:44
  • It's possible, but it takes some work. Are you familiar with getline? How about stringstreamor strtok? How about dynamic memory allocation or containers like std::vector? Commented May 19, 2015 at 3:15
  • I have read some summaries and examples of what they are and do. But I'm not sure enough to know how to implement them yet. Commented May 19, 2015 at 3:16
  • You'll have to learn to use them one at a time. 1) How to grab a whole line of input using getline. 2) How to onstruct dynamic memory or a vector. 3) Given a line like "10 20 30", how to parse it using strtok or stringstream to either fill a vector or construct an array and fill it. Commented May 19, 2015 at 3:26

2 Answers 2

1

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. The solution is to either use a standard container like 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.

Sign up to request clarification or add additional context in comments.

2 Comments

But vector-of-vector is not ideal for rectangular matrices. Better to have one vector with size (rows*columns), and optionally a second vector of pointers, to the beginning of each row. That way locality is maintained.
@BenVoigt Thanks for the comment, indeed, you keep it continuous this way.
0

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. 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.

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.

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.

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.

4 Comments

int array1[l][m]; int array2[z][n]; is not legal in C++
I'm not sure but I thought giving arrays variable values like this was not allowed.
i think that once you have assigned a value to a variable, then you can use it any way you want. I think that this should work . If it does not work , then you can use constant to solve the problem.
Beside this , i typed a code right now and fortunately it is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.