0

1) In CS50 ide, it does not even compile giving error: format specifies type

'int *' but the argument has type 'int' [-Werror,-Wformat]
        scanf (" %d", array[n]);

2) In codeblocks, it runs and I am able to read in to howMany variable but then gives seg fault after it asks for the first number to be read

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

int main(void)

{

    int n, howMany;
    float total = 0;
    float average = 0;

    printf(" How many numbers you want to Enter\n");
    scanf(" %i", &howMany);

    int array[howMany];

    for (n=0; n < howMany; n++)
    {
        printf("enter a number: ");
        scanf (" %d", array[n]);
        total += array[n];
    }
    average = total/howMany;
    printf ("average of all numbers entered is %f\n", average);


}

1 Answer 1

0

Ok got it, I had to write &array[n], I had skipped it thinking array is already a pointer.

1
  • The array is a pointer, yet its element is not. As you seem to have solved your problem yourself, you might want to accept your own answer. You won't get points for that, but it helps keep some lists clean.
    – Blauelf
    Commented Mar 14, 2017 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .