Like Tree1Likes

help with nested loops

This is a discussion on help with nested loops within the C Programming forums, part of the General Programming Boards category; Hello, I am new to c programming and am trying to write a simple program to get the following output: ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    help with nested loops

    Hello, I am new to c programming and am trying to write a simple program to get the following output:

    ABCDE
    ABCD
    ABC
    AB
    A

    The code I have written gives me an output of:

    ABCDE
    BCDE
    CDE
    DE
    E

    I can't seem to figure out how to change this output. I have played with the nested loop but still cannot figure out the problem. There must be a way to count down. Any help I can get is much appreciated. Thank you.

    source code:
    #include <stdio.h>
    int main(void)
    {
    const int ROWS = 5;
    const int CHARS = 5;
    int row;
    int ch;

    for (row = 0; row < ROWS; row++)
    {
    for (ch = ('A' + row); ch < ('A' + CHARS); ch++)
    printf("%c", ch);
    printf("\n");
    }
    fflush(stdin);
    getchar();
    return 0;
    }

  2. #2
    Newbie "%s", username's Avatar
    Join Date
    Jul 2004
    Posts
    4
    for (ch = ('A' + row); ch < ('A' + CHARS); ch++)
    I may not know everything about c, but if the value of the first character is 'A' plus the row it's on, wouldn't that row not start with an 'A'?

    Maybe that'll help.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The poster needs to use [code] tags, otherwise most people don't even bother reading their posts. Just like they didn't bother reading the board's Announcements!

    [edit]Line snipped. Still not awake.[/edit]

    [edit2]Fun with only one loop...
    Code:
    void OxOO(char *OxO0,int Ox0O)
    {
            char Ox00[0x1A]={0x00},Ox01;
            for( Ox0O=Ox0O%0x1A,Ox01=0x00;Ox01<Ox0O;Ox01=Ox01+0x01 )
            {
                    strncpy( Ox00, OxO0+Ox01, Ox0O-Ox01 );
                    (Ox0O-Ox01)[Ox00]=0x00;
                    printf("%s\n",Ox00);
            }
    }
    Because we wouldn't want you to turn it in as-is for home work, now would we?
    [/edit2]


    Quzah.
    Last edited by quzah; 07-08-2004 at 01:26 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I guess you can always do this:

    Code:
    char str[]="ABCDE\0";
    int i;
    
    i = strlen(str);
    while (i--) {
       printf("%s", str);
       str[i] = '\0';
    }

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Devil Panther
    I guess you can always do this:

    Code:
    char str[]="ABCDE\0";
    int i;
    
    i = strlen(str);
    while (i--) {
       printf("%s", str);
       str[i] = '\0';
    }

    You proabably want to try that again, because the output of yours is:
    ABCDEABCDABCABA
    Of course, I misread the output they wanted, so mine gives the gradiated version:
    ABC
    BC
    C
    (Because I figured that would be the more challenging of the two, so that'd be what they wanted. Guess not. )


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    oops, forgot the \n

    try this:
    Code:
    char str[]="ABCDE\0";
    int i;
    
    i = strlen(str);
    while (i--) {
       printf("%s\n", str);
       str[i] = '\0';
    }
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could also have just used puts.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    you always have to say the last word...

    me too
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's another fun one, just for grins...
    Code:
    void O_Oy( char *o_o, int o_O )
    {
            char V_V [BUFSIZ]="%s\n";
            char T_T [BUFSIZ]="%%[^%s]";
            char v_v [BUFSIZ]={0};
            char x_x [BUFSIZ]={0};
    
            while( o_O -- )
            {
                    sprintf( v_v, T_T, o_o + o_O + 1  );
                    sscanf( o_o, v_v, x_x );
                    printf( V_V, x_x );
            }
    }
    Because modifiable format specifiers are fun!

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Quzah - your code always reminds me of a MagicEye cartoon.

  11. #11
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    and here's a recursive version for completeness
    Code:
    void foo(char *s, int n)
    {
      int i;
    
      if (n >= 1)
      {
        for (i = 0; i < n; i++)
          printf("%c", s[i]);
        putchar('\n');
    
        foo(s, --n);
      }
    }

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Quote Originally Posted by geo_c
    for (row = 0; row < ROWS; row++)
    {
    for (ch = ('A' + row); ch < ('A' + CHARS); ch++)
    printf("%c", ch);
    printf("\n");
    }
    As %'s said that ch has to start at 'A' every time so adding row to it is bad. yet you want to have it only disply a diminishing number of letters each round.

    If you read your for loop out
    For Ch is A plus row(wich incrments up); stop the loop when ch is equal to 'A' + char(wich needs to incment down every loop); incment ch.

    Any way,
    I do have a question about the code from some of the more expeienced coders, what is the use of useing a constant integer there over a regular integer? And why do teachers and books tech fflush(stdin); when its um bad?

    ~Meloshski

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    Thanks to all for all the suggestions! You have put me on the right track. Much appreciated.

    Geo.

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by geo_c
    Thanks to all for all the suggestions! You have put me on the right track. Much appreciated.

    Geo.
    With all the suggestions they gave you, they put you more than on track. They stuck a firecracker in your ass and made you lap the track.

    I thought this would be a question I could handle, until I came in and saw it overkilled.

    Haha, this forum is awesome.
    quzah likes this.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    Yes, I said the right track.

    for (stop_ch = 'E'; stop_ch >= 'A'; stop_ch--)
    {
    for (ch='A'; ch<=stop_ch; ch++)
    {
    printf("%c", ch);
    }
    printf("\n");

    Thanks anyway.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 07:58 AM
  2. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 02:47 PM
  3. nested for loops and bank account interest help!!
    By webvigator2k in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 09:03 PM
  4. Output from nested loops
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-01-2002, 07:09 AM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM

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