can u help me understand what this pogram does?

This is a discussion on can u help me understand what this pogram does? within the C Programming forums, part of the General Programming Boards category; Im using Dev C++ and this program cant run.... This is an example from arrays book from C by dissection ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    19

    Post can u help me understand what this pogram does?

    Im using Dev C++ and this program cant run....
    This is an example from arrays book from C by dissection
    Code:
    /* Count each uppercase letters separately */
    
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    
    int main(void)
    {
        int c, i, letter[26];
        
        for (i = 0; i < 26;++i)
            letter[i] = 0;  //initialize i to zero yep good here
        while ((c = getchar()) != EOF) /*Input character and not equal to escape */
              if (isupper(c))    /* If it is uppercase */
                 ++letter [c - 'A']; /* <<<< This is the part i do not understand i think its ASCII */
        
    for (i = 0; i < 26; ++i) {
            if (i % 6 == 0) /* yeah must have 6 columns till newline */
               printf ("/n"); 
            printf ("%4c:%3d", 'A' + i, letter [i]); /* i think i get it the first part is A,B,C,D  and the second part addresses the value of letter??? */
            }
            printf ("\n\n"); 
            getchar();
            return 0;
    }
    Last edited by EdwardElric; 10-26-2007 at 06:49 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    What do you mean "can't run" ?

    > printf ("/n");
    There's a / which should be a \ perhaps.

    Before the final getchar() call (you've reached EOF in the while loop remember), you need to do
    clearerr(stdin);
    otherwise the final getchar() will immediately return with EOF.

    > ++letter [c - 'A']; /* <<<< This is the part i do not understand i think its ASCII */
    Add some debug, and print the result of c - 'A' and find out.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unless you are using a strange OS:
    Code:
        while ((c = getchar()) != EOF) /*Input character and not equal to escape */
    Comment and code are not matching. EOF is an indication that the input stream is at it's end. Usually produced by pressing CTRL-Z (Windows, DOS, CP/M, VAX/VMS, etc) or CTRL-D (Linux, Unix and such) when using the console for input - or indicated by the current file-pointer being equal to the number of bytes in the file if the input is a text-file in your file-system.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    This program cant still run and i tried what you did
    Only some blank screen appeared
    Code:
    printf ("\n\n");
            clearerr(stdin);
            getchar();
            return 0;
    is this what you mean?

    from here:
    printf ("&#37;4c:%3d", 'A' + i, letter [i]);
    the output of these (taken from the book) are:
    A: 75 B: 52 C: 219 D:14 .........
    what are these numbers? Idonot understand here
    can u help?...
    Last edited by EdwardElric; 10-26-2007 at 07:19 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by EdwardElric View Post
    This program cant still run and i tried what you did
    Only some blank screen appeared
    It's waiting for your to type some letters (counting the upper-case ones). Finish by issuing the CTRL-key for "End of file" as I wrote in my previous post.
    Code:
    printf ("\n\n");
            clearerr(stdin);
            getchar();
            return 0;
    Yup.

    is this what you mean?

    from here:
    printf ("%4c:%3d", 'A' + i, letter [i]);
    the output of these (taken from the book) are:
    A: 75 B: 52 C: 219 D:14 .........
    what are these numbers? Idonot understand here
    can u help?...
    What do you THINK it means, what does the program say that it does?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    What do you THINK it means, what does the program say that it does?
    No that question is part of my upper code....


    This is the output of the program
    A: 75 B: 52 C: 219 D:14 E:121 F: 13
    G: 9 ......... up to Z

    This is what my problem lies what are these numbers?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What part of "what are these numbers" is it that you have a difficulty understanding. Do you have a description of the overall program that tells you WHAT THE PURPOSE OF THE PRORGRAM is?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    Counting each letter separately
    and followed by the numbers stuff....

    if u have the book
    its on
    C by dissection 4th edition page 307, lesson 9.2

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by EdwardElric View Post
    Counting each letter separately
    and followed by the numbers stuff....

    if u have the book
    its on
    C by dissection 4th edition page 307, lesson 9.2
    No, I don't have that book - and I'm asking you the question so that YOU understand things, not because I don't understand it. If I had the book, I may recommend that you re-read a particular paragraph, but as it stands, I don't so I can't say WHERE in the book, it explains exactly what this program does.

    So if the application is counting letters, what do you think the output is?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by matsp
    EOF is an indication that the input stream is at it's end. Usually produced by pressing CTRL-Z (Windows, DOS, CP/M, VAX/VMS, etc) or CTRL-D (Linux, Unix and such) when using the console for input - or indicated by the current file-pointer being equal to the number of bytes in the file if the input is a text-file in your file-system.
    Note that on some systems or emulators you have to press <enter> after typing EOF, or so I hear.

    So, what does this do?
    Code:
    ++letter [c - 'A'];
    Let's deal with the indexing code first. c - 'A'. Have you ever seen an ASCII table or chart? http://www.ascii.cl/

    An ASCII table shows how each character is represented by a number. 'A' is 65, ' ' (space) is 32, and so on. Every character is actually a number as far as the compiler is concerned. You could use 65 anywhere you could use 'A' -- but don't bother, because 'A' is a lot easier to read than 65.

    If you look at the table carefully, you'll notice that the letters 'A' through 'Z' (and 'a' through 'z') appear in a sequence. The values for 'A' to 'Z' are 65 ('A'), 66 ('B'), ... to 90 for 'Z'. So if you subtract 65 ('A') from each of these numbers, you'll get 0, 1, ... 25, which can be used as array indexes. And that's exactly what your code does.

    So the c - 'A' gets the index of the array; the rest of the code increments that element of the array. In effect, it adds one to letter[0] if c is 'A', to letter[1] if c is 'B', and so on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I want to make a pogram that captures another program's HWND.
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 07-26-2005, 04:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21