Bay 12 Games Forum

Finally... => Life Advice => Topic started by: Mephisto on January 24, 2009, 10:16:16 pm

Title: C++ Newbie
Post by: Mephisto on January 24, 2009, 10:16:16 pm
Problem solved. Using a different book now, as the old one was using code that was deprecated back in '98.
Title: Re: C++ and multidimensional arrays
Post by: Vaiolis on January 24, 2009, 10:48:38 pm
If all the sales were equal, or could be calculated depending on the sale in question, you could make a series of For... Next loops, with the array numbers being the variables. In other words...

Code: [Select]
For(x=0;x<=4;x++)
{
     For(y=0;y<=3;y++)
     {
          For(z=0;z<=1;z++)
          {
               sales[x][y][z] = salesAmount;
          }
     }
}

If you have a specific number for each sale, you can also do this as you are declaring it...

Code: [Select]
int sales[5][4][2] = {91, 22, 37, 40, 58, 63, 99, 35, ...};

If you've already declared sales, you might be able to do this, but I'm unsure...

Code: [Select]
sales[] = {91, 22, 37, 40, 58, 63, 99, 35, ...};

I know the first one and second one will work, but I'm not sure about the third. Hope it helps!
Title: Re: C++ and multidimensional arrays
Post by: Mephisto on January 24, 2009, 10:55:37 pm
If all the sales were equal, or could be calculated depending on the sale in question, you could make a series of For... Next loops, with the array numbers being the variables. In other words...

Code: [Select]
For(x=0;x<=4;x++)
{
     For(y=0;y<=3;y++)
     {
          For(z=0;z<=1;z++)
          {
               sales[x][y][z] = salesAmount;
          }
     }
}
I guess I could have explained better. There are five working days in a week, four weeks in a month, and this covers two months. The numbers are the amount of widgets sold that day.

I had thought about doing it like that, but I would have to either enter all values at run time, which would not be good for recordkeeping, or find some way to make 40 different ints to use in the innermost loop.

I'm using an HTML version of the book, so I took the shortcut and copy/pasted the excessive typing.
Title: Re: C++ and multidimensional arrays
Post by: Captain Hat on January 25, 2009, 01:44:40 am
Well, other than getting user input, or setting it up like you've got it, I'm not too sure. You could try reading the numbers from a text file, but I haven't really tried that myself, random numbers also make for a nice way of quickly creating data.
Title: Re: C++ and multidimensional arrays
Post by: kcwong on January 25, 2009, 10:47:00 am
It'd help if you can tell us what do you intend to do with the array. I understand it's an exercise... so that "arrays are not suitable data structure for that" would be beside the point.

But do you want the arrays fixed size (from a constant), or do you want them created during run-time (i.e. so you can make arrays for 6 months with the same code)?

Do you want the values constant, initialized or set during run-time?
Title: Re: C++ and multidimensional arrays
Post by: Mephisto on January 25, 2009, 11:45:49 am
I'm not doing anything with it. It was one of the example programs in the book. I'm already 10+ chapters farther along.
Title: Re: C++ and multidimensional arrays
Post by: kcwong on January 25, 2009, 10:51:58 pm
Is that what you're after, declaring and initializing a fixed length multidimensional array?

Code: [Select]
#include <stdio.h>

int fixedLengthData[5][4][2] = {
    { {1,1}, {1,1}, {1,1}, {1,1} },
    { {1,1}, {1,1}, {1,1}, {1,1} },
    { {1,1}, {1,1}, {1,1}, {1,1} },
    { {1,1}, {1,1}, {1,1}, {1,1} },
    { {1,1}, {1,1}, {1,1}, {1,1} },
};

void printFixedData() {
    for (int i = 0; i < sizeof(fixedLengthData) / sizeof(int[4][2]); i++) {
        for (int j = 0; j < sizeof(fixedLengthData[i]) / sizeof(int[2]); j++) {
            for (int k = 0; k < sizeof(fixedLengthData[i][j]) / sizeof(int); k++) {
                printf("fixedLengthData[%d][%d][%d] = %d\n", i, j, k, fixedLengthData[i][j][k]);
            }
        }   
    }
}

void changeFixedData() {
    for (int i = 0; i < sizeof(fixedLengthData) / sizeof(int[4][2]); i++) {
        for (int j = 0; j < sizeof(fixedLengthData[i]) / sizeof(int[2]); j++) {
            for (int k = 0; k < sizeof(fixedLengthData[i][j]) / sizeof(int); k++) {
                fixedLengthData[i][j][k] = (i + 1) * 100 + (j + 1) * 10 + (k + 1);
            }
        }   
    }
}

int main(int argc, char **argv) {
    printFixedData();
    changeFixedData();
    printFixedData();
    return 0;
}
Title: Re: C++ and multidimensional arrays
Post by: Mephisto on January 25, 2009, 11:39:05 pm
Is that what you're after, declaring and initializing a fixed length multidimensional array?

I'm sorry to have wasted your time. The main post is now updated. I couldn't take the bugs anymore. The book consistently used code that was outdated and I finally ran into something I couldn't fix.
Title: Re: C++ and multidimensional arrays
Post by: kcwong on January 26, 2009, 04:06:05 am
Take a look at Bruce Eckel's books if you haven't already (they're free).

Bruce Eckel's Books on MindView (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html)