07.02.2012, 15:31
What is the best way to compare n integers and get the largest one?
if(int1 > in2) //This is saying if int 1 is bigger...
if(int1 < int2) // this is saying if Int 2 is bigger Its jst simple maths ![]() |
new
max_int = 0, // The variable will be the largest integer.
x = 1, // 1st integer.
y = 5, // 2nd integer.
z = 10; // 3rd integer.
// if x is higher than 0, then the max is the x's value
if( x > max_int )
{
max_int = x; // max = x = 1
}
else if( y > max_int ) // the same happens with the y variable too. If y is higher than max then max = y's value, else it skip this and continue..
{
max_int = y; // max = y = 5
}
else if( z > max_int ) // It's check if the z value is higher than the max and if it is, max takes z's value..
{
max_int = z; // max = z = 10
}
printf( "The max is: %i", max_int ); // It will print in the console the higher number
new Value[]={84,257,24,8,258,42,7,81,67,456,812,588,12,1,815,46};
new MaxValueYet;
new MaxValueInCell;
for(new c=0;c<sizeof(Value);c++)
{
if(Value[c]>MaxValueYet)
{
MaxValueInCell=c;
//MaxValueYet=Value[c];//its the same as:
MaxValueYet=Value[MaxValueInCell];
}
}