我的目标是整理出一个名为“3_1.txt”的txt文件中的数字数组。我已经在 C 语言中实现了代码来对称为“sort.c”的数字进行排序。这是我一直在做的学校作业,但似乎看不出我哪里出错了。我认为某些事情不正确的唯一原因是因为在 GitHub 教室反馈/调试中说以下内容 --> 错误 ❌ sort.c: 运行动态测试 ::error::Error: Exit with code: 1 and signal: null
有什么我想念的吗?
sort.c在 C 语言中:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* The following code is supposed to sort the .txt file
when ran through the terminal. */
int main(int argc, char*argv[]){
int numbers[22];
int i;
int n = sizeof(numbers)/sizeof(numbers[0]);
FILE *myFile;
myFile = fopen(argv[1], "r");
if(myFile == NULL){
printf("Error reading file\n");
exit (0);
}
for(i = 0; i < 22; i++){
fscanf(myFile, "%d", &numbers[i]);
}
selectionSort(numbers, n);
printArray(numbers, n);
fclose(myFile);
return 0;
}
void swap(int *xs, int *ys){
int temp = *xs;
*xs = *ys;
*ys = temp;
}
void selectionSort(int arr[], int n){
int i, j, min_idx;
for (i = 0; i < n-1; i++){
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size){
int i;
for (i = 0; i < size; i++){
printf("%d ", arr[i]);
}
}
// EOF
3_1.txt
14 15 6
23 20
5 10
67 80
1 5 7 3 4
54 55
96
8
12
37 25 37