You are not logged in.
I'm curious about something. Once you've got your code working, how would it handle something like this
I saw a red truck
john
Offline
exapple, I wouldn't expect that code to do anything useful.
Walk through it yourself with an example input and pretend you are the computer. You increment the current place of the array for EVERY SINGLE character read. But the current place in the array is incremented (but never reset) for every letter in the input.
So a one word input like "hello" would give a resulting array of:
0,1,1,1,1,1,0,0,0,0,0...
(place in array advances for every non-whitespace non-number character, incremement the value after each character has been read.)
A two word input like "hello world" would give something like:
0,1,1,1,1,2,1,1,1,1,0,0.......
(same as above, but the last "1" from hello gets incremented a second time to "2" for the space after hello because there was no non-whitespace,non-numeric character to move the array counter forward so the 1's from the hello and the 1's from world, overlap in the array there.)
What do the elements in your array represent? They are not the number of characters per word. If that were the case, the array size would not have to be more than 32 if you limit the input to 64 characters.
-----------------------------
edit: I tested your code. I was right with my predictions, except for one point: every string of ones ends in a 2. This does make perfect sense the way your code is written, but I missed it the first time. So "hello" gives:
0,1,1,1,1,2,0,0,0....
and "hello world" gives:
0,1,1,1,1,2,1,1,1,2,0,0,0,0.....
neither of these are represented in the histogram loop. I can't make heads or tails of what that should even do.
Also note I did add steps to initialize the array to all zeros - otherwise you just get whatever garbage happened to be in that memory space previously.
Last edited by Trilby (2012-07-13 22:37:16)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
exapple, I wouldn't expect that code to do anything useful.
Walk through it yourself with an example input and pretend you are the computer. You increment the current place of the array for EVERY SINGLE character read. But the current place in the array is incremented (but never reset) for every letter in the input.
So a one word input like "hello" would give a resulting array of:
0,1,1,1,1,1,0,0,0,0,0...
(place in array advances for every non-whitespace non-number character, incremement the value after each character has been read.)A two word input like "hello world" would give something like:
0,1,1,1,1,2,1,1,1,1,0,0.......
(same as above, but the last "1" from hello gets incremented a second time to "2" for the space after hello because there was no non-whitespace,non-numeric character to move the array counter forward so the 1's from the hello and the 1's from world, overlap in the array there.)What do the elements in your array represent? They are not the number of characters per word. If that were the case, the array size would not have to be more than 32 if you limit the input to 64 characters.
-----------------------------
edit: I tested your code. I was right with my predictions, except for one point: every string of ones ends in a 2. This does make perfect sense the way your code is written, but I missed it the first time. So "hello" gives:
0,1,1,1,1,2,0,0,0....
and "hello world" gives:
0,1,1,1,1,2,1,1,1,2,0,0,0,0.....neither of these are represented in the histogram loop. I can't make heads or tails of what that should even do.
Also note I did add steps to initialize the array to all zeros - otherwise you just get whatever garbage happened to be in that memory space previously.
I will try to tweak the code, but no matter if I fix it or not, I will then move to another solution of the same exercise that I found online. It is with functions, but I have read the part about functions in meanwhile, and I will try to re-write that solution myself. Then, I will move to exercie 1-14 which says:
Write a program to print a histogram of the frequencies of different characters in its input.
Soli Deo Gloria!
I Stand Up For Christ! Do you?
________________________
Always have your stuff when you need it with Dropbox. Sign up for free (under my referral link)! http://db.tt/3rOCPK4r
Offline
...Also note I did add steps to initialize the array to all zeros - otherwise you just get whatever garbage happened to be in that memory space previously.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
But I cheated ... I used calloc. I suspect that's not until a later chapter.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
If you're looking at other people's code anyway, here's mine.
I've linked it so if you still want to figure it out on your own (which is good to do) then you can wait to see it. But if you are looking at other people's version anyway, for this one you can ask the coder why he did some of the odd things he did.
As noted above, I used calloc to initialize the array. If you haven't learned about this yet, it can wait - essentially that line is (mostly) just like declaring an array and filling it with zeros.
edit: and 18 is an entirely arbitrary number. I just chose it as the longest word I'd care about. As is, any word longer than 18 characters is broken into two: a 21 character word would be counted as an 18 character word and a 3 character word. If I wanted this to work in a more sensible way, I'd make it just count as an 18 character word.
Last edited by Trilby (2012-07-14 00:44:49)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline