问题描述
我的目标是整理一个名为“ 3_1.txt”的txt文件中的数字数组。我已经在C语言中实现了将数字排序为“ sort.c”的代码。这是我一直在工作的学校的一项作业,但似乎无法确定我要去哪里。我认为不正确的唯一原因是因为在GitHub教室上, Feedback / debug 表示以下内容-> 错误 ❌ sort.c:运行动态测试 :: error :: Error:以代码1退出并发出信号:空
有什么我想念的吗?
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
解决方法
如果有人希望查看我对sort.c文件所做的操作,则完整的存储库将作为公共仓库here发布。谢谢大家的帮助!