1

I am trying to sort *argv[] and I am getting random numbers printing out when i try and display the results of sorting.

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

void sortIntegers(int value[], int length);
int convertToInt(char *string);
int *sortArguments(int argc, char *argv[]);

int main() {

    int i;
    int value[9] = {0, 234, 345345, 91, -3, 12, 3, 19, 17};
    char *argv[9] = {"0", "234", "345345", "91", "-3", "12", "3", "19", "17"};
    int argc = 9;
    int *result;

    printf("\n");

/* order before sorting */
    printf("Before: ");
    for(i = 0; i < 9; i++){
        printf("%d ", value[i]);
    } 
/* end or sorting proof */

/* converting char sting to integers and sorting */
    result =sortArguments(argc, argv);
    printf("\nAfter: ");
    for (i = 0; i < argc; i++) {
        printf("%d, ", result[i]);
    }
    printf("\n");
/* end of sorting and conversion */

/* this is to prove my sort algorith works */
    sortIntegers(value, 9);
    printf("Desired Results: ");
    for(i = 0; i < 9; i++){
        printf("%d ", value[i]);
    }
    printf("\n\n");
/* end or sorting proof */

    free(result);
    return 0;
}

 void sortIntegers(int value[], int length) {     

    int i;
    int x;
    int temp;

    for(x = 0; x < (length - 1); x++)
    {
        for(i = 0; i < (length - x -1); i++)
        {
            if(value[i] > value[i+1])
            {
                temp = value[i];
                value[i] = value[i + 1];
                value[i + 1] = temp;
            }
        }
    }
}
int convertToInt(const char *string) {
    char stringToNum;

    stringToNum = atoi(string);

    return stringToNum;
}
int *sortArguments(int argc, char *argv[]) {

    int *list = malloc(sizeof(int) * (argc));
    int i;    

    for (i = 0; i < argc; i++) 
    {    
        list[i] = convertToInt(argv[i]);
    }
    sortIntegers(list, (argc));

    return list;
}

I have viewed the question Bubble sorting random numbers and I believe i have not committed this error.

here is the output of my program: Before: 0 234 345345 91 -3 12 3 19 17 After: -22, -3, 0, 1, 3, 12, 17, 19, Desired Results: -3 0 3 12 17 19 91 234 345345

The before statement comes from a sorting test within my main, that is done correctly, because of this i believe my problem is either with pointer work or calling the function.

does anyone know why sorting would insert random numbers?

  • 1
    Have you traced the code with a debugger in order to see the values the variables have at each point in the program? Follow the debugger with a pen and paper, and check when the code first changes a number in a way you didn't expect, that way you can isolate the issue to just one or two lines instead of the whole program. – Davy M Feb 16 '18 at 16:39
  • 1
    @DavyM I have done that, I must have missed something, I am going to do that again now – Tyler Green Feb 16 '18 at 16:44
  • The `convertToInt` is useless _and_ wrong. Just use `atoi` directly. – Jabberwocky Feb 16 '18 at 16:45
  • 2
    `int *list = malloc(sizeof(int) * (argc-1));` is wrong. Should be `sizeof(int) * argc` – Ingo Leonhardt Feb 16 '18 at 17:09
  • @IngoLeonhardt I have changed it, thank you – Tyler Green Feb 16 '18 at 17:41
  • @MichaelWalz [Why shouldn't I use atoi()?](https://stackoverflow.com/questions/17710018/why-shouldnt-i-use-atoi) – machine_1 Feb 16 '18 at 17:45
  • @machine_1 the OP was already using `atoi` in his code. For me `atoi` is OK in toy programs. – Jabberwocky Feb 16 '18 at 17:47
  • atoi may be obsolete but my school still makes us program in ANSI – Tyler Green Feb 16 '18 at 17:50

1 Answers1

1

Don't use char in convertToInt, use int

int convertToInt(char *string) {
    int stringToNum;

    stringToNum = atoi(string);

    return stringToNum;
}

or even better

int convertToInt(char *string) {
    return atoi(string);
}

or even better, get rid of the function call all together and use atoi() directly in sortArguments() (as suggested by Michael)

int *sortArguments(int argc, char *argv[]) {

    int *list = malloc(sizeof(int) * (argc));
    int i;    

    for (i = 0; i < argc; i++) 
    {    
        list[i] = atoi(argv[i]);
    }
    sortIntegers(list, (argc));

    return list;
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
  • You might want to go into why, such as 234, 345345 don't fit in a signed char. – Max Feb 16 '18 at 17:00
  • Indeed, sizeof(char) = 1 byte, signed gives you 7 bits for positive value, 2^7 =128, which gives you a range of 0 to 127 – Stephen Docy Feb 16 '18 at 17:02
  • @StephenDocy This is the reason that a number like -22 would appear in the array after sorting, 345345 is to large to be stored into a char. Because of this the computer makes up random numbers? – Tyler Green Feb 16 '18 at 17:24
  • It is the reason why you got -22, but it is not just a random number. When the value goes beyond the positive value range, it sets the sign bit, turning it into a negative value....google "integer overflow" – Stephen Docy Feb 16 '18 at 17:30
  • @TylerGreen have a look at Ingo Leonhardt's comment to your answer. This is crucial, without the correction he suggests your program has undefined behaviour (google that). – Jabberwocky Feb 16 '18 at 17:34
  • @StephenDocy or even better: just use `atoi` and drop the `convertToInt` function alltogether. BTW it should be `int convertToInt(const char *string)` – Jabberwocky Feb 16 '18 at 17:46
  • @MichaelWalz Edited, ill keep this in mind for next time, the less possible confusion, the better. – Tyler Green Feb 16 '18 at 17:55