views:

790

answers:

4

Hi People :)

I have from 4 up to 20 variables that differ in size. They are all of type float and number values. Is there an easy way to find the smallest value among them and assign it to a variable? Thanks

+5  A: 

Not sure about objective-c but the procedure's something like:

float min = arrayofvalues[0];
foreach( float value in arrayofvalues)
{
    if(value < min)
        min=value;
}
Davy8
Languages change, algorithms do not. +1
Devin Jeanpierre
just got 213 errors :) There is no such function like foreach in obj-c that I'm aware of :(
CC
I don't know objective-c, but can replace "foreach" with a regular "for" loop. This assumes that all your variables are in a single array called arrayofvalues. From your comment I see that they are separate variables, so it'd be easier to first put them into an array. Otherwise you'll need
Davy8
a bunch of if statements. Either way, if you're unable to convert my example into something in your language of choice then I think you need to first take a look into an intro to programming book or article, as this is pretty fundamental stuff. Hope I don't come off as sounding mean.
Davy8
A: 

I agree with Davy8 - you could try rewriting his code into Objective C.

But, I have found some min()-like code - in Objective C!

Look at this:

- (int) smallestOf: (int) a andOf: (int) b andOf: (int) c
{
     int min = a;
     if ( b < min )
         min = b;

     if( c < min )
         min = c;

     return min;
}

This code assumes it'll always compare only three variables, but I guess that's something you can deal with ;)

Martin Janiczek
hmmm... But my number of variables change. Sometimes there is 4 and sometimes 20... but I like the idea... I could make an if function like this inside each of my parent if functions and solve it that way.. I'll try :D
CC
Well, if you know how many of them are there (can you keep track of them while creating them?), it would be handy to put them into an array and then iterating through it in simple FOR loop.You wouldn't need that FOREACH, because you'd specify "borders" of that array.
Martin Janiczek
A: 

The best solution, without foreach.

`- (float)minFromArray:(float *)array size:(int)arrSize

{

float min;
int i;

min = array[0]
for(i=1;i<arrSize;i++)
    if(array[i] < min)
        min = array[i];
return min;

} `

If you want to be sure, add a check of the arrSize > 0.

Marco

Marco
A: 

Thanks for all your answers and comments.. I learn a lot from you guys :)

I ended up using something like Martin suggested.

if (segmentValueNumber == 11) {

float min = 100000000;

    if(game51 > 0, game51 < min){
  min=game51;
 }

 if(game52 > 0, game52 < min){
  min=game52;
 }

...............................................

I could not figure out how to implement it all into one array since each result depends on a segment control, and I think the program is more optimised this way since it only checks relevant variables.

But thanks again, you are most helpful..

CC