-3

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);
    }

}
Stefano Feltre
  • 71
  • 1
  • 4
  • 10

1 Answers1

0

You have an array indexing issue:

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];
    }   
}

After one completion of the inner loop, you iterate oneRow times.

After two completions of the inner loop, you have iterated 2*oneRow times.

... etc

You want:

    firstMatrix[i*oneRow + j]

Additionally, as others have pointed out, the following two lines declare your arrays on the stack as VLAs (variable length arrays) because the values oneColumn and oneRow are provided by the user and are not known until runtime.

int firstMatrix[oneColoumn*oneRow];
int secondMatrix[twoColoumn*twoRow];

This is not necessarily supported, but may be depending on your compiler. For gcc, see https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html. And also see this: What's the difference between a VLA and dynamic memory allocation via malloc?

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25