3 dimensional array

I need to have static values for a 3 dimensional array, am i inputting the values correctly?
Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
#define NUM_DEPTS 2
#define NUM_STORES 2
#define NUM_MONTHS 12

float A[NUM_STORES][NUM_DEPTS][NUM_MONTHS] = { { 1.1 }, { 1.2 }, { 1.3 }, { 1.4 }, { 1.5 }, { 1.6 }, { 1.7 }, { 1.8 }, { 1.9 }, { 2.0 }, { 2.1 }, { 2.2 },
{ 2.1 }, { 2.2 }, { 2.3 }, { 2.4 }, { 2.5 }, { 2.6 }, { 2.7 }, { 2.8 }, { 2.9 }, { 3.0 }, { 3.1 }, { 3.2 }, { 3.1 }, { 3.2 }, { 3.3 }, { 3.4 }, { 3.5 },
{ 3.6 }, { 3.7 }, { 3.8 }, { 3.9 }, { 4.0 }, { 4.1 }, { 4.2 }, { 2.1 }, { 2.2 }, { 2.3 }, { 2.4 }, { 2.5 }, { 2.6 }, { 2.7 }, { 2.8 }, { 2.9 }, { 3.0 },
{ 3.1 }, { 3.2 } };
Here's a snippet of my own program that may help you with your 3D array:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
inline int savingThrows (ClassSaveAs classSaveAs, int level, SavingThrowType savingThrowType) {
	static const std::vector<std::vector<std::vector<int>>> savingThrowChart = {
		{	// SaveAsFighter
		    {12, 13, 14, 15, 16},
		    {10, 11, 12, 13, 14},
		    {8,   9, 10, 10, 12},
		    {6,   7,  8,  8, 10},
		    {4,   5,  6,  5,  8}
		},
		{	// SaveAsMagicUser
		    {13, 14, 13, 16, 15},
		    {11, 12, 11, 14, 12},
		    { 8,  9,  8, 11,  8}
		},
		{	// SaveAsCleric
		    {11, 12, 14, 16, 15},
		    { 9, 10, 12, 14, 12},
		    { 6,  7,  9, 11,  9},
		    { 3,  5,  7,  8,  7}
		},
		{   // SaveAsNormalMan
			{14, 15, 16, 17, 18}
		},
		{	// SaveAsThief
		    {13, 14, 13, 16, 15},
		    {12, 13, 11, 14, 13},
		    {10, 11,  9, 12, 10},
		    { 8,  9,  7, 10,  8}
		},
		{	// SaveAsElf
		    {12, 13, 13, 15, 15},
		    {10, 11, 11, 13, 12},
		    { 8,  9,  9, 10, 10},
		    { 6,  7,  8,  8,  8}
		},
		{	// SaveAsDwarf
		    { 8,  9, 10, 13, 12},
		    { 6,  7,  8, 10, 10},
		    { 4,  5,  6,  7,  8},
		    { 2,  3,  4,  4,  6}
		},
		{	// SaveAsHalfling
		    { 8,  9, 10, 13, 12},
		    { 6,  7,  8, 10, 10},
		    { 4,  5,  6,  7,  8}
		}
	};
	const int levelCategory = [&classSaveAs, level]()->int {
		switch (classSaveAs) {
			case SaveAsFighter:
				if (level <= 0) {classSaveAs = SaveAsNormalMan; return 0;}
				return std::min ((level - 1) / 3, static_cast<int> (savingThrowChart[SaveAsFighter].size() - 1));
			case SaveAsMagicUser:  return std::min ((level - 1) / 5, static_cast<int> (savingThrowChart[SaveAsMagicUser].size() - 1));
			case SaveAsCleric:  return std::min ((level - 1) / 4, static_cast<int> (savingThrowChart[SaveAsCleric].size() - 1));
			case SaveAsNormalMan:  return 0;
			case SaveAsThief:  return std::min ((level - 1) / 4, static_cast<int> (savingThrowChart[SaveAsThief].size() - 1));
			case SaveAsElf:  return std::min ((level - 1) / 3, static_cast<int> (savingThrowChart[SaveAsElf].size() - 1));
			case SaveAsDwarf:  return std::min ((level - 1) / 3, static_cast<int> (savingThrowChart[SaveAsDwarf].size() - 1));
			case SaveAsHalfling:  return std::min ((level - 1) / 3, static_cast<int> (savingThrowChart[SaveAsHalfling].size() - 1));
		}
	}();
	return savingThrowChart[classSaveAs][levelCategory][savingThrowType];
}
Last edited on
Just throwing my bit here (feel free to ignore this comment).

It is at this point I'd feel more comfortable working with a 1D array and just treat it as a 3D array.
prestokeys, you have to have multiple sets of brackets for each array? For instance 2 groups of 24 values.
And Daleth I get what your saying but I think that would make more problems for me down the road.
Last edited on
so I have it broken up like this now,
1
2
3
4
5
6
7
float A[NUM_STORES][NUM_DEPTS][NUM_MONTHS] = 
{
{ { 1.1 }, { 1.2 }, { 1.3 }, { 1.4 }, { 1.5 }, { 1.6 }, { 1.7 }, { 1.8 }, { 1.9 }, { 2.0 }, { 2.1 }, { 2.2 } },	//NUM_DEPTS
{ { 2.1 }, { 2.2 }, { 2.3 }, { 2.4 }, { 2.5 }, { 2.6 }, { 2.7 }, { 2.8 }, { 2.9 }, { 3.0 }, { 3.1 }, { 3.2 } }, //NUM_DEPTS
{ { 3.1 }, { 3.2 }, { 3.3 }, { 3.4 }, { 3.5 }, { 3.6 }, { 3.7 }, { 3.8 }, { 3.9 }, { 4.0 }, { 4.1 }, { 4.2 } },	//NUM_STORES
{ { 2.1 }, { 2.2 }, { 2.3 }, { 2.4 }, { 2.5 }, { 2.6 }, { 2.7 }, { 2.8 }, { 2.9 }, { 3.0 }, { 3.1 }, { 3.2 } }	//NUM_STORES
};

but I get a "too many initializer error", any suggestions?
You don't have 2x2x12 as you should.
1
2
3
4
5
A = { C, D };
// where
define C   { E, F } // i.e. A = { { E, F }, D };
// where
define E   { a, b, c, d, e, f, g, h, i, j }
So then having like this might as well be a 2X12 array right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define NUM_DEPTS 2
#define NUM_STORES 2
#define NUM_MONTHS 12

float A[NUM_STORES][NUM_DEPTS][NUM_MONTHS] = 
{
	{
		{ 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2 },		//NUM_STORES
		{ 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2 }		//NUM_STORES
	},
	{
		{ 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2 },		//NUM_DEPTS
		{ 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2 } 		//NUM_DEPTS
	}
};
Keskiverto, I am confused by your example.
So then having like this

Yes, except that your comments are misleading.
1
2
3
4
5
6
7
8
9
10
11
float A[][][] = 
{
  { // Store_0
    { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2 }, // Dept_0
    { 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2 }  // Dept_1
  },
  { // Store_1
    { 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2 }, // Dept_0
    { 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2 }  // Dept_1
  }
};
what if I wanted to make it into a [2][12][2] array?
Would this be the way?
1
2
3
4
5
6
7
8
9
10
11
void sales(float A[NUM_STORES][NUM_MONTHS][NUM_DEPTS] =
{
	{	//NUM_STORES_0
		{ { 1.1, 1.2 }, { 1.3, 1.4 }, { 1.5, 1.6 }, { 1.7, 1.8 }, { 1.9, 2.0 }, { 2.1, 2.2 },	//NUM_DEPTS_0
		{ 2.1, 2.2 }, { 2.3, 2.4 }, { 2.5, 2.6 }, { 2.7, 2.8 }, { 2.9, 3.0 }, { 3.1, 3.2 } }    //NUM_DEPTS_1
	},
	{	//NUM_STORES_1
		{ { 3.1, 3.2 }, { 3.3, 3.4 }, { 3.5, 3.6 }, { 3.7, 3.8 }, { 3.9, 4.0 }, { 4.1, 4.2 },	//NUM_DEPTS_0
		{ 2.1, 2.2 }, { 2.3, 2.4 }, { 2.5, 2.6 }, { 2.7, 2.8 }, { 2.9, 3.0 }, { 3.1, 3.2 } }	//NUM_DEPTS_1
	}
});
Last edited on
That looks like a 2x1x12x2.
So how could I get a 2x12x2 then?
You have extra pair of curly braces that adds the fourth dimension. Take it out.

Alternatively, only one set of braces: int array[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };
Arrays are contiguous blocks of memory and can be initialized as 1D, even though they are dereferenced as multidimensional arrays.
Topic archived. No new replies allowed.