user11333485
user11333485

Reputation:

No output given when executing a program on C

When I compile this program with gcc:

#include <stdio.h>

/* This program accepts some text as an input and gives the output
 * of longest word and shortest word lengths*/

int main(){
int c, i, wordcount, symbolcount, longestword, shortestword;
wordcount = symbolcount = longestword = shortestword = 0;
int wlength[1];
while((c = getchar()) != EOF){
    ++symbolcount;
    if(c == ' ' || c == '\n' || c == '\t'){
        ++wordcount;
        wlength[wordcount];
        wlength[wordcount - 1] = symbolcount;
        symbolcount = 0;
    }
}
for(i = 0;i <= wordcount;)
wlength[0] = longestword;
wlength[i] = shortestword;
while(shortestword < 1){
    if(shortestword == longestword){
        continue;
        ++i;
    }else if(shortestword < longestword && shortestword > 0){
        shortestword = wlength[i];
        break;
    }
}
for(i = 0; i <= wordcount - 1; ++i){
    if(wlength[i] > longestword){
        longestword = wlength[i];
    }else if(wlength[i] < longestword && wlength[i] > shortestword){
        continue;
    }else{
        wlength[i] = shortestword;
        }
    }
printf("%d\t%d", longestword, shortestword);
return 0;
}

There are no errors or warnings. But when I try to run it, it accepts the input, but there is no output at all. Even when I press Ctrl + D(I work on a debian based distro), current terminal session is not suspended and the program just keeps running. What can be the problem?

Upvotes: 0

Views: 65

Answers (3)

Jens
Jens

Reputation: 72639

The problem is that

int wlength[1];

only declares an array with one element, but you access out of bounds with

shortestword = wlength[i];

This is undefined behavior in C parlance, anything can happen, including what you observe.

To fix this, declare the array with as many elements as you expect i to be. Make sure your loops over i only take values that do not exceed the array element count.

Upvotes: 1

M Oehm
M Oehm

Reputation: 29126

There are several things wrong with your program.

  • You don't allocate space for only one word.
  • You have an infinite loop over i. That's acutally why you don't see any output: The program is stuck in this loop.
  • The second while loop doesn't look as if you knew what you were doing there. I doubt that the condition shortestword < 1 will ever be true. A contunue before other statements makes those statements useless. And what exactly is i here. (Okay, perhaps the while is supposed to be inside the for loop? If so, you need curly braces on the loop body.)

Most of the errors stem from a misunderstanding of the problem. You do not need to store the lengths of all words in order to find the sortest and longest words. Just keeping track of the length of the current word is enough. The algorithm goes like this:

  • set longest to 0.
  • set shortest to a large number.
  • set length to 0.
  • for each character in the input:
    • if it is a white-space character:
      • update longest and shortest if necessary.
      • reset length to 0.
    • otherwise:
      • increase length

This lets you find the longest word in Moby-Dick without having to store more than the current word length. in C, it may look like this:

#include <stdio.h>

int main(void)
{
    int longest = 0;            // length of currently longest word
    int shortest = 0;           // length of currently shortest word

    int length = 0;             // length of current word
    int c = getchar();

    while (c != EOF) {
        if (c == ' ' || c == '\n' || c == '\t') {
            if (length) {
                if (longest == 0 || length < shortest) shortest = length;    
                if (length > longest) longest = length;

                length = 0;
            }
        } else {
            length++;
        }

        c = getchar();
    }

    printf("max: %d\nmin: %d\n", longest, shortest);

    return 0;
}

Upvotes: 1

Karthikgr
Karthikgr

Reputation: 81

You have declared an integer array wlength whose size is 2 i.e.,

int wlength[1];

and within the if condition you increment wordcount.

Now assume you have 4 words in a line and wordcount keep on increasing and will be assigned to wlength index but as you have defined the array size 2 , where it overflows. Thus when that is used further in

 shortestword = wlength[i]; 

and

longestword = wlength[i];

it causes junk values to be assigned.

Upvotes: 1

Related Questions