Does anyone know an easy way to do array's of arrays? I'm not 100% sure this is the thing I want though because I just need a simple way to make a loop able to change the list it's comparing. I have the loop done, I just can't figure out how to make the array change, so i was thinking there might be a way to just do arrays of arrays.

Thanks everyone,
matt

Recommended Answers

All 9 Replies

int arrayOfArrays[2][3] will make an array of 2 integer arrays of length 3 each. A 2x3 if you will.

Member Avatar for iamthwee

Please give a better description with some kind of context. It is hard to understand what you are trying to do.

int arrayOfArrays[2][3] will make an array of 2 integer arrays of length 3 each. A 2x3 if you will.

Isn't an array of 3 integers of 4 elements each?

For on the fly sparse arrays, you can try something like:

#include <iostream>

int main()
{
    using namespace std;

    const int ROWS = 3;
    const int COLS = 4;

    double** dArr = new double*[ROWS];
    for(int i = 0; i < ROWS; ++i)
    {
        dArr[i] = new double[COLS];
        for(int j = 0; j < COLS; ++j)
        {
            dArr[i][j] = i * COLS + j;
        }
    }

    for(int i = 0; i < ROWS; ++i)
    {
        for(int j = 0; j < COLS; ++j)
        {
            cout << dArr[i][j] << "\t";
        }
        cout << '\n';
    }
    cin.get();
}

Isn't an array of 3 integers of 4 elements each?

Nope, the dimensions which we specify at the time of declaration/definition are the actual dimensions of the array. Their indexing is 1 off of the dimensions.

Nope, the dimensions which we specify at the time of declaration/definition are the actual dimensions of the array. Their indexing is 1 off of the dimensions.

Let's see if I finally get this correct for once:

int array[3];

array[0]
array[1]
array[2]
array[3]

Four elements in an a single dimention array.

int array[3][2];

array[0][0]
array[0][1]

array[1][0]
array[1][1]

array[2][0]
array[2][1]

Correct?

Let's see if I finally get this correct for once:

int array[3];

array[0]
array[1]
array[2]
array[3]

Four elements in an a single dimention array.

No. Valid indices would be 0-2, for a total of 3.

int array[3][2];

array[0][0]
array[0][1]

array[1][0]
array[1][1]

array[2][0]
array[2][1]

Correct?

Yes

When you allocate an array, it basically tells the compiler "I want an array of N objects of type T". The compiler will lay those out in memory like so:

int arr[5];
|  0  |  1  |  2  |  3  |  4  |

Then when you index into the array, it takes the address of the first element of the array and adds the index to that address (it moves the index by the appropriate size of the data type, e.g. 1 byte for char, 4 bytes for a 32-bit int, etc..)

No. Valid indices would be 0-2, for a total of 3.

So I always got it wrong (opposite) in my mind.
Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.