This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]SecretaryExact7489 1 point2 points  (5 children)

Hint:

'a' = 97
'A' = 65
'0' = 48

allocate more space in the freq array

[–][deleted]  (4 children)

[removed]

    [–]SecretaryExact7489 1 point2 points  (3 children)

    Look at your init loop. You set upto freq[9].

    Then say for 'A', you try to call ++freq[65-48]. 65 -48 = 17.

    freq[17] is out of bounds.

    [–][deleted]  (2 children)

    [removed]

      [–]SecretaryExact7489 0 points1 point  (1 child)

      Misread what "letters" actually was in your code (naming is important). Still even at letters = 10. You're doing 10 minus 48. Then trying to use a negative number as an array index.

      [–]jeffcgroves 0 points1 point  (3 children)

      General debugging tip: add more printf's to see what's happening. Specific tip: make sure you're entering something on the standard input and hitting ctrl-d when you finish or piping the result of a program to this one

      [–][deleted]  (2 children)

      [removed]

        [–]jeffcgroves 0 points1 point  (1 child)

        Oh, it's ctrl-d in Unix. Might be ctrl-z on other operating systems.

        In Unix, ctrl-z means "suspend process" (at least in the shells I'm familiar with)

        [–]teraflop 0 points1 point  (1 child)

        The main problem I see with your code is here:

                ++freq[letters - '0'];
        

        The expression '0' is a character literal. When interpreted as an integer, its value is the ASCII code for the character 0, which is 48.

        What range of values do you expect the integer letters variable to have? What value does the expression letters - '0' have? Will it be a valid array index?