3D arrays

I am trying to find the minimum and maximum values in a 3D array. But for some reason my code just doesn't work. There are no errors that apear in the compiler but I think it's a logical error. Basically, when I run the code, it doesn't show anything.

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
  #include <iostream>
#define NUM_ROW 4
#define NUM_COLUMN 4
#define NUM_DEPTH 3

using namespace std;


/* 1. The minimum and maximum values among all the array elements*/

float minMax(float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH], float &minValue, float &maxValue)
{
	minValue = 1000.0;
	maxValue = -1000.0;

	for (int i = 0; i < 4, i++;)
		for (int j = 0; j < 4, j++;)
			for (int k = 0; k < 3, k++;)
			{
		if (Value[i][j][k] < minValue)
		{
			return minValue;
		}
		if (Value[i][j][k] > maxValue)
		{
			return maxValue;
		}

			}
}

/*2. The average values of all array elements*/


/*3. The location (i, j, k)  of the element that 
has the maximum value difference with its neighboring elements (1 point). 
Here (i, j, k) refers to the array indices of the element. */

int main(){

	float a, b;

	float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] =
	{ 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,
	4.1, 4.2, 4.3, 4.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2
	};

	minMax(Value, a, b);

	system("pause");
	return 0;


}
There's no statement to print anything...
looking for cout ?
http://www.cplusplus.com/reference/iostream/cout/?kw=cout

lines 20 and 24 test if the Value[] element is less than -1000 or greater than 1000
line 43 defines elements between 1 and 5ish so minMax will reach the end of the loops
should return something on line 30 for that case...
Note that a "return" instruction will immediately halt the execution of the function. So the first time the program gets to "return minValue" or "return maxValue", your function will end and return that value. I don't think that is what you want to do.
I fixed my code. Now the problem is with the algorithm. Can you guys help me make a successful algorithm that finds the min and max value in my 3D array?

So I set my maxValue and minValue to the first element in my array which is 1.1 or Value[0][0][0]. Then, I start to compare 1.1 with the other elements in the function.

Although there are elements in the array that are bigger than 1.1 but the value assigned to the variable maxValue never change. Am I doing the passing-by-reference wrong?

Thanks in advance.

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
  #include <iostream>
#define NUM_ROW 4
#define NUM_COLUMN 4
#define NUM_DEPTH 3

using namespace std;


/* 1. The minimum and maximum values among all the array elements*/

float minMax(float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH], float &minValue, float &maxValue)
{

	minValue = Value[0][0][0];
	maxValue = Value[0][0][0];

	for (int i = 0; i < 4, i++;)
		for (int j = 0; j < 4, j++;)
			for (int k = 0; k < 3, k++;)
			{
				if (minValue < Value[i][j][k])
				{
					minValue = Value[i][j][k];
				}

				else if (maxValue > Value[i][j][k])
				{

					maxValue = Value[i][j][k];
				}
			}

			cout << minValue << endl;
			cout << maxValue << endl;

			return 0.0;
}

/*2. The average values of all array elements*/


/*3. The location (i, j, k)  of the element that 
has the maximum value difference with its neighboring elements (1 point). 
Here (i, j, k) refers to the array indices of the element. */

int main(){

	float a, b;

	float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] =
	{ 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,
	4.1, 4.2, 4.3, 4.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2
	};

	minMax(Value, a, b);

	system("pause");
	return 0;


}
Last edited on
The conditions should be reversed:

1
2
3
4
5
6
7
8
if (Value[i][j][k] < minValue)
{
    minValue = Value[i][j][k];
}
else if (Value[i][j][k] > maxValue)
{
    maxValue = Value[i][j][k];
}
Last edited on
@abhishekm71

I reversed the conditions, it still gives me the same output.

Edit: for some reason, the values of Max and Min never changes although I used pass-by-reference. What is wrong that I am doing?

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
#include <iostream>
#define NUM_ROW 4
#define NUM_COLUMN 4
#define NUM_DEPTH 3

using namespace std;

/* Finds Maximum and Minimum Elements in Array */

float minMax(float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH], float &minValue, float &maxValue)
	{

	minValue = Value[0][0][0];
	maxValue = Value[0][0][0];

	for ( int i = 0; i < 4; i++)
		for ( int j = 0; j < 4; j++)
			for (int k = 0; k < 3; k++)
				{
				if (Value[i][j][k] < minValue)
					{
					minValue = Value[i][j][k];
					}
				else if(Value[i][j][k] > maxValue)
					{
					maxValue = Value[i][j][k];
	
					}
				}

			cout << maxValue << endl;
			cout << minValue << endl;

			return 0.0;
	}

int main()
	{

	float a, b;
	
	float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] =
	{ 	 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,
         4.1, 4.2, 4.3, 4.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2     
	};

	
	minMax(Value, a, b);
	
	system("pause");
	return 0;
	
	}


1.1
1.1
Press any key to continue . . .
Last edited on
Your 3D array initialization is incorrect. See this link:
http://stackoverflow.com/questions/2178909/how-to-initialize-3d-array-in-c
Also, you have a problem with your j for loop. It only goes from 4 to 4.
Is this a correct way of initializing it?

It's still giving me some errors like "expect ; before }" and etc.. too many of them.

Edit: I fixed the j = 0;

1
2
3
4
5
	float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] = 
					{ { {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} } }
					{ {4.1, 4.2, 4.3}, {4.4, 2.5, 2.6}, {2.7, 2.8, 2.9}, {3.0, 3.1, 3.2} } };
Last edited on
Try this. I think you had two extra closing brackets and a missing comma.

1
2
3
4
5
	float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] = 
					{ { {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} },
					  { {4.1, 4.2, 4.3}, {4.4, 2.5, 2.6}, {2.7, 2.8, 2.9}, {3.0, 3.1, 3.2} } };
Last edited on
@squished18

Yes!

Now it Works!

Thank you so much..

My other question is, I am trying to find the average of all those elements in my 3D array.

For some reason, the output of Average is 0. Which means there is something wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float valueAverage(float Value[4][4][3])
	{
	int Length = 4 * 4 * 3;
	float sum = 0;
	float Average = sum/Length;

	for ( int i = 0; i < 4; i++ )
		for ( int j = 0; j < 4; j++ )
			for ( int k = 0; k < 3; k++ )
				{
				sum = sum + Value[i][j][k];
				}

			cout << "The Average of array is: " << Average << endl;

	return 0.0;

	}


Edit: Okay. I got it.

It should be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float valueAverage(float Value[4][4][3])
	{
	int Length = 4 * 4 * 3;
	float sum = 0;

	for ( int i = 0; i < 4; i++ )
		for ( int j = 0; j < 4; j++ )
			for ( int k = 0; k < 3; k++ )
				{
				sum = sum + Value[i][j][k];
				}

			float Average = sum/Length;
			cout << "The Average of array is: " <<  Average << endl;

	return 0.0;

	}
Last edited on
I think you should be able to figure this one out on your own. Here is a hint: what is the value of sum when you calculate Average?
Got it.

I edited my post above. It works perfectly.

Thanks guys for the help, I have one last question:

I am trying to write a function that finds The location (i, j, k) of the element that has the maximum value difference with its neighboring elements.

This is a picture of an example that may help:

http://i.imgur.com/INwyS4e.jpg

Can you guys give me any idea how to start this? I don't want a code. Just a simple idea and I will implement it.
Last edited on
Topic archived. No new replies allowed.