私の仕事は、対角行列上のベクトルと要素の要素を置き換えることです。ベクトルはユーザによって入力され、行列はランダムである。例えば、私は、ベクトルの書き込み:ベクトルをmallocとポインタを使って行列に置き換えます。
1 2 3
とランダム行列は、私はこの
1) 7 3 8
2)
1 0 0
0 2 0
0 0 3
私が得たそれの最初の部分が、私は」第二取得する必要があります
7 0 0
0 3 0
0 0 8
ですm積層されている。ここでは、ベクトルを入力されています。ここでは
int size;
std::cout << ("Enter the dimentoinal of vector and matrix (enter one number): ");
std::cin >> size;
int * arrayPtr = (int*) calloc(size,sizeof(int));
if (arrayPtr == NULL) exit (1);
for (int ix = 0; ix < size; ix++)
{
std::cout << "Enter element #" << ix<< " ";
std::cin >> arrayPtr[ix];
}
system ("clear") ;
std::cout << "\n\nResulting vector is:\n[ ";
for (int ix = 0; ix < size; ix++)
{
std::cout << arrayPtr[ix] << " ";
}
cout << "]\n\n\n" ;
は、コードが(画面上の正しい結果ではありません)動作していないことを、次のとおりです。
cout << "The new matrix is :\n" ;
int * matr_n = (int*) calloc(size,sizeof(int));
cout << "\n" ;
for (int ix = 0 ; ix<size; ix++)
{
matr_n = &arrayPtr[ix] ;
cout << *matr_n << " " ;
for (int i = 0; i<size; i++)
{
cout << "\n" ;
for (int j = 0; j<size; j++)
{
if (i==j)
{
cout << *matr_n << " " ;
}
else
cout << 0 << " " ;
}
}
}
私は問題はポインタやmalloc関数/のcalloc関数を使用していることを知っていますしかし、初心者のためにそれを高速にキャッチすることは難しいです。 修正できますか?
**インデント**キリストのためにあなたのコード。 – gsamaras
*なぜ* mallocをC++で使いたいのですか?それはほとんど常に悪い考えです。 – UnholySheep
* "修正できますか?" * - 'std :: vector'を使用してください。 –