Array dimensions

Alright this i found interesting.. i was playing with arrays and i tried to do an (x,y,z) array to try to have three dimensions to it instead of two. I understand on how some of it came out but i dont understand one line:

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
#include <iostream>

using namespace std;

int main ()
{
  int bob [3][3][3];
  int x=0, y=0, z=0, a=3, b=3, c=3, num=100;
  for(x; x<a; x++)
  {
      bob[x][y][z]=num;
      num--;
      bob[x][y+1][z]=num;
      num--;
      bob[x][y][z+1]=num;
      num--;
      y++;
      z++;
  }
  y=0;
  z=0;
  x=0;
  for(x=0;x<b;x++)
  {
      cout<<"\n"<<bob[x][y][z]<<" "<<bob[x][y+1][z]<<" "<<bob[x][y][z+1];
      y++;
      z++;
  }  
  cout<<"\n bob[2][1][0]= "<<bob[2][1][0]<<" bob[1][1][1]= "<<bob[1][1][1];   
  cin.get();//used in windows, all that is needed is to press enter
  return 0;
}

OUTPUT:
1
2
3
4
100 99 98
97 96 95
94 93 92
 bob[2][1][0]=4072650 bob[1][1][1]= 97


The Output from line 29. it should be within 100 right?
Last edited on
You never assign a value to that location. When you're doing your assignment loop in lines 9-18, [2][1][0] never gets assigned, since by that point y = 2, and thus the position in your array at [2][1] never gets accessed. The number you're seeing is (I'm pretty sure) the value of the pointer to that particular region in memory.

If you're trying to assign your whole array, the easiest way is to use 3 for loops, nested within one another.
Last edited on
Is there any possible way you could give me an example.. Thsi isn't for an assignment just something i want to play around with
well, to have two nested for loops, you'd just do

1
2
3
for (int i=0;i<x_limit;i++)
   for (int j=0;j<y_limit;j++)
      your_array[i][j] = something;


I'm sure you can extrapolate to three loops :)
Topic archived. No new replies allowed.