c - Magnitude of numbers -
problem : determine number of pos, neg , zeros entered user.
code in c:
#include <stdio.h> void main() { int pos, neg, 0 = 0 ; char x[7] ; int num = 0 ; printf("\npress q quit anytime"); while( printf("\n") , gets(x)) { num = atoi(x) ; if( !strcmp(x , "q")) break ; if( num > 0) pos++ ; else if (num < 0) neg++ ; else zero++ ; } printf("\npos nos: %d \tneg nos: %d \tzeros: %d", pos, neg, zero); }
here, although neg , zeros being counted appropriately, positives not. shows positive numbers offset of 4196240 ie if there 4 positive numbers, show 4196244.
whats special 4196240? , why showing this?
you need initialize of counting variables. declaration initializes zero
, leaving pos
, neg
uninitialized.
int pos = 0, neg = 0, 0 = 0 ;
whats special 4196240 ? , why showing ?
it happens value when not initialized you, there's nothing special it, it's garbage value.
Comments
Post a Comment