# Thread: find smallest non-value in an 2-d array !! need help :D

1. ## find smallest non-value in an 2-d array !! need help :D

hi guys, i need to find the smallest non-zero value in each row and print them to the screen.
for instance this case will be
2, 15, 49, 11. I've been thinking bout this for quite long time, can anyone give me some hints here plss ?

Code:
```int table [5][5] =
{
{10, 0,  2, 23,  0},
{0, 15,  0, 45, 35},
{0, 49, 89,  0, 59},
{0, 11, 0, 52,  22}
};

int row, col, smallest;

for (row = 0; row < 5; row++)
{
for (col = 0; col < 5; col++)
{
if (table[row][col] != 0)
smallest = table[row][col];
}
printf("%d, ", smallest);
}
// my incomplete code so far :D```

2. You are updating "smallest" every time the value is not zero. Instead, you should compare the current value in the array with "smallest" and, if it is less than "smallest," update "smallest" with that value. Be sure "smallest" is initialized before using it for comparison!

Also, in the 2D array, it should be "variable[col][row]" so that the order of the values flow natural from left to right (row) before continuing to a new line (column) and printing the next row. The inner loop should represent the "row" as per the requirements of your assignment.

3. Code:
```for (col = 0; col < 5; col++)
{
for (row = 0; row < 5; row++)
{
if (table[col][row] != 0)
smallest = table[col][row];
if (smallest < table[col][row])
smallest = table[col][row];
}
printf("%d, ", smallest);
} // is this what you meant, i dont have the IDE now but pretty sure its not gonna work :(```

4. Originally Posted by Matticus
Also, in the 2D array, it should be "variable[col][row]" so that the order of the values flow natural from left to right (row) before continuing to a new line (column) and printing the next row. The inner loop should represent the "row" as per the requirements of your assignment.
Actually his code looks OK on this point. The first dimension is the row and the second is column. He's iterating over the array in row-major order.

5. Originally Posted by christop
Actually his code looks OK on this point. The first dimension is the row and the second is column. He's iterating over the array in row-major order.
Ah yes, you are correct. I was totally seeing that wrong. My apologies to the OP.

---

Code:
```if (table[col][row] != 0)
smallest = table[col][row];  // smallest is now equal to that value ...
if (smallest < table[col][row])  // ... so it cannot be less than that same value
smallest = table[col][row];```
I think you're looking for something like this:

Code:
```if( something && something )
// update "smallest"```

Regarding the logic to use in your program: why not use the same logic you would use?

If I gave you a sack with 8 apples in it, I'll bet you could quickly figure out which apple was the smallest! Go through the mental (or actual) exercise a few times, and you'll begin to see the patterns you used to select the smallest apple.

That pattern that emerges, will have the logic you need to make your program work. Jot the steps down, and that's your program's "core" logic, right there. A lot of programming involves practical problem solving - and practice really will improve your skill in this vital area.

7. T_T T_ T still dont get it
anyway thank you all 4 ur great exaplainations ~~

8. Put 5 apples onto a table with a saucer under each one. Now put 3 saucers more, just empty, on the table also.

Now how would you find which apple was the smallest one?

Would you look at each apple? Sure.

After you found the first apple, you'd have to compare the second apple, with the first one, to see which one was smaller, wouldn't you?

Then the first apple you look at, could be considered the "smallest apple SO FAR", and all the other apples would have to be compared with the "smallest apple SO FAR", to see which one really was the smallest one.

It's best to do this exercise, when you're not hungry!