How to find the smallest number in an array ? -
NeXoR - 30.09.2016
So, I have been told to find the smallest number in an array, that's what I did
PHP код:
// Task 1
new biggest = 0;
for(new i = 0; i < sizeof(Arr); i++) if(Arr[i] > biggest) biggest = Arr[i];
printf("The highest number is: %d", biggest);
// Task 2
new smallest = biggest;
for(new i = 0; i < sizeof(Arr); i++) if(Arr[i] < smallest) smallest = Arr[i];
printf("The smallest number is: %d", smallest);
And my fellow scripter told me that there is another way to do it, instead of run two loops I can run only one
And he gave me a hint to use hexa numbers, anyone's got a clue ?
Re: How to find the smallest number in an array ? -
Vince - 30.09.2016
Not sure why you would need hexadecimal numbers, except perhaps to represent the highest and lowest possible numbers. After a while you know those by heart: 0x7FFFFFFF is the largest possible number that can be stored in a 32-bit unsigned integer. This is equal to roughly 2.14 billion. Likewise, 0x80000000 is the smallest possible number, equal to -2.14 billion.
And yes, if you need to find both lowest and highest then you can indeed bundle them in a single loop.
Re: How to find the smallest number in an array ? -
SickAttack - 30.09.2016
pawn Код:
new array[] = {3, 7, 2, 4, 5, 8, 2, 9, 2, 1, 3, 5, 6, 7}, highest, lowest = 9999;
for(new i = 0; i < sizeof(array); i ++)
{
if(array[i] > highest)
{
highest = array[i];
}
else if(array[i] < lowest)
{
lowest = array[i];
}
}
printf("Highest: %d", highest);
printf("Lowest: %d", lowest);
Re: How to find the smallest number in an array ? -
NeXoR - 30.09.2016
Quote:
Originally Posted by SickAttack
pawn Код:
new array[] = {3, 7, 2, 4, 5, 8, 2, 9, 2, 1, 3, 5, 6, 7}, highest, lowest = 9999; for(new i = 0; i < sizeof(array); i ++) { if(array[i] > highest) { highest = array[i]; } else if(array[i] < lowest) { lowest = array[i]; } }
printf("Highest: %d", highest); printf("Lowest: %d", lowest);
|
That's the point, I have been asked to get the lowest number, without stating a number beforehand (without lowest = 9999) so it can get the lowest number no matter what the highest is.
Do you know a way to do it ?
I mean doing the Task 2 point without using task1 or stated variables (lowest = X)
Re: How to find the smallest number in an array ? -
SickAttack - 30.09.2016
Quote:
Originally Posted by NeXoR
That's the point, I have been asked to get the lowest number, without stating a number beforehand (without lowest = 9999) so it can get the lowest number no matter what the highest is.
Do you know a way to do it ?
I mean doing the Task 2 point without using task1 or stated variables (lowest = X)
|
pawn Код:
new array[] = {3, 7, 2, 4, 5, 8, 2, 9, 2, 1, 3, 5, 6, 7}, lowest = array[0];
for(new i = 1; i < sizeof(array); i ++)
{
if(array[i] < lowest)
{
lowest = array[i];
}
}
printf("Lowest: %d", lowest);
You have to initialize the variable, because it starts at 0. Now it should work regardless of what the highest number is.
Re: How to find the smallest number in an array ? -
NeXoR - 30.09.2016
Quote:
Originally Posted by SickAttack
pawn Код:
new array[] = {3, 7, 2, 4, 5, 8, 2, 9, 2, 1, 3, 5, 6, 7}, lowest = array[0]; for(new i = 1; i < sizeof(array); i ++) { if(array[i] < lowest) { lowest = array[i]; } }
printf("Lowest: %d", lowest);
You have to initialize the variable, because it starts at 0. Now it should work regardless of what the highest number is.
|
That should make it, I would have repped you if I could I just did it too soon xD
Re: How to find the smallest number in an array ? -
Jayse - 30.09.2016
I know you fixed it already, but I would like to share this for anyone who's looking for a function like this (not expecting anything)
PHP код:
returnLowestValue( arr[ ], len ) {
new val = 0x7FFFFFFF;
for( new i; i < len; i ++ ) if( arr[ i ] < val ) val = arr[ i ];
return val;
}
Re: How to find the smallest number in an array ? -
SickAttack - 30.09.2016
Quote:
Originally Posted by Jayse
I know you fixed it already, but I would like to share this for anyone who's looking for a function like this (not expecting anything)
PHP код:
returnLowestValue( arr[ ], len ) {
new val = 0x7FFFFFFF;
for( new i; i < len; i ++ ) if( arr[ i ] < val ) val = arr[ i ];
return val;
}
|
That's what Vince explained. I guess it's better to just assign the first value of the array to the variable since it will cut one cycle off the loop.
Re: How to find the smallest number in an array ? -
NeXoR - 30.09.2016
Quote:
Originally Posted by Jayse
I know you fixed it already, but I would like to share this for anyone who's looking for a function like this (not expecting anything)
PHP код:
returnLowestValue( arr[ ], len ) {
new val = 0x7FFFFFFF;
for( new i; i < len; i ++ ) if( arr[ i ] < val ) val = arr[ i ];
return val;
}
|
I'll take a note of it, thanks.