0

My console keeps on crashing after entering a few numbers. I am trying to get an array of 10 numbers from the user thru the console and then taking count of positives, negatives, evens, and odds. What am I doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int pos, neg, even, odd;
int nums[10];
printf("Give me 10 numbers: ");


pos = neg = even = odd = 0;
for(int i = 0; i < 10; i++){
    scanf(" %d", nums[i]);
    if(nums[i] > 0){
        pos++;
        if(nums[i] % 2 == 0){
            even++;
        }
        else{
            odd++;
        }
    }
    else{
        neg++;
    }
}

printf("Positives: %d, Negatives: %d, Evens: %d, Odds: %d\n", pos, neg, even, odd);
    return 0;
}
2
  • 1
    Didn't you get any warnings about how you use scanf? Commented Feb 1, 2016 at 7:52
  • Also, your negative numbers aren't counting towards your even or odd count. Move your modulo one if out. And 0 will count as odd by your logic above (remember, 0 is neither positive nor negative, it will need its own case). Commented Feb 1, 2016 at 8:05

6 Answers 6

3

In your code,

 scanf(" %d", nums[i]);

should be

scanf(" %d", &(nums[i]));

or,

scanf(" %d", nums+i);

as you need to pass the pointer to variable as the format specifier's argument in scanf() .

To elaborate, %d expects a pointer to int and what you're supplying is an int variable. it invokes undefined behavior.

That said,

  1. Always check the return value of scanf() to ensure proper scanning.
  2. int main() should be int main(void) to conform to the standard.
Sign up to request clarification or add additional context in comments.

9 Comments

... or just pass num + i. Which at least is fewer typing.
You don't need the extra parentheses in scanf(" %d", &(nums[i])); - it can just be scanf(" %d", &nums[i]);.
@Paul R Curious you mentioned no need for () yet did not mention about no need for " " in " %d". --> scanf("%d", &nums[i]);
@chux: yes, maybe not needed in this instance, but often useful for flushing trailing newlines or other white space from previous reads.
@Paul R scanf("%d", &nums[i]); flushes "trailing newlines or other white space from previous reads" even without the leading" " in the format.
|
0

Modify scanf like scanf(" %d", &nums[i]);

Comments

0
scanf(" %d", nums[i]);

Scanf expects a pointer to a location to write to, and you're not giving it one.

Change your scanf to:

scanf(" %d", &(nums[i]));

to make your program work.

With this change I tested your program with stdin of

20 10 9 1 39 1 2 2 31 1

And recieved output:

Give me 10 numbers: Positives: 10, Negatives: 0, Evens: 4, Odds: 6

ideone of the thing for your testing purposes.

2 Comments

You don't need the extra parentheses in scanf(" %d", &(nums[i])); - it can just be scanf(" %d", &nums[i]);.
@PaulR Thats true. I usually like to put them anyways, but it works without.
0

Change scanf(" %d", nums[i]); to scanf(" %d", &nums[i]);, because scanf() needs addresses. The parentheses around nums[i] isn't necessary, and may effect readability.

Also note that 0 is even, but not negative.

Comments

0

When scanf is usedto convert numbers, it expects a pointer to the corresponding type as argument, in your case int *:

scanf(" %d", &nums[i]);

This should get rid of your crash. scanf has a return value, namely the number of conversions made or the special value EOF to indicate the end of input. Please check it, otherwise you can't be sure that you have read a valid number.

When you look at your code, you'll notice that you don't need an array. Afterreading the number, you don't do aything with the array. You just keep a tally of odd, even and so on numbers. That means you just need a single integer to store the current number. That also extends your program nicely to inputs of any length.

Here's a variant that reads numbers until the end of input is reached (by pressing Ctrl-D or Ctrl-Z) or until a non-number is entered, e.g. "stop":

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int count = 0;
    int pos = 0;
    int neg = 0;
    int even = 0;
    int odd = 0;

    int num;

    while (scanf("%d", &num) == 1) {
        count++;

        if (num > 0) pos++;
        if (num < 0) neg++;

        if (num % 2 == 0) even++;
        if (num % 2 != 0) odd++;
    }

    printf("%d numbers, of which:\n", count);
    printf("    %d positive\n", pos);
    printf("    %d negative\n", neg);
    printf("    %d even\n", even);
    printf("    %d odd\n", odd);

    return 0;
}

Comments

0

Change scanf statement after for loop to

scanf(" %d", &nums[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.