-
going through an array
hello i have this piece of code
Code:
for(i=0; i<NUMROWS; i++) {
for(j=0; j<NUMCOLUMNS; j++)
if (tsafrir1[i][j]<tsafrir1[5][4]){
row=i;
col=j;
break;}break;}
the array consists of:"double" not "integer" numbers.
what i want it to do, is go through all the array and find me the smallest number, then put the row and column number inside row, col variables.
is this the right way to do it?
cuz i tried
Code:
tsafrir1[i][j]<tsafrir1
and that's a compile error.
does
Code:
(tsafrir1[i][j]<tsafrir1[5][4])
means his going through all the array? or just row5 col4?
thank you
-
You are always comparing to the number in the 6th row, 5th column of your array and as soon as you find one that is smaller (not necessarily the smallest) you immediately stop.
-
Instead of comparing to a specific value in your array (tasfir[5][4]) as you are now, you need to compare to whatever the current smallest value is, which you need to store outside the array in a temporary variable. Set your min value equal to say, the first element of the array, or the maximum value of a double, say, and then loop through the array and compare each array value to the current minimum. If the value is smaller than the minimum, update it.
What you don't want to do is break out of the loop as soon as you find a smaller one.
something like (this is pseudocode, probably won;t compile, just to show you the logic)
Code:
min = array[0][0];
for (i = 0; i < nrows; i++)
{
for (j = 0; j < ncols; j++)
{
if (array[i][j] < min)
{
min = array[i][j];
}
}
}
-
thank you friend! i used the debugger and it goes through all the array. thank you!