So I have this program that deals with one dimensional arrays. I want to expand it to two with array[row][col] how can I do this?
#include
using namespace std;
int* getArray(int row);
void printArray(int* array, int row);
int main()
{
int row;
cout <<"Enter row length: ";
cin>>row;
int* array = getArray(row);
cout << "array in main" << endl;
printArray(array, row);
/*int* array2 = getArray();
cout << "array2 in main" << endl;
printArray(array2);
cout << "array in main" << endl;
printArray(array);
delete[] array2;*/
delete[] array;
return 0;
}
int* getArray(int row){
int *array = new int[row];
for ( int i = 0; i < row; i++ ){
cout << "Enter an integer: ";
cin >> array[i];
}
cout << "array in getArray()" << endl;
printArray(array, row);
return array;
}
void printArray(int* array, int row)
{
for ( int i = 0; i < row; i++ ){
cout << *(array+i) << " ";
}
cout << endl;
cout << endl;
}
2007-10-02
07:57:19
·
4 answers
·
asked by
thenamelessrock
1
in
Computers & Internet
➔ Programming & Design
It would also be nice if I could make a structure matrix that getArray, printArray, and Array are all part of, but I can't figure out how to make an array in a structure using constants of that structure.
2007-10-02
07:58:29 ·
update #1
I know how to make a two dimensional array, but two dimensional arrays don't work well with functions, so I'm assuming you use pointers to deal with that (as we did in the one dimensional case)
2007-10-02
08:46:19 ·
update #2