Here is my code.. not sure what the issue is but it keeps crashing. Im not fnished yet but i cant get passed this part. i have no idea why its crashing. Any and all help is greatly appreciated. thanks.
#include <iostream>
usingnamespace std;
int main()
{
// 2) Exercise 2 (Matrix multiplication)
//
// The goal is to write a program that computes the product of two matrices.
// First, your code will ask the user to enter the size of the first and second matrices. If the number
// of columns of the first matrix is not equal to the number of rows of the second matrix, the program
// your display “Multiplication is impossible due to matrix size”.
// Second your code will ask the user to enter each element of the matrices.
// Then, the program will compute the product.
// Finally, the program will display the result in form off matrix data.
int col1, col2, row1, row2;
int a [row1] [col1];
int b [row2] [col2];
cout << "Please enter the size of the first matrix (row x column)." << endl;
cin >> row1 >> col1;
cout << endl << endl;
cout << "Please enter the size of the second matrix (row x column)." << endl;
cin >> row2 >> col2;
if ( col1 != row2)
{
cout << "Multiplication is impossible due to matrix size." << endl;
}
elseif ( col1 == row2)
{
cout << "\nEnter values for your first matrix:\n";
for ( int i = 0 ; i <= row1 ; i++ )
{
for (int j = 0 ; j <= col1 ; j++ )
cin >> a [i][j] ;
}
cout << "\nEnter values for your second matrix:\n";
for ( int i = 0 ; i <= row2 ; i++ )
{
for (int j = 0 ; j <= col2 ; j++ )
cin >> b [i][j] ;
}
}
return 0;
}
The compiler needs to know the size of arrays at compile time. row1,col1,row2,col2 are going to have uninitialized garbage values when this is compiled. If you need to wait for user input to determine the size of the array, you can dynamically allocate the array with new after the values are input. (along with a corresponding delete to free the memory)
Also - in your for loops - the largest element in an array is going to have a number one less than the actual size of the array. When you have <= to, the = part is trying to access an element of the array that doesn't exist.