Thread: Is this a valid statement?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    2

    Is this a valid statement?

    Code:
    for(ii = 0, j = 0; j <= m; ++j, ++ii)
            ++wlenght[truewlenght[j] - 1];
    This statement is a part of a program that is not functioning correctly.
    Is that maybe a reason?

    The program purpose is to print a histogram of the words. This is actually an exercise 1-13 from Kernighan-Ritchie's book.
    An eventual other corrections would be apprecieated. Here goes the whole code.
    Thanks.

    Code:
    #include<stdio.h>
    
    
    int main(void)
    {
        int c, i, n, j, m, s, ii, jj, nn, wlenght[150], truewlenght[150]; 
        c = i = n = j = m = s = ii = jj = nn = 0;
        
        for(s = 0; s <= 150; ++s)
        {
            wlenght[s] = 0;
            truewlenght[s] = 0;
        }
    
    
    
    
    
    
        while((c = getchar()) != EOF)
        {
            if(c != ' ' && c != '\t' && c != '\n')
                ++wlenght[i];
            
            if(c == ' ' || c == '\t' || c == '\n')
                ++i;
        }
    
    
        n = i;
    
    
        for(i = 0; i <= n; ++i) /* Eliminates 0 character array members */
            if(wlenght[i] > 0)
            {
                truewlenght[j] = wlenght[i];
                    ++j;
            }
    
    
        for(i = 0; i <= n; ++i)
            wlenght[i] = 0;;
    
    
    
    
        m = j;
    
    
        for(ii = 0, j = 0; j <= m; ++j, ++ii)
            ++wlenght[truewlenght[j] - 1]; /* Forms histogram inside the array wlenght */
    
    
        
        for(jj = 0; jj < ii; ++jj)
        {
            putchar('|');
            putchar(jj);
            putchar('|');
    
    
            for(nn = 1; nn <= wlenght[jj]; ++nn)
                putchar('*');
        }
        
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    2
    Quote Originally Posted by laserlight View Post
    How does it not work?
    Instead of histogram I get a messed up output, here is the input that I made and the output that I got:

    The creation, performance, significance, and even the definition of music vary according to culture and social context. Indeed, throughout history, some new forms or styles of music have been criticized as "not being music", including Beethoven's Grosse Fuge string quartet in 1825,[3] early jazz in the beginning of the 1900s[4] and hardcore punk in the 1980s.^Z^Z
    | |||*********||*********||*********||*****||*****|| ****||*****|****| |***|
    |||||||||||||||||||||||||||||||||||||| ||!||"||#||$||%||&||'||(||)||*||+||,||-||.||/||0||1||2||3||4||5||6||7||8|Press any key to continue...
    Last edited by uskk; 05-11-2016 at 05:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  2. Is this valid?
    By KBriggs in forum C++ Programming
    Replies: 3
    Last Post: 07-08-2009, 10:42 AM
  3. Is this valid
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-25-2007, 03:26 AM
  4. 2=1, is this valid?
    By the Wookie in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-16-2003, 05:51 PM
  5. Is this valid?
    By Boksha in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2002, 01:55 PM

Tags for this Thread