PDA

View Full Version : How to find smallest??

Beckie
May 6, 2005, 11:57 PM
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;
}

HexMonkey
May 7, 2005, 12:38 AM
if (smallest > list[row].games[col])
{
list[row].games[col] = smallest;
}

Shouldn't this assignment be the other way around? Try this instead:

if (smallest > list[row].games[col])
{
smallest = list[row].games[col];
}

Beckie
May 7, 2005, 04:31 PM
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

HexMonkey
May 7, 2005, 05:32 PM
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.

Beckie
May 8, 2005, 12:21 PM
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