Thread: K&R array program learning

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37

    K&R array program learning

    hello experts i am learning K&R array program i have a lot of doubts please help me

    How the output shows as digits = 2 1 1 1 1 1 1 1 1 1 ?

    Code:
    #include<stdio.h>
    int main()
    {
    	int c,i,nwhite,nother;
        int ndigit[10];
    
        nwhite = nother = 0;
        for(i = 0; i < 10; ++i)
        	ndigit[i] = 01
        	;
    
        while((c = getchar()) != EOF)
        	 if(c <= '0' && c <= '9')
        		 ++ndigit[c- '0'];
        	 else if (c == "" || c == '\n' || c == '\t')
           		 ++nwhite;
        	 else
        		 ++nother;
            printf("digits =");
            for(i = 0; i < 10; i++)
            	printf(" %d ", ndigit[i]);
                printf(",white space = %d, other = %d \n", nwhite ,nother);
    
    }
    In put
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    l
    a
    s
    d
    f

    Output
    digits = 2 1 1 1 1 1 1 1 1 1 ,white space = 0, other = 14


    thanks

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Indentation fixed. Actual fixes noted with // comments. The main issue was c <= '0' when it needed to be c >= '0'.

    The program is generating a histogram.

    Code:
    #include<stdio.h>
    int main()
    {
        int c,i,nwhite,nother;
        int ndigit[10];
    
        nwhite = nother = 0;
        for(i = 0; i < 10; ++i)
            ndigit[i] = 0;                                  // 0
    
        while((c = getchar()) != EOF)
             if(c >= '0' && c <= '9')                       // >= 
                 ++ndigit[c- '0'];
             else if (c == ' ' || c == '\n' || c == '\t')   // ' '
                 ++nwhite;
             else
                 ++nother;
        printf("digits =");
        for(i = 0; i < 10; i++)                             
        // spaces on the next two lines
            printf(" %d", ndigit[i]);
        printf(", white space = %d, other = %d\n", nwhite ,nother);
        return(0);                                          // return
    }
    Input, if one line:

    Code:
    1234567890|asdf
    Last edited by rcgldr; 08-24-2013 at 05:51 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might also want to look up functions in the standard header <ctype.h>. There are facilities there like isdigit() to test if a character is a digit, isspace() to test if a character is whitespace, isalpha() to test for alphabetic characters, and more.

    Note that those standard functions also work with internationalisation (for example, the notion of what is an alphabetic character varies with natural language). Also, there are more whitespace characters than just the three you are testing for.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning C. Need help on this program
    By jwall in forum C Programming
    Replies: 9
    Last Post: 01-25-2013, 09:03 PM
  2. Learning C++, needing help with a program
    By Wilfenite in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2008, 09:39 AM
  3. Learning how to program
    By Tesnik in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2007, 07:33 PM
  4. reinforcement learning c program
    By Temden in forum C Programming
    Replies: 1
    Last Post: 05-10-2006, 07:27 AM
  5. Q-Learning C program help
    By Temden in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2006, 04:56 AM