Search code examples
cstringsortingcommand-lineatoi

C Program, string to int array command line sort


This program is supposed to sort numbers entered from the command line as -a for small to large sort, or -d for large to small sort. I compiled this program earlier, ran it and the output was fine. I tested it on my laptop later and then the sorting was all wasn't working correctly.

I entered ./sort -a 5 14 10 18 20 2 100 6 7 1
The Result: -1950355064 2 5 6 10 14 18 20 100

I entered: ./sort -d 5 14 10 18 20 2 100 6 7 1
The Result: 761262572 32767 18 14 10 6 20 5 100 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

#define N 10

// function declarations
void small_to_large(int a[], int n);
void large_to_small(int a[], int n);

int main(int argc, char *argv[])
{
    int i;
    char letter_d[3] = "-d";            // find "-d" in function
    char letter_a[3] = "-a";            // find "-a" in function
    int arg_d;              
    int arg_a;              
    char *find_letter = argv[1];        
    int stringToInt[N+1];               

    for( i = 0; i < argc; i++){     
        printf("%s ", argv[i]);
    }
    printf("\n");

    arg_d = strcmp(find_letter, letter_d);  
    arg_a = strcmp(find_letter, letter_a);  

    if (arg_d == 0) {               
        printf("descend!\n");
        for(i = 2; i<argc; i++) {
            stringToInt[i] = atoi(argv[i]); 
        }
        large_to_small(stringToInt, N);     
        for(i = 0; i < N; i++) {        
            printf(" %d", stringToInt[i]); 
        }
    }
    else if (arg_a == 0) {          
        printf("ascend!\n");
        for(i = 2; i < argc; i++) {
            stringToInt[i] = atoi(argv[i]); 
        }
        small_to_large(stringToInt, N);     
        for(i = 0; i < N; i++) {        
            printf(" %d", stringToInt[i]);
        }
    }
        else {
            printf("Invalid command: %s\n", find_letter);   
        }
    printf("\n");
    return 0;
}

// small_to_large function
void small_to_large(int a[], int n) 
{   
    int i, largest = 0, temp;

    if (n == 1)    
        return;

    for (i = 1; i < n; i++)     
        if (a[i] > a[largest])       
            largest = i;

    if (largest < n - 1) {     
        temp = a[n-1];     
        a[n-1] = a[largest];     
        a[largest] = temp;   
    }
    small_to_large(a, n - 1); 
}

// large_to_small function
void large_to_small(int a[], int n)
{
    int i, largest = 0, temp;

    if(n == 1)
        return;

    for (i = n; i >= 2; i--) 
        if(a[i] < a[largest])
            largest = i;
    if (largest < n - 1) {
        temp = a[n-1];
        a[n-1] = a[largest];
        a[largest] = temp;
    }
    large_to_small(a, n-1);
}

Solution

  • There are mainly two problems.

    1. stringToInt[i] = atoi(argv[i]); : index of stringToInt should start from 0.
    2. large_to_small is wrong. Sorting algorithm may be the same. So you can use the same code.

    Fix to sample:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> 
    #include <stdbool.h>
    
    // function declarations
    void small_to_large(int a[], int n);
    void large_to_small(int a[], int n);
    
    #define OPT_D "-d"
    #define OPT_A "-a"
    
    int main(int argc, char *argv[]) {
        int i;
        for(i = 0; i < argc; i++){   
            printf("%s ", argv[i]);
        }
        printf("\n");
    
        int n = argc - 2;//-2 : program_name and option
        if(n <= 0){
            printf("Usage : %s option(-a or -d) numbers...\n", *argv);
            return EXIT_FAILURE;
        }
    
        int stringToInt[n];
        for(i = 0; i < n; ++i)
            stringToInt[i] = atoi(argv[i+2]);
    
        char *opt = argv[1];
        bool opt_d, opt_a;
        opt_d = !strcmp(opt, OPT_D);
        opt_a = !strcmp(opt, OPT_A);
    
        if(opt_d){
            printf("descend!\n");
            large_to_small(stringToInt, n);
        } else if(opt_a) {
            printf("ascend!\n");
            small_to_large(stringToInt, n);
        } else {
            printf("Invalid option: %s\n", opt);
            return EXIT_FAILURE;
        }
    
        for(i = 0; i < n; i++) {
            printf("%d ", stringToInt[i]);
        }
        printf("\n");
        return 0;
    }
    
    void selection_sort(int a[], int n, bool test(int a, int b)){
        int i, select = 0, temp;
    
        if (n == 1) 
            return;
    
        for (i = 1; i < n; i++)
            if(test(a[i], a[select]))
                select = i;
    
        if (select !=  n-1) {
            temp = a[n-1];   
            a[n-1] = a[select];
            a[select] = temp;
        }
        selection_sort(a, n - 1, test);
    }
    
    static inline bool greater(int a, int b){
        return a > b;
    }
    static inline bool smaller(int a, int b){
        return a < b;
    }
    
    // small_to_large function
    void small_to_large(int a[], int n) {
        selection_sort(a, n, greater);
    }
    
    // large_to_small function
    void large_to_small(int a[], int n){
        selection_sort(a, n, smaller);
    }