Diskuze: Vracení z funkce 2d pole
Zobrazeno 4 zpráv z 4.
Ještě jedna věc k tomu, to pole by mělo být dynamické aby se mohla zadat jeho velikost během výpočtu. Děkuji.
Ahoj, malo by to byť nejak takto:
#include <iostream>
using namespace std;
int **createMatrix(int height, int width);
int main(){
cout << "Enter number of rows: " << endl;
int rows;
cin >> rows;
cout << "Enter number od columns: " << endl;
int columns;
cin >> columns;
int** matrix = createMatrix(rows, columns);
// vypíše obsah poľa
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
cout << matrix[i][j] << "\t";
}
cout << endl;
}
// uvolni pamäť
for (int i = 0; i < rows; i++){
delete[] matrix[i];
}
delete[] matrix;
}
int **createMatrix(int rows, int columns){
// alokuje dynamicky pamäť
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++){
matrix[i] = new int[columns];
}
// naplní pole nulami
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
matrix[i][j] = 0;
}
}
return matrix;
}
Ak niečomu nerozumieš, pýtaj sa
Zobrazeno 4 zpráv z 4.