-3

I've tried so much code trying to display my data in a histogram. I've been stuck on this problem for two days now, data crawling the internet and such to find an answer but they usually talk about pointers and ect. while this is much more basic. I've tried an actual histogram that displayed the same error but for the sake of keeping it simple I choose to post this one.

This is the description of the program from The C Programming language: "Write a program to print a histogram of the lengths of the words in its input. " I'm doing it horizontally then I will do it vertically. Although, in this example I am just trying to display the number, obviously.

#include <stdio.h>
#define MAXIMUM 99


main()
{
int i, c, j, lenOfWord[MAXIMUM], charCount;

for (i = 0; i < MAXIMUM; i++)
    lenOfWord[i] = 0;


printf("Starting program, please type then ctrl+d\n");



while((c = getchar()) != EOF) {
    if (c == ' ') {
        //new word
        ++lenOfWord[charCount];
        charCount = 0;
    }
    else
        //inword
        ++charCount;
}

printf("\n Displaying now... \n");


for (j = 0; j <= MAXIMUM; j++)
    printf("%d: %d", j, lenOfWord[j]); 


}

output:

 JohnJackson@linux-72db:~/Desktop> gcc c.c -o c
 JohnJackson@linux-72db:~/Desktop> ./c
 Starting program, please type then ctrl+d
 Hello my name is john jacksonSegmentation fault

*Segmentation fault on the end is what i get when I do EOF (ctrl+D)

Additional information: GCC compiler from terminal in openSUSE linux

Thanks so much, I'm trying to learn C but I've hit alot of road blocks like these, I'm getting frustrated.

user275564
  • 163
  • 1
  • 2
  • 10
  • 2
    Compile with all warnings & debug info (`gcc -Wall -Wextra -g`). Use the debugger (`gdb`) – Basile Starynkevitch Oct 21 '14 at 21:06
  • 3
    `j >= MAXIMUM`? That'll terminate your loop on the first iteration. the second clause of a `for()` is the loop termination condition. As long as the condition evaluates to "true", the loop continues executing. You've basically setting up your loops to fail, since `0 >= 99` is false right off the bat. – Marc B Oct 21 '14 at 21:08
  • 1
    charCount is never initialized, and you're trying to access lenOfWord[charCount] – xd6_ Oct 21 '14 at 21:11
  • sorry everyone, I fixed the for error, but I am getting the same segmentation error – user275564 Oct 21 '14 at 21:12
  • 1
    @user275564 Which of the errors did you fix - why not update the question with what's currently not working? – Rowland Shaw Oct 21 '14 at 21:13
  • The uninitialized `charCount` is your immediate problem, I believe. Fix that and you'll run into more. – Fred Larson Oct 21 '14 at 21:15
  • The underlying reason that you're having problems is that you're failing to appreciate that programming requires *precision*. Read over your program *carefully* to see if what you're doing makes sense and is accurate. – Jim Balter Oct 21 '14 at 21:19
  • I have updated the program. The problem has been solved. It was the uninitialized variable charCount. I found that this has been the same error throughout and all my programs, I have been forgeting to initialize my variables. Thanks everyone. Lesson learned. Jim, I will take more time to be precise and accurate. Thank you everyone. – user275564 Oct 21 '14 at 21:31
  • As one more general hint, especially for any novice programmer: always use those `{}` for ifs and loops, even if there is one statement. It's just too easy to make a mistake. So just put them there to eliminate a few possible issues. – hyde Mar 15 '19 at 10:38

2 Answers2

2

The actual problem is

++lenOfWord[charCount];
        charCount = 0;

You have not initialized charCount to 0 before using it. Initialize it to 0 first before the loop

charCount = 0;

while((c = getchar()) != EOF) {

And assigning charCount to 0 in loop is of no use. Remove it.

You have to check one more thing. Since the array size is MAXIMUM. So You have to put a check that charCount does not exceed MAXIMUM. Otherwise, it will again give segmentation fault.

Another problem is in the for loop condition. Instead of

for (i = 0; i < MAXIMUM; i++)

change it to

for (i = 0; i < MAXIMUM; i++)

And

Instead of

for (j = 0; j >= MAXIMUM; j++)

to

for (j = 0; j < MAXIMUM; j++)

Otherwise you won't get any output.

Arpit
  • 767
  • 7
  • 20
  • I think you are correct, thank you so much. Although, I believe that setting the charCount back to 0 after a space is essential to the program. The char Count after a space is 0. – user275564 Oct 21 '14 at 21:25
  • what is the difference between the first two for statements in your example? – user275564 Oct 21 '14 at 21:39
0

Your charCount variable is not initialized

Nikita
  • 6,270
  • 2
  • 24
  • 37
  • Thank you sir/mam, problem solved. I think this was the problem in my last versions of the program. LESSON LEARNED! – user275564 Oct 21 '14 at 21:27