How to find smallest??

Discussion in 'Mac Programming' started by Beckie, May 6, 2005.

  1. Beckie
    Expand Collapse
    macrumors newbie

    Joined:
    Apr 6, 2005
    #1
    for some reason, I cannot get my function for finding the smallest number to work. I have one almost identical to this for finding the largest number and it works perfectly, and so I just switched from < to > and it isn't working. It's really strange. If you could help me out on this, I would greatly appreciate it.

    Thank you so much!!
    Take care,
    Beckie

    {
    int row;
    int col;
    int smallest;
    find smallest in each array of list

    for (row = 0; row < 20; row++)
    {
    smallest = list[row].games[0];

    for (col = 1; col < 15; col++)

    if (smallest > list[row].games[col])
    {
    list[row].games[col] = smallest;
    }
    list[row].findlow = smallest;
    col = 0;
    }
     
  2. HexMonkey
    Expand Collapse
    Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    Shouldn't this assignment be the other way around? Try this instead:

    Code:
    if (smallest > list[row].games[col])
    {
       smallest = list[row].games[col];
    }
     
  3. Beckie
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Apr 6, 2005
    #3
    Hi
    Thanks for helping.

    I tried that too before and it doesn't work. It puts everything at zero.

    It's really strange.

    Well anyway, thanks for helping!

    Take care,
    Beckie
     
  4. HexMonkey
    Expand Collapse
    Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #4
    I tried it and it worked perfectly for me after making the above change. Perhaps there's a bug somewhere else in your code, such as where you set the values in the arrays.
     
  5. Beckie
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Apr 6, 2005
    #5
    Thanks again HexMonkey for helping me with my program. You're the best!!!

    Just in case someone needs to know how to do this in a program, the thing that was wrong with my find the smallest thing was that I had zeros in my array and I needed to change list[row].games[col] = smallest; to smallest = list[row].games[col]; so it was finding the zeros as being the smallest, logically so. So anyway, I had to make some adjustments and it works now.

    So if anyone wants to use that, it works. :)

    Take care,
    Beckie
     

Share This Page