1

My objective is to sort out an array of numbers that are in a txt file called "3_1.txt". I have implemented code in C Lang to sort the numbers out called "sort.c". This is an assignment for school I have been working on but cannot seem where I am going wrong. The only reason I think something is NOT correct is because on the GitHub classrooms feedback / debug says the following --> Errorsort.c: run dynamic test ::error::Error: Exit with code: 1 and signal: null

Is there something I'm missing?

sort.c In C Language:

#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
Kenster
  • 23,465
  • 21
  • 80
  • 106
  • Did you specify the file name when you was running the program? – Vlad from Moscow Sep 15 '20 at 22:49
  • What was the actual result if you run the code directly? – kaylum Sep 15 '20 at 23:32
  • Also, are you confident that the test input is always exactly `22` numbers? Magic numbers like that in code are usually not good practice and in this case would cause problems if the input contains more or less than 22 numbers. – kaylum Sep 15 '20 at 23:33
  • @VladfromMoscow yes I have. Tell me if I'm wrong... I used _./sort.out 3_1.txt_. – Cody DOrazio Sep 15 '20 at 23:36
  • @kaylum The issue with the magic number of 22 is that the provided test file 3_1.txt is exactly 22 integers. Currently the program works specifically for that exact amount of integers. Is there a way this code can refactored with either a dynamic array using malloc() or a variable length array? – Cody DOrazio Sep 15 '20 at 23:48
  • Yes there are multiple ways to do that. One way is to `malloc` an initial fixed size array and then use `realloc` to increase the size when the existing size is exceeded. I suspect that the "dynamic test" in the error msg may be alluding to a test case that requires dynamic allocation - but even if not it is much better to do it that way. – kaylum Sep 15 '20 at 23:51
  • Thank you @kaylum! I ended up getting the entire program to work. Much appreciated. – Cody DOrazio Sep 21 '20 at 21:20
  • With the solution I created, I left the final project as the answer to this thread if anyone would like to see what I did. – Cody DOrazio Sep 21 '20 at 22:26

1 Answers1

0

The full repository is posted as a public repo here if anyone would like to see what I did to my sort.c file. Thanks for everyones help!