accessing/assigning values of a 5 dimensional array

I'm okay with 2-dimensional arrays, but when I get to 3 or more, I can't seem to wrap my head around how to assign and/or pull values from specific parts.

To give an example, let's take the following example:

We know that a player can take up to 5 total quests, and each quest can have a max of 5 tasks.

Let's assume I have the following multi-dimensional array holding all of a players quest data:

           Quest ID   Quest task   #needed   #done   #flag
int quests    [5]         [5]        [1]      [1]     [1]


Pretend that a player accepts a quest that wants them to kill 2 bears, 5 ducks, and 13 wolves.

That quest has an ID of 78, which we store in "Quest ID", as shown above.
Each creature has an ID, stored in the "Quest Task" as shown above.
The number of each creature needed in the "#needed" as indicated above.
For the progress of each kill, that value is stored in "#done".
The "#flag", in this case, will remain 0.

What I'd like to do is the following:

1
2
3
4
5
6
7
quests[0] = 78;			// Store the questID
quests[0][0] = 3945;		// Store the 1st creature ID
quests[0][1] = 2230;		// Store the 2nd creature ID
quests[0][2] = 3045;		// Store the 3rd creature ID
quests[0][0][0] = 2;		// Store how many needed of the 1st creature
quests[0][1][0] = 5;		// Store how many needed of the 2nd creature
quests[0][2][0] = 13;		// Store how many needed of the 3rd creature 


As we know, the above code can't be done. How do I assign certain values to each specific dimension?

Thanks.
Nevermind... sorry. Got it figured out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    struct quest
    {
        int qID;
        struct task
        {
            int qTask;
            struct needed
            {
                int qNeed;
                struct obtained
                {
                    int qHave;
                    struct flags
                    {
                        int qFlag;
                    }flag[1];
                }have[1];
            }need[1];
        }task[5];
    }ID[5];


    ID[0].qID = 78;
    ID[0].task[0].qTask = 3945;
    ID[0].task[1].qTask = 2230;
    ID[0].task[2].qTask = 3045;
    ID[0].task[0].need[0].qNeed = 2;
    ID[0].task[1].need[0].qNeed = 5;
    ID[0].task[2].need[0].qNeed = 13;
As for the earlier problem you are forgetting to give it the proper location.

Think about a 1 d as a x coord. Think of a 2d as a X and Y coord , now anything more can be.. X , Y , Z , ect...Coords so in order to find the location it needs all of the coordinates.

Also I think you could probably present it in a neater way.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Task
{
    int CreatureID; //Id for the creature.
    int Required; //How many creatures you need to kill or w/e
    int Completed; //How many you killed or w/e currently
};

struct Quest
{
     int QuestID; //Stores the id of quest
     std::vector<Task> Tasks; //stores the tasks for the quest
     bool flag; //has the quest been completed?
};


Either way would work though its all about preference.
Thanks, giblit, that perspective does help. :)

If we were going with my proposed fix, I think this would be better, ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    struct quest
    {
        int qID;
        struct task
        {
            int qTask;
            int qNeed;
            int qHave;
            int qFlag;
        } task[5];
    } ID[5];    

    ID[0].qID = 78;
    ID[0].task[0].qTask = 3945;
    ID[0].task[1].qTask = 2230;
    ID[0].task[2].qTask = 3045;
    ID[0].task[0].qNeed = 2;
    ID[0].task[1].qNeed = 5;
    ID[0].task[2].qNeed = 13;



I'll give your suggestion a try and determine which I'll use. Main point though, I now have two workable solutions. Yay!
Giblit, is this kind-of what you meant?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    struct Task
    {
        int TaskID;
        int Required;
        int Completed;
        int flag;
    };

    struct Quest
    {
        int QuestID;
        std::vector<Task> Tasks;
    } q[5];

    q[0].QuestID = 78;
    q[0].Tasks[0].TaskID = 3945;
    q[0].Tasks[0].Required = 2;
    q[0].Tasks[1].TaskID = 2230;
    q[0].Tasks[1].Required = 5;
    q[0].Tasks[2].TaskID = 3045;
    q[0].Tasks[2].Required = 13;
Yes but I used a vector you are using arrays. If you wish to use array instead of vector it would be Task task[5]; instead of vector.

A vector is a container you can use if you do not know the size ahead of time.

It could store 0->??? items.

http://www.cplusplus.com/reference/vector/vector/?kw=vector

There are also other containers you can use.
http://www.cplusplus.com/reference/stl/


If you feel like they are too advanced just stick with static arrays for now.
Okay, I understand. I did use q[0].Tasks.resize(5);, just forgot to mention it above. I think you're right though. I probably will just stick with the arrays and leave vectors out of it for now, until I understand them completely. This is the first time I've used vectors so I should know more before I blindly start using in a real-world program. :P

Thanks for all your help!
Topic archived. No new replies allowed.