Thread: array concepts need

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    array concepts need

    can someone please explain this to me ?
    Code:
    if (c >= '0' && c <= '9')
    ++ndigit[c-'0'];  // this line > what is it and what does it do?
    taken from K&R:
    Code:
     
    #include <stdio.h>
    int main(void)
    {
        int c, i, nwhite, nother;
        int ndigit[10];
        nwhite = nother = 0;
        
        for (i = 0; i < 10; ++i)
        ndigit[i] = 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)
                  printf(" %d", ndigit[i]);
             printf(", white space = %d, other = %d\n",nwhite, nother);
    return 0;
    }
    any help is appreciated thank u

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    ++ndigit[c-'0'];
    c is an integer variable representing, the result of getchar (a ASCII character in most cases). And, in this case it represents an ASCII digit between '0' and '9'.

    When the value of the ASCII code for '0' is subtracted from it.
    The answer yields an integer between 0 and 9.

    The rest of the meaning of the line should be clear if you understand integer arrays and the ++ operator.
    I am not sure how to explain arrays; I learned them so long ago; I just can not remember ever not know the meaning.

    Maybe, this link will help you.
    Arrays in C - Cprogramming.com

    Things to remember on C arrays the first element is always numbered 0.
    The last legal index value is always one less that declared. In this case the max legal index is 9.

    Code:
    int ndigit[10];
    The above line declares an array called ndigit with the size of 10.
    The first element index is 0 the last element index is 9.

    Tim S.
    Last edited by stahta01; 05-04-2012 at 09:32 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    i'm ok with arrays it was the part above which u answered i didn't understand thank you that helps allot

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > i'm ok with arrays
    So why was the title of the thread "array concepts"?

    Consider adding some print statements so you can see what things are doing.
    Code:
    if (c >= '0' && c <= '9') {
        printf("What is it %d\n", c - '0' );
        ++ndigit[c-'0'];
    }
    It's source code, you can do whatever you want to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. Some basic C++ concepts
    By tx1988 in forum C++ Programming
    Replies: 12
    Last Post: 09-15-2007, 08:16 PM
  3. concepts on 3d array
    By AngKar in forum C Programming
    Replies: 5
    Last Post: 12-29-2005, 07:56 PM
  4. Java Concepts
    By mart_man00 in forum Tech Board
    Replies: 11
    Last Post: 09-04-2003, 05:14 AM