Thread: confusion in array concept...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Unhappy confusion in array concept...

    Code:
      int c,a[10];
      while((c=getchar()!=EOF)
      {
             if(c>='0' && c<='9')
                  ++a[c-'0'];
      }
    i have a confusion in the body part of while loop .... what does the body part mean??????

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If c is a digit, increment the element of a corresponding to the digit (i.e., count the frequency of the digit). Incidentally, you probably should initialise a.
    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
    Apr 2011
    Posts
    5
    that's fine ... i know for array we increment the index for getting the next value...
    but over here,
    [code]
    if(c>='0' && c<='9')
    ++a[c-'0'];
    [\code]
    what are we doing over here???
    basically as we are writing 0 as a character '0' thus as c is an integer, '0' will be converted into it's ascii value 48 and thus it will be checked in the if condition and similary for '9'.... now when we write ++a[c-'0']
    it means a[c-'0']=a[c-'0'] + 1 which i am confused ???
    if you could explain it more it would be clear to me....

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You are correct that the entered value is in ASCII. That is, digits '0' to '9' are really integers 48 to 57.
    The 'if' statement checks that the character is within that range. You could have done it this way as well: if (c >= 48 && c <=57) but using the quoted chars is easier to read.

    The c-'0' calculation removes that offset. We know '0' is 48, or you could have written it as c-48. It ensures the index becomes 0 to 9.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by xterminator View Post
    what are we doing over here???
    Counting number frequency. If the character provided by getchar() is between 0 and 9, then the corresponding element of the array is increased by one. This might be clearer:
    Code:
    a[c-'0']++;
    Since it does not matter whether it is a post or pre increment. As you say, the ASCII value of '0' is 48. So, eg, if c == '5' (ASCII 53), then element 53-48 = 5 will have one added to it.

    Make sense?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    thanks...
    got it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM