Thread: going through an array

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    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
    Last edited by fatsrir; 05-24-2010 at 01:01 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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];
             }
          }
    }

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    thank you friend! i used the debugger and it goes through all the array. thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM