3D array


I created a 3d array that inputs a value into a coordinate but when I execute, it only shows the start of coordinate 7,0,2 to 9,9,9.
When I had only a 2 dimensional array, it did show all the coordinates. Can you please help me figure out why it is doing this.

Thanks,
Angela




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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>
#include <cstdlib>	// Needed for rand and srand
#include <ctime>	// Needed for the time function
using namespace std;

	
//Function Prototypes
void processThroughArray(int [][10][10],int,int,int);

int main()
{

	// establish array and set all values to 0
	int myArray[10][10][10]= { 0 };

	// establish x and y position markers
	int x = 0;
	int y = 0;
	int z = 0;


	// establish input for x and y from the user
	int xInput = 0;
	int yInput = 0;
	int zInput = 0;
	

	// variable for value entered
	int inputValue = 0;

	// Get the user's value and coordinate
	cout << "\nPlease enter the x coordinate ";
	cin >> xInput;
	cout << "\nPlease enter the y coordinate ";
	cin >> yInput;
	cout << "\nPlease enter the z coordinate ";
	cin >> zInput;

	
	cout << "\nPlease enter the value to place in " << xInput << "," << yInput << "," << zInput <<" ";
	cin >> inputValue;
	
        
	
  	// place the value in the coordinate
	myArray[xInput][yInput][zInput] = inputValue;
	cout << "\nYou have successfully placed the value " << inputValue << " in coordinate " << xInput << ", " << yInput << ", " << zInput << " ";
	
    //Function performing for loop
    processThroughArray(myArray, x, y, z);

	// indicate end of array processing
	cout << "\nArray Processed" << endl;
	
    system("pause");
	
	return 0;
}


//**************************************************************************
// Definition of function processThroughArray: Process through the array   *
//the for loop                                                             *
//**************************************************************************
void processThroughArray (int myArray[][10][10],int x, int y, int z)
{
  
	for (int x = 0; x < 10; x++)	
	{
        for (int y = 0;  y < 10; y++)
        
        {
            for (int z = 0;  z < 10; z++)
            {
             
// Display the value of the coordinate
cout << "\nCordinate " << x << ", " << y << ", " << z << " value is " << myArray[x][y][z];  
        }
    }
	}
}
When I clicked the run tool (The gear button top right of the code), all coordinates showed up, and the variable got changed. Maybe you should try to build this on a different IDE (maybe mingw command line?).

Also, Remove that system pause immediately. use:
1
2
3
std::cout<<"Press any key...\n";
std::string s;
std::getline(std::cin,s);
closed account (48T7M4Gy)
Try this:

http://superuser.com/questions/161125/how-do-i-enable-scrolling-in-windows-command-prompt
thanks, I increased the screen buffer size on the windows command prompt (right click then layout tab, then properties) and then I was able to see all the way to the top but as I continued to work on it I noticed that there was actually two sets of coordinates. so I changed the for loop to this and now it is only 1 set of coordinates with a value on two of the coordinates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void processThroughArray(int myArray[][10][10], int x, int y, int z, int x2, int y2, int z2)
{

	for (int x=0, x2 = 0; x<10, x2 < 10; x++,x2++)
	{
		for (int y=0,y2 = 0; y<10,y2 < 10; y++,y2++)

		{
			for (int z=0,z2 = 0; z< 10,z2 < 10; z++,z2++)
			{

				// Display the value of the coordinate
				cout << "\nCordinate " << x << ", " << y << ", " << z << " value is " << myArray[x,x2][y,y2][z,z2];
				
			}
		}
	}


	
}
Last edited on
Topic archived. No new replies allowed.