Johnny
Johnny

Reputation: 1

Getting an Array from user? C programming

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;
}

Upvotes: 0

Views: 3293

Answers (6)

Ash
Ash

Reputation: 186

Change scanf statement after for loop to

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

Upvotes: 0

M Oehm
M Oehm

Reputation: 29136

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;
}

Upvotes: 0

nalzok
nalzok

Reputation: 16157

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.

Upvotes: 0

Magisch
Magisch

Reputation: 7352

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.

Upvotes: 0

Karthick
Karthick

Reputation: 1000

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

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

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.

Upvotes: 3

Related Questions