Originally Posted by
mrpeach
Furthermore, you clearly haven't read and understood the requirements stated in the parent post. The parent specifically states:
"I'm supposed to find the smallest value among all the values. After, finding the smallest value, I''m to find the second smallest value, next the third smallest value....etc"
Your "stupid" code WILL NOT do this as it can only return the smallest and next smallest, not the third most smallest etc..
MY code WILL. YOUR code WON'T.
Try reading for comprehension for a change. It might be enlightening.
*yawn*
Code:
#include <stdio.h>
#include <limits.h>
int smallerthan( int array[], int size, int thanthis )
{
int x, smallish = INT_MAX, index = -1;
for( x = 0; x < size; x++ )
{
if( array[x] < smallish && array[x] > thanthis )
{
smallish = array[x];
index = x;
}
}
return index; /* I'll return the index. */
}
int main( void )
{
int array[] = { 33, 77, 22, 44, 11, 99, 22, 55 };
int size = sizeof( array ) / sizeof( array[0] );
int small = -1;
int x;
/* You want it done by index? */
for( x = 0; x < size; x++ )
{
printf("Pass %d \'smallerthan( array, size, %d ) yields\' ", x + 1, small );
fflush( stdout );
small = smallerthan( array, size, small );
if( small != -1 )
small = array[ small ];
printf("%d\n", small );
}
return 0;
}
/*
Pass 1 'smallerthan( array, size, -1 ) yields' 11
Pass 2 'smallerthan( array, size, 11 ) yields' 22
Pass 3 'smallerthan( array, size, 22 ) yields' 33
Pass 4 'smallerthan( array, size, 33 ) yields' 44
Pass 5 'smallerthan( array, size, 44 ) yields' 55
Pass 6 'smallerthan( array, size, 55 ) yields' 77
Pass 7 'smallerthan( array, size, 77 ) yields' 99
Pass 8 'smallerthan( array, size, 99 ) yields' -1
*/
Since you're a natural born idiot, I've posted a full working example. Oh, and look, there's even a double value! Gee, I sure hope it doesn't break!
Now this is done returning the index, just in case they decide that's what they prefer. Knock out a few lines of code if you want to return the value rather than the index. Knock out even more lines if you don't want to display the output the way I chose to. The point is, it is trivial. A pair of loops is all it takes.
I don't know how on earth you think that can possibly fail if there are duplicate values. That's absurd. I guess back in what, third grade, you were asleep when they taught the concept of "less than".
Oh, and since you're so big on quotes, here's one:
Originally Posted by
AssistMe
I cannot sort the values.......that is why I'm so traumatized and being constantly haunted by this...........
I guess you missed that one. Building an index and sorting that is in effect sorting. The same would be if you created an array of pointers, each assigned to a location in the array and sorted that. You're still sorting, which apparently is not allowed.
Quzah.