code outputing hex values, expecting int/float

The code below is to test characteristics of a pointer array. It only has output in hex. I was expecting int/float or anything other than hex. What does it have/didn't have that is causing this?

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
// array_4.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>

using namespace std;
int main()
{
	int x = 10, y = 12, z = 2;
	int count = 1;

	int*** a = new int**[x];

	for (int i = 0; i < x; i++) {

		// Allocate memory blocks for
		// rows of each 2D array
		a[i] = new int*[y];

		for (int j = 0; j < y; j++) {

			// Allocate memory blocks for
			// columns of each 2D array
			a[i][j] = new int[z];
		}
	}
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < y; j++) {
			for (int k = 0; k < z; k++) {
				// Assign values to the
				// memory blocks created
				a[i][j][0] = 99;
				a[i][j][1] = 69;
				k++;
			}
		}
	}
	
	std::cout << "Print matrix" << std::endl;
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < y; j++) {
			for (int k = 0; k < z-1; k++) {
				std::cout << a[i][j][0] << std::endl;
				std::cout << a[i][j][1] << std::endl;
			}
			cout << endl;
		}
		cout << endl;
	}
	
	std::cout << "working order:" << std::endl;
	std::cout << "x'y' :";
	int img_x = 0, img_y = 0;
	std::cin >> img_x;
	std::cin >>  img_y;
	std::cout << "point: img x: " << img_x << "y: " << img_y << a[img_x, img_y, 0]<< std::endl << a[img_x, img_y, 1] << std::endl;

}
Last edited on
It doesn't compile, because you haven't declare i_x and i_y.

It spits out garbage because thats what you told to spit out with a[i_x, i_y, 0] (these are 3-component c++ arrays, not Python's numpy arrays).
Ok, I've reposted what was an error on my part. The same problem persists when running, hexadecimal output:

Input image point x,y to retrieve corresponding x'y' :2
2 point: img x: 2 y: 2000002CB239E32C0  000002CB239E3330
Last edited on
 
a[img_x][img_y][0]
1
2
a[img_x, img_y, 0]


That doesn't do what you think it does. It uses the comma operator within an array index so it evaluates img_x then img_y then 0 and returns the value of final evaluation which is 0. So this becomes:

 
a[0]


which is a pointer - so the value of the pointer is printed in hex.

You probably mean

 
a[img_x][img_y][0]

An xy problem, lipstick on a pig, or dreaded waste polishing?

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

int main()
{
    int x_LIMIT = 4, y_LIMIT = 2, z_LIMIT = 3;
    float*** array_3D = new float**[x_LIMIT];
    
    for (int i = 0; i < x_LIMIT; i++)
    {
        array_3D[i] = new float* [y_LIMIT];
        
        for (int j = 0; j < y_LIMIT; j++)
        {
            array_3D[i][j] = new float[z_LIMIT]{-99.99};
        }
    }
    
    for (int i = 0; i < x_LIMIT; i++)
    {
        for (int j = 0; j < y_LIMIT; j++)
        {
            for (int k = 0; k < z_LIMIT; k++)
            {
                array_3D[i][j][k] = (i + j + k) * 0.9;
            }
        }
    }
    
    std::cout << "Print matrix\n";
    for (int i = 0; i < x_LIMIT; i++)
    {
        for (int j = 0; j < y_LIMIT; j++)
        {
            for (int k = 0; k < z_LIMIT; k++)
            {
                std::cout << array_3D[i][j][k] << ' ';
            }
            std::cout << ' ';
        }
        std::cout << '\n';
    }
    
    int XX{0}, YY{0}, ZZ{0};
    std::cout << "Enter coordinates as x y z: ";
    std::cin >> XX >>  YY >> ZZ;
    
    std::cout
    << "Location: " << XX << ' ' << YY << ' ' << ZZ
    << " Value: " << array_3D[XX][YY][ZZ]
    << '\n';
    
    return 0;
}


Print matrix
0 0.9 1.8  0.9 1.8 2.7  
0.9 1.8 2.7  1.8 2.7 3.6  
1.8 2.7 3.6  2.7 3.6 4.5  
2.7 3.6 4.5  3.6 4.5 5.4  
Enter coordinates as x y z: 1 1 2
Location: 1 1 2 Value: 3.6
Program ended with exit code: 0


Topic archived. No new replies allowed.