Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: C++ Newbie  (Read 1959 times)

Mephisto

  • Bay Watcher
    • View Profile
C++ Newbie
« 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.
« Last Edit: January 27, 2009, 11:30:25 pm by Mephisto »
Logged

Vaiolis

  • Bay Watcher
  • I Am Penguin!
    • View Profile
Re: C++ and multidimensional arrays
« Reply #1 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!
« Last Edit: January 24, 2009, 10:50:48 pm by Vaiolis »
Logged
Today's Toady Tip: 3 and 4 are not the same number.
How borgly is your borgle's borgle?
The Minister of BEEEES! and of The Great Charter for the Toady Protectorate!

Mephisto

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #2 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.
Logged

Captain Hat

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #3 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.

kcwong

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #4 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?
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #5 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.
Logged

kcwong

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #6 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;
}
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #7 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.
Logged

kcwong

  • Bay Watcher
    • View Profile
Re: C++ and multidimensional arrays
« Reply #8 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
Logged