Language:c++ System:Linux: Compile:g++ prog.cpp -o prog
Here is the problem. I have a program that let the user insert the size of two matrices(I'm keeping the matrices 1D instead of 2D as an exercise).The part of the code that is giving me problems is usually fine, except when i put in input:
2
1
1
1
When I do this, the output is segmentation fault between printf("4") and printf("5").
#include <iostream>
int main(void)
{
int oneColoumn, oneRow, twoColoumn, twoRow;
std::cout << "\nHow many coloumns do you want for the first matrix?" << std::endl;
std::cin >> oneColoumn;
std::cout << "\nHow many rows do you want for the first matrix?" << std::endl;
std::cin >> oneRow;
std::cout << "\nHow many coloumns do you want for the second matrix?" << std::endl;
std::cin >> twoColoumn;
std::cout << "\nHow many rows do you want for the second matrix?" << std::endl;
std::cin >> twoRow;
int firstMatrix[oneColoumn*oneRow];
int secondMatrix[twoColoumn*twoRow];
for(int i=0; i < oneColoumn; i++)
{
for(int j=0; j < oneRow; j++)
{
std::cout << "Insert a number for the first matrix";
std::cin >> firstMatrix[i*oneColoumn + j];
}
}
printf("1");
for(int i=0; i < twoColoumn; i++)
{printf("2");
for(int j=0; j < twoRow; j++)
{printf("3");
std::cout << "Insert a number for the second matrix";
printf("4");
std::cin >> secondMatrix[i*twoColoumn + j];
printf("5");
}
}
int threeColoumn, threeRow;
if(oneColoumn>twoColoumn)
threeColoumn=twoColoumn;
if(oneRow>twoRow)
threeRow=twoRow;
int thirdMatrix[threeColoumn*threeRow];
char choice;
std::cout<<"Do you want to add or multiply the two matrices?(a/m)"<<std::endl;
std::cin>>choice;
if(choice=='a')
{
std::cout<<"The two matrices have been added"<<std::endl;
//Addition(firstMatrix,oneRow,oneColoumn,secondMatrix,twoRow,twoColoumn,thirdMatrix,threeRow,threeColoumn);
}
else if(choice=='m')
{
std::cout<<"The two matrices have been multiplied"<<std::endl;
//Multiplication(firstMatrix,oneRow,oneColoumn,secondMatrix,twoRow,twoColoumn,thirdMatrix,threeRow,threeColoumn);
}
}