c - 排序函数不会打印排序数组?
问题描述
我的排序函数不会打印排序后的数组?
我正在尝试编写一个程序来收集数组元素,对数组进行排序,然后打印每个元素的阶乘。如果数组未正确排序,我不想超越自己并编写递归函数。对我来说,那种感觉很好;人们批评我使用 while 循环,但我还不知道另一种方法。任何输入表示赞赏。
#include <stdio.h>
int sorter(int numbList[]);
int getData(int numList[]);
//int recursive(int numList[]);
int main(void) {
int x;
int numberList[x];
getData(&numberList[x]);
sorter(&numberList[x]);
//recursive(&numberList[x]);
return 0;
}
//gets user input-data
int getData(int numbList[]) {
int i;
int x;
printf("Enter number of Elements:\n");
scanf("%d", &x);
printf("Enter the values for each element starting from first
element:\n");
for (i = 0; i < x; i++) {
scanf("%d", &numbList[i]);
}
printf("\nYou have filled the array list with\n");
for (i = 0; i < x; i++) {
printf("%d\n", numbList[i]);
}
return numbList[x];
}
//sorter function
int sorter(int numbList[]) {
int x;
int temp;
int swapped;
while (1) {
swapped = 0;
for (int i = 0; i < x; i++) {
if (i > numbList[i + 1]) {
temp = numbList[x];
numbList[x] = numbList[x + 1];
numbList[x + 1] = numbList[x];
swapped = 1;
}
if (swapped == 0) {
break;
}
}
printf("Array as sorted:\n");
for (int i = 0; i < x; i++) {
printf("%d\t", numbList[x]);
}
return(numbList[x]);
}
}
//recursive factorial function
/* int recursive(int numbList[]) {
int b = 0;
numbList[b] *= numbList[b - 1];
return 0;
} */
解决方案
您的代码中有多个问题:
x
定义数组时元素的数量未初始化numbList[x]
。这具有未定义的行为。你应该传递一个指向计数的指针,getData
这个函数应该更新这个值,分配数组,读取值并返回一个指向数组的指针。你不应该在没有a的情况下打破多行的字符串
\
交换码坏了:测试
if (i > numbList[i + 1])
不正确,应该是if (numbList[i] > numbList[i + 1])
- 交换代码应该使用
i
而不是x
作为索引,并且交换代码中的最后一个分配应该存储temp
到numbList[i + 1]
. - 内部循环应该停止
x - 1
以避免读取超出数组的末尾。 - 你应该让内循环运行到最后并从外循环中断 if
swapped == 0
。
这是一个更正的版本:
#include <stdio.h>
#include <stdlib.h>
int *getData(int *count);
void sorter(int numList[], int count);
int main(void) {
int x;
int *numberList;
numberList = getData(&x);
if (numberList != NULL) {
printf("Elements entered:");
for (int i = 0; i < x; i++) {
printf(" %d", numberList[i]);
}
printf("\n");
sorter(numberList, x);
printf("Sorted array:");
for (int i = 0; i < x; i++) {
printf(" %d", numberList[i]);
}
printf("\n");
free(numberList);
}
return 0;
}
//gets user input-data
int *getData(int *countp) {
int i, x;
int *numbList;
printf("Enter the number of elements: ");
if (scanf("%d", &x) != 1 || x <= 0) {
printf("Invalid size:");
return NULL;
}
numbList = calloc(sizeof *numbList, x);
if (numbList == NULL) {
printf("Memory allocation error:");
return NULL;
}
printf("Enter the element values: ");
for (i = 0; i < x; i++) {
if (scanf("%d", &numbList[i]) != 1) {
free(numbList);
return NULL;
}
}
*countp = x;
return numbList;
}
//sorter function
void sorter(int numbList[], int x) {
for (;;) {
int swapped = 0;
for (int i = 0; i < x - 1; i++) {
if (numbList[i] > numbList[i + 1]) {
int temp = numbList[i];
numbList[i] = numbList[i + 1];
numbList[i + 1] = temp;
swapped = 1;
}
}
if (swapped == 0) {
break;
}
}
}
推荐阅读
- typescript - 为什么 isElementPresent() 在我的量角器脚本中不起作用?
- vuejs2 - 如何显示两个相邻的 v-navigation-drawer 并能够从左侧隐藏右侧?
- intellij-idea - 如何在 Intellij 插件中持久/回读运行配置参数
- c# - SQLite 在重建时删除参考
- sql - 合并具有相同值的行的日期范围
- java - 奇怪的安卓异常
- javascript - 如何从 Firebase 获取数据以创建列表?
- ruby - 复选标记字符 = "\u2713" - 打印输出不一致
- python - 替换 Excel 表格中的数据并保存表格
- java - 如何在 Java 中向 GridLayout 添加多个 TextView?