Finding the minimum value of a line in a 3D array

Hello everyone,

I need a small help. I have a 3D array like X[a][b][c]. Each cell of this array holds a value. I need the b indice which corresponds to the minimum value. Let me explain it with an example
X[0][0][0]=2
X[0][1][0]=4
X[0][2][0]=5
X[0][3][0]=1

I need to find 3.

X[0][0][1]=0
X[0][1][1]=2
X[0][2][1]=5
X[0][3][1]=7 I need to find 0 here. I need to do that for all a and c.

and if there are more than one indice which corresponds the same minimum value, I should stock them in an array. Like

X[2][0][5]=10
X[2][1][5]=2
X[2][2][5]=2
X[2][3][5]=7

I should save 1 and 2.Thank you so much in advance


Last edited on
What have you tried so far? Do you know how to iterate through a multi-dimensional array with nested for loops?
Yes, I tried Following:
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
	
	for (int p = 0; p <a; ++p) {
		for (int t = 0; t < c; ++t) {
                      int min2 = 99999;
			for (int m = 0; m < b; ++m) {
				
				if (X[a][b][c] < min2) {
					min2 = X[a][b][c];

				}
			}


			//here we stock the b 
			
			vector<int>k;
			for (int m = 0; m < b; ++m) {
				if (min2 ==X[a][b][c])
				{
					k.push_back(b);
				}

			}
}
}
1
2
3
4
5
for (int p = 0; p <a; ++p) {

    if (X[a] < min2) {
        min2 = X[a];
    }

If first dimension has a elements, then X[a] is out of range error.

Furthermore, why have a loop, if you always dereference the same element: X[a][b][c]
In other words: You should be using your iteration variables (p, t, m) as indices.
It's more "idiomatic" to use (i, j, k) as for-loop variables if you don't have better names for them. Otherwise, usually you should avoid one-letter variable names.
Last edited on
There is more. You want minimum for each "line", don't you?

Since you have array int X[A][B][C]; you will have A*C "lines".
If you would keep only one index per line, then you would need array int min_index[A][C]; to store them.

You want potentially many indices per line. You would need, for example:
vector<int> min_indices[A][C];

For your example, the min_indices[2][5] would then be a vector<int> that contains values 1 and 2.
Thank you so much @keskiverto and @Ganado,

I just need to store one indice which is B.

I guess I did not understand your comments. For using the p,t,m. I did it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

	int min2 = 99999;
	for (int p = 0; p < nbProducts; ++p) {
		for (int t = 0; t < nbPeriods; ++t) {
			for (int m = 0; m < nbMachines; ++m) {
				
				if (X[p][m][t] < min2) {
					min2 = lagpart2[p][m][t];

				}
			}
}
}



here , I try to find the minimum value for each X[p][t]. It should iterate on m and find the minimum of it. So, the compared X[p][m][t] should be like

X[2][0][5]
X[2][1][5]
X[2][2][5]
X[2][3][5]

Thank you so much for your help.
Those are better names. You code, however, looks for the smallest value of the entire array.

This is the part that iterates every possible pair of (p,t):
1
2
3
4
5
for (int p = 0; p < nbProducts; ++p) {
  for (int t = 0; t < nbPeriods; ++t) {
    // code X
  }
}

The code X should find the answer for one pair. (2,5) is one such pair.

To get minumum "cell values" for each pair:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int results[nbProducts][nbPeriods] {};
for (int p = 0; p < nbProducts; ++p) {
  for (int t = 0; t < nbPeriods; ++t) {

    int min2 = 99999;
    for (int m = 0; m < nbMachines; ++m) {
      if ( X[p][m][t] < min2 ) {
        min2 = lagpart2[p][m][t];
      }
    }
    results[p][t] = min2;

  }
}

Thank you so much @keskiverto. However, I need the middle indice of the minimum value. I need the "m" indices. Your code holds p and t , does not it ? I solved the problem thank you

Last edited on
Topic archived. No new replies allowed.