d3adlycasanova
d3adlycasanova

Reputation: 23

working with a 3D array

I'll preface this with the standard "I'm new to C++" statement. This is my first time working with arrays and am having some difficulty.

I understand arrays in general. I also understand initializing the array. Where I am struggling is with printing specific rows/columns from my 3D array. Basically, we have a large array of high and low temperatures for three months. The goal is to print various subsets of the array per user request. Here is the shell of my program. I've entered only 3 days worth of data for starters.

The functions for calculating average, max, and min were provided. Maybe I'm not fully understanding how these functions are operating.

I started with something like this:

case 'A':
    cout << "Which month? ('J' for June, 'U' for July, 'A' for Aug)" << endl;
cin >> y;
    switch (y)
    {
    case 'J':
        cout << "The average for June was " << for (i=0; i<DAY; i++)
        {
            for (j=0; j<1; j++)
            {
                for (k=0; k<XTR; k++)
                {
                    average_array[3][0][2];
                }
            }
        } << endl; break;

where I am wanting to call the function average_array to iterate through all (3) days for only month June, and print the result.

I also tried replacing things like:

average_array[DAY][MONTH][XTR];

or

for (i=0; i<30; i++)
{
    for (j=0; j<1; j++)
    {
        for (k=0; k<2; k++)

Any suggestions are much appreciated! Obviously I am going about this entirely the wrong way, and am just not understanding how to reference specific sets of values in the array. The closest our textbook gets to explaining this is iterating through 1D and 2D arrays and printing the result. This is a small assignment, but understanding this is the key to completing our next (and last) large project.

Upvotes: 1

Views: 101

Answers (2)

jensa
jensa

Reputation: 2890

The following statement defines a (C-style) integer array of arrays of arrays:

int a[A][B][C];

The variable a now refers to an array which consists of arrays of arrays.

Looping through all the values would indeed be:

for(int i=0;i<A;++i){
    for(int j=0;j<B;++j){
        for(int k=0;k<C;++k){
            // do whatever....
        }
    }
}

If you only wanted a specific index for say the second-most "loop", remove that loop, for example:

for(int i=0;i<A;++i){
    for(int k=0;k<C;++k){
        std::cout << a[i][2][k];
    }
}

Upvotes: 1

Cole
Cole

Reputation: 740

A for loop can't be used as input into cout. You should cout each array value separately like this:

cout << "The average for June was ";
for (i=0; i<DAY; i++) {
   for (j=0; j<1; j++){
       for (k=0; k<XTR; k++){
           cout << average_array[3][0][2];
           //to make it a little nicer, use this
           //cout << ", "
       }
   }
} 
cout << endl;

However, there are a couple issues even with the above fix:

  1. This just prints out the value of average_array[3][0][2] many times. If you wanted to print out each value in the array change the [3][0][2] to [i][j][k].

  2. Your for (j=0; j<1; j++) only loops over one value of j, namely 0. There's no reason to have this loop.

Upvotes: 1

Related Questions