can't get 3 lowest values to print correctly

I can't get my randomly generated multidimensional array to print out the 3 lowest numbers correctly. I thought I was onto something, but I guess not. It finds the lowest but for the 2nd and 3rd lowest it is always wrong. It will say the 2nd and 3rd lowest are 0 when the code didn't generate more than one zero. Everything works correctly except for the 2nd and 3rd lowest. Any hints or advice you could give me are appreciated! This is the section of code that is not working right. Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

	int seclow = test[0][0][0];

	for (int r = 0; r < arow; r++)
	{
		for (int c = 0; c < acol; c++) {
			for (int d = 0; d < adep; d++) {
				if ((seclow > test[r][c][d]) && (seclow > low))
				{
					seclow = test[r][c][d];
				}
			}
		}
	}

	int thirdlow = test[0][0][0];

Last edited on
What if test[][][] has 3 values that are all 1? seclow would have to ignore repeats of the lowest value. Is there some way of doing all 3 at the same time? Set low = seclow = thirdlow = 10 and use
1
2
3
4
5
6
7
8
9
10
11
12
If (thirdlow >= test[r][d][c])
    If (seclow > test [r][d][c])
        If (low > test[r][d][c])
            thirdlow = seclow
            seclow = low
            low = test [r][d][c]
        Else
            thirdlow = seclow
            seclow = test[r][d][c]
    Else
        thirdlow = test[r][d][c]
    

I made this up on the spot so I'm not sure if it works, but hopefully it helps.
Have you considered sorting the array?
no i havent considered sorting it because i havent learned how to do that. do you think it is more efficient?
update: PB i tried editing the code with your idea. It works some of the time but not all of the time. it usually messes up the third lowest number. however i do think im closer because it seems to be getting the first and second lowest right. thanks and please let me know if you have any ideas about the third lowest
nevermind guys I got it!! thanks again
Topic archived. No new replies allowed.