1

I did the below program in C. It works fine up to number 4, if you type number 5 forward I'm getting Segmentation fault: 11 error? Why? I cannot find where is the error. Thank you.

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

int main(void)
{ 
  srand(time(NULL));
  
  int sum, i, input;
  int array [input];
  float average;
  sum = average = 0;
  int size = sizeof (array) / sizeof(array[0]);

  printf("Type the value of your array : ");
  scanf("%d", &input);

  printf("The size of your array is : %.2d \n", input);

  for (i = 0; i < input; i++)
  {
    array[i] = rand() % 100 + 1;
  }

  // loop for printing results

  for (i = 0; i < input; i++)
  {
    printf("Element %d; %d \n", i, array[i]);
  }
 
  for(i = 0; i < input; i ++){
    sum = sum + array[i];
  }

  average = (float)sum / i;
  printf("The average of array values is %.2f \n", average);
  
  
  return 0;
  };  
10
  • Are you compiling using the C99 specification? Commented Jul 4, 2020 at 19:28
  • What do you mean C99 specification? Commented Jul 4, 2020 at 19:30
  • How are you compiling your program? If you're using a command line, can you show your compilation command? Commented Jul 4, 2020 at 19:30
  • 3
    int input; int array[input]; is OH SO VERY WRONG! Commented Jul 4, 2020 at 19:31
  • 2
    @DanielWalker pmg is correct. In c99 you can have variable arrays as shown by your link. However, in this case ‘input’ hasn’t been initialized to anything. This is very bad... Commented Jul 4, 2020 at 19:40

1 Answer 1

3

The defects in the code:

  1. Used the variable input uninitialized to initialize an array.

  2. Attempted to get the size of the array which has incorrectly determined previously.

  3. The variable size is never used in the entire program (redundant declaration).

Note: I'll be using -std=c99 (C99 standard)

gcc -std=c99 -o main main.c; ./main

Code redefined (read the added comment to get the issue solved):

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

int main(void) {
    srand(time(NULL));

    int sum, i, input;
    float average;
    sum = average = 0;

    printf("Type the value of your array : ");
    scanf("%d", &input);

    int array[input]; // placing after defining of 'input'
    // int size = sizeof(array) / sizeof(array[0]); // unused variable

    printf("The size of your array is : %.2d \n", input);

    for (i = 0; i < input; i++)
        array[i] = rand() % 100 + 1;

    // loop for printing results

    for (i = 0; i < input; i++) {
        printf("Element %d; %d \n", i, array[i]);
        sum += array[i];
    }

    average = (float) sum / i;

    printf("The average of array values is %.2f \n", average);

    return 0;
};

This will output:

Type the value of your array : 10 
The size of your array is : 10 
Element 0; 6
Element 1; 14
Element 2; 66
Element 3; 73
Element 4; 19
Element 5; 14
Element 6; 62
Element 7; 78
Element 8; 31
Element 9; 31
The average of array values is 39.40
Sign up to request clarification or add additional context in comments.

1 Comment

Ohhh I got it. Awesome! Thank you so much @Rohan Bari

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.