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;
}
getline? How aboutstringstreamorstrtok? How about dynamic memory allocation or containers likestd::vector?getline. 2) How to onstruct dynamic memory or avector. 3) Given a line like "10 20 30", how to parse it usingstrtokorstringstreamto either fill a vector or construct an array and fill it.