2017-10-12 1 views
-3

言語を与えます。私は、ユーザーが2つの行列のサイズを挿入できるようにするプログラムを持っています(私は2次元の代わりに1次元の行列を運動として保持しています)。問題を与えているコードの部分は、 :なぜ "std :: cin >> secondMatrix [i * twoColoumn + j];" C++ システム:Linuxの場合: コンパイル:G ++ prog.cpp -o progをここで</p> <p>が問題である私にセグメンテーションフォールト

2 
1 
1 
1 

このようにすると、出力はprintf( "4")とprintf( "5")の間のセグメンテーション違反になります。

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

内部ループの1が完了した後、あなたがoneRow回繰り返す:

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

} 
+0

VLAのは本当にC + +のものではありません。いくつかのコンパイラでサポートされていますが。 – Ron

+0

私はあなたのデバッガがどのように動作するかを学ぶべきだと思うので、すべての変数の値をチェックすることができます。 – nvoigt

+0

@Ronまた、コンパイラ拡張でサポートされていても、スタックサイズはまだ制限されています。 – user0042

答えて

0

あなたは、配列のインデックスの問題を持っています。

内側ループが2回完了した後、2 * oneRow回繰り返しました。

... など

は、あなたがしたい:

firstMatrix[i*oneRow + j] 

また、他の人が指摘したように、次の2行は、(可変長配列)ためのVLAとしてスタック上の配列を宣言します値oneColumnおよびoneRowは、ユーザーによって提供され、実行時まで認識されません。

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

これは必ずしもではありませんがサポートされていますが、お使いのコンパイラに依存することができます。 gccについては、https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.htmlを参照してください。また、これを参照してください:What's the difference between a VLA and dynamic memory allocation via malloc?

関連する問題

 関連する問題