0

I'm having some weird issues with sort. I got the program working but for whatever reason numbers above about 53574 dont get displayed. I only found this by printing the values of the sort function out into the terminal to double check.

Here is the code for the sort function that I used, would be interested if anyone has some advice for me as to whats going wrong. I suspect its some sort of overflow error but I can't think of what caused it. I don't really know how to ask this without showing the code unfortunately :\

void sort(int values[], int n)
{
    const int max = 65536;
    int i;
    int j = 0;
    char count[max];
    for ...
    {
        while(i <= n)
        {
            ...
        }
    }
}

[EDIT: Text reinstated to keep question intact, unrelated code starting at for loop removed for honor code compliance]

1 Answer 1

0

You never initialise your count array, you could use for example

int count[max] = {0};

The while (i <= n) is off by one, should be while (i < n).

I don't think the i > n should be able to trigger, at least if everything else is working correctly.

char is an integer that's usually 8 bits long. It's implementation dependent whether that one is signed or unsigned, but that makes the possible range -128..127 (signed char) or 0..255 (unsigned char). You might want higher numbers (up to the maximum value of n), so better use int.

1
  • Wow, thank you very much I didn't think about the size of char vs int. I'll edit out the question now to prevent people getting the answer, much appreciated :).
    – user440950
    Commented May 4, 2017 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .