Reputation: 307
First of all I'm not even sure whether you call it a member, couldn't think of a better term.
I'm trying to learn basics of debugging and arrays - so I wanted to create something resembling insert-sort from memory (so mistakes would be made) and then debug the program.
void findingsmaller (int *array, int num_inputs){
int a = 0;
int b = 1;
for ( b=1; b == num_inputs-1; b++ ) {
if ( array[a] > array[b] ) {
goleft(array, a, b);
a++;
}
}
}
Let's say we have this in array: 6 5 3 1 8 7 2 4
. array[a] should be 6
and array[b] should be 5
. 6 > 5
so we should enter the function that would find the first smaller number on the left of the array.
From my debugging session it seems like the condition is FALSE
so I don't enter goleft
at all. More specifically, Step into
ignores it, the testing printf wasn't executed either. I'm assuming the array comparison is not written properly, what's the correction?
WHOLE CODE if somebody wants to see other possible mistakes.
Thank you in advance!
EDIT: <= num_inputs
is correct, somehow I thought for
has (range1, range2, change)
instead of (start, condition, change)
. Anyway, now the problem seems that my goleft
function does its do-while
cycle one time too many although it shouldn't get past that condition.
EDIT2: A couple of other mistakes were fixed.
My printing in main
is now for( ; i <= num_inputs-1; )
My goleft
would do too many iterations due to the condition, fixed into ... while ( a >= 0 && array[a] > array[b] )
My findingsmaller
would only operate if the number next is smaller but does nothing when the number is greater. For example for 6 8
the program wouldn't function properly. Added else {a++}
My fixed code for anyone interested in the comparison of the changes.
Upvotes: 0
Views: 78
Reputation: 382
I was looking a little bit at your code and i notice something that doesnt work.
while (scanf("%d", &array[i]) != EOF)
As the documentation of the function scanf say :
The return value is EOF for an error
your while condition was making you num_imputs reaching 200 even if there was only 3 inputs. I would replace EOF by '\n'.
while (scanf("%d", &array[i]) != '\n') /* stop when the user input '\n' you
can replace with any character you want to make it stop. */
I did not make a lot of test but this should make your program work fine.
Upvotes: -1
Reputation: 473
The for loop is executed as long as the condition is True.
for ( ;Condition; )
{
// body
}
In your for loop, the condition is always False if the input is greater than 1.
Upvotes: 2
Reputation: 1117
Instead of b == num_inputs - 1
, you should put b < num_inputs
in your for
loop condition. Since the equality isn't true on the first iteration of the loop, it is immediately breaking.
Upvotes: 2