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

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    6

    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. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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.
    Last edited by Matticus; 07-10-2012 at 03:29 PM.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    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. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    704
    Quote Originally Posted by Matticus View Post
    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. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by christop View Post
    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"

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have the code answer, already given to you, above.

    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. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    T_T T_ T still dont get it
    anyway thank you all 4 ur great exaplainations ~~

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find the smallest number. (Writing a subprogram)
    By november1992 in forum C Programming
    Replies: 13
    Last Post: 05-03-2012, 03:32 PM
  2. Replies: 6
    Last Post: 09-23-2010, 10:12 PM
  3. recursive find of the smallest member..
    By transgalactic2 in forum C Programming
    Replies: 35
    Last Post: 01-13-2009, 09:21 AM
  4. need to find smallest int
    By lankeveil in forum C Programming
    Replies: 3
    Last Post: 11-30-2008, 05:48 AM
  5. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM