0

I'm trying to write a simple program that'll prompt the user to enter N numbers, store them in an array, then just sum them all up

I understand I can just do this with a recursion but I'm trying to learn how array works

Example:

1 (hit enter) 2 (hit enter) ... 10 (hit enter)

Expected output: 55

#include <stdio.h>

int main (void){
  int n;
  int a[n];
  int counter;

  printf("How many numbers do you want to enter? \n");
  scanf("%d", &n);

  printf("OK! now enter your number: \n");
  for (int i = 0; i <= n; i++){
    scanf("%d", &a[i]);
    counter =+ a[i];
  }

  printf("The answer is: %d\n", counter);
  return 0;
} 

Right now there's no error message, no output, just the standard windows error message "scanner.exe has stopped working..."

I'm using Win8 and GCC compiler

2
  • answers , and i <= n --> i < n , =+ --> += Commented Jan 28, 2014 at 22:40
  • Please condense and improve indentation on your code. Commented Jan 28, 2014 at 23:36

3 Answers 3

3

First of all, you can't create an static array without first knowing its size. You first need to ask the user for the "n" variable and then declare your array.

You also need to explicitly initialize your counter variable to be zero before you start counting. In C, variables don't default to 0 when you declare them.

The operator "=+" doesn't exist AKAIK, change it to "+=".

Last but not least, the limit in your loops is a little off, you're asking for 11 values ;) (I edited this post, I was wrong about only asking for 9 values. I tend to confuse that sort of stuff)

#include <stdio.h>

int main (void){
  int n;
  int counter = 0;

  printf("How many numbers do you want to enter? \n");
  scanf("%d", &n);

  int a[n];

  printf("OK! now enter your number: \n");
  for (int i = 0; i < n; i++){
    scanf("%d", &a[i]);
    counter += a[i];
  }

  printf("The answer is: %d\n", counter);
  return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@abelenky yeah, but I think OP wants to accumulate the value, not assign each element to the array without a sum.
@abelenky Still, it is valid C syntax. Thanks for pointing that out =)
@userXxXxXx You're welcome, tell me if it works for you.
2

You are using variable length arrays. At run time the value of n must be known. Place the declaration

int a[n];  

after taking input for n, i.e, after scanf("%d", &n); and initialize counter to zero before using it otherwise you will get garbage value (because of undefined behavior).
Also change the for loop condition from i <= n to i < n.

4 Comments

How will n be known at compile time if n is dependent on user input?
@ValekHalfHeart; That was a typo.
It doesn't really matter; the first printf should run before this bug is triggered.
In addition, the condition in the for-loop should be i < n, not i <= n.
0

After this line:

int n;

What do you think the value of n is?

Now go to the next line:

int a[n];   

How big is this array?
Can you access it properly?

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.