English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

4 answers

Hi. I would love to help you. But, answering the question will be as long as your question. I thought that instead of giving a long answer, i could redirect you to some place where you can find good references.

http://www.cplusplus.com/doc/tutorial/
www.cplusplus.com/doc/tutorial/pointers.html
www.codersource.net/c++_pointers.html

If you are still unable to find any useful information, and if there is anything specific that you would like to know, just drop in a mail to me. I shall try to help you.

Thanks! :)

2007-10-02 08:04:10 · answer #1 · answered by Asif 5 · 0 0

No, you have it wrong. If you write: int ar[20]; That declares an area of memory large enough to hold 20 integers. The name ar is a constant pointer to that area of memory. If you write: int arar[2][3]; That declares an area of memory large enough to store six integers, arranged as two arrays of three ints. The name arar is a constant pointer to that area of memory. While ar and arar are both pointers, they're not stored in a variable, they're just values. This is the same as the distinction between 42 and an int i, if you write 42 in your code it isn't stored anywhere, its just a value that is used to build the machine code, whereas the i variable has memory allocated and is stored. So pretty much your arr variable is how you think it should be. There is not an array of pointers stored anywhere in memory, there are only address calculations based on the constant pointer value of arr.

2016-05-19 15:48:36 · answer #2 · answered by ? 3 · 0 0

Generally speaking to access an array you use a for loop. This is for a 1 dimentional array. You want a two dimentional array.

So the way to do this is a nested for loop
ie.
for ( int i = 0; i < row; i++ ){
for ( int j = 0; j < col, j++ ){
cout << "Enter an integer: ";
cin >> array[i][j];
}
}

That is all there is to it. You already understand the array concept. Now go ahead and extend what you know.

Good luck.

2007-10-02 08:14:34 · answer #3 · answered by AnalProgrammer 7 · 0 0

u r goin right way baby

2007-10-02 18:30:18 · answer #4 · answered by i_am_the_next_best_one 5 · 0 0

fedest.com, questions and answers