gpt4 book ai didi

c - C 中的冒泡排序函数 - undefined symbol 错误

转载 作者:行者123 更新时间:2023-11-30 17:38:44 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序来对整数数组进行冒泡排序。我收到错误:

Segmentation Fault

我知道这通常与链接正确的库有关,但我不确定我缺少哪个库?

这是我的程序:

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

感谢您的帮助!

最佳答案

您调用函数bubble_sort(),而它实际上名为bubblesort()。您还必须在 main 函数之前创建一个定义,例如添加:

void bubblesort(int list[], int N)

位于 int *allocatearray(int N) 定义下方。

关于c - C 中的冒泡排序函数 - undefined symbol 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com