简体   繁体   中英

bubble sort function in C - undefined symbols error

I'm trying to write a simple program to bubble sort an array of integers. I'm getting the error:

Segmentation Fault

I know this normally has to do with linking with the correct library but I'm not sure what one I'm missing?

Here's my program:

 //Bubble Sort program

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

void bubblesort(int list[], int N);



//To do: write a function to allocate an array of size N. N will be input by the user

//TODO:Write a function allocatearray()
int *allocatearray(int N){
  int *array;                  
  array = malloc(N * sizeof(int));    //making an array of changeable size
  return array;
}

int main()  {
  int *array;
  int n, i, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (i = 0; i < n; i++)
    //scanf("%d", &array[i]); 
      scanf("%d", &array[i]);  
      bubblesort(array, n);

  printf("Sorted list in ascending order:\n");
  for ( i = 0 ; i < n ; i++ ) {
     printf("%d\n", array[i]);
     }

  return 0;
}

//TODO:Write a function bubblesort()
void bubblesort(int list[], int N) {
  int i, d, t;
  for (i = 0 ; i < (N - 1); i++) {        //array of entered integers
    for (d = 0 ; d < (N - i - 1); d++) {  //d array is one smaller than c
        if (list[d] > list[d+1]) {            //if value "d" is greater than "d + 1"
        /* Bubble swap */
        t = list[d];                     //create new long variable t equal to value of list[d]
        list[d] = list[d+1];             //update d value to d + 1 value
        list[d+1] = t;                   //
        }
       }
      }
     }

Thanks for the help!

You call function bubble_sort() while it is actually named bubblesort() . You also have to create a definition before main function, for example add:

void bubblesort(int list[], int N)

right below int *allocatearray(int N) definition.

You are trying to use function bubble_sort in your main function but it is not declared anywhere, thus compiler complains it cannot find this method.

Even if this is only a typo mistake, calling bubblesort won't work too because main function is located upper bubblesort , thus compiler will complain that it does not know any method named bubblesort . You need either to put the bubblesort method before the main one, or add a prototype line before your main :

//declare the signature of the method, the implementation is found below.
void bubblesort(int list[], int N);

You're trying to use your function bubblesort in main without declaring it at the top of your c source file.

You have two ways to follow:

  1. Cut and paste bubble sort function to the top:

     #include <stdio.h> #include <stdlib.h> //To do: write a function to allocate an array of size N. N will be input by the user //TODO:Write a function allocatearray() int *allocatearray(int N){ int *array; array = malloc(N * sizeof(int)); //making an array of changeable size return array; } void bubblesort(int list[], int N) { int i, d, t; for (i = 0 ; i < (N - 1); i++) { //AND ON... int main() { // ... } 
  2. Just add function definition at the top:

     void bubblesort(int list[], int N); 

    Right below #includes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM