For Loops

This is a discussion on For Loops within the C Programming forums, part of the General Programming Boards category; This probably is a stupid question hey we all got to start programming somewhere. I got to use for loops ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    For Loops

    This probably is a stupid question hey we all got to start programming somewhere.

    I got to use for loops to generate two patterns as follows.
    *****
    ****
    ***
    **
    *

    and

    *
    ***
    *****
    *******
    *****
    ***
    *

    Any Ideas.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    well
    Code:
    for(i = 0; i < 10; ++i)
    {
    }
    is the same as
    Code:
    i = 0;
    while(i < 10)
    {
        ++i;
    }
    write some code if u haven't already and we'll help u out from there.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Try expirmenting with nested for loops.

    For instance there are five lines to suggestion which implies a loop counting to five. Second there is an inner loop which print a number of stars based on the current line.

    Code:
    numberofstars = maxstars;  // maxstars=5
    for (int lines=0; lines<5;++lines)
    {  
        while (numberofstar>0)
        {
             cout << '*';
             numberofstar =numberofstars-1;    
         }  
        maxstars--;  // decrement by 1
        numberstars=maxstars;
    }

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    clueless

    I've absolutley no idea how to write a for loop. Does this website explain them well. I've written while loops if that is any help.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    51

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've absolutley no idea how to write a for loop.
    I had problems with for loops when I started. It helps to build a for loop from the equivalent while loop. You construct it like so:
    Code:
    /* Start with a while loop */
    i = 0;
    while ( i < 10 ) {
      i++;
    }
    
    /* Put semicolons around the condition */
    i = 0;
    while ( ; i < 10 ; ) {
      i++;
    }
    
    /* Put the initialization of the condition variable before the condition */
    while ( i = 0 ; i < 10 ; ) {
      i++;
    }
    
    /* Put the increment of the condition variable after the condition */
    while ( i = 0 ; i < 10 ; i++ ) {
    }
    
    /* Change the keyword while to for */
    for ( i = 0 ; i < 10 ; i++ ) {
    }
    And you're done! Just remember that only the first and last steps of this process result in valid C code.
    My best code is written with the delete key.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    why doesn't this code work?

    Code:
    // starts declining
    
    # include <stdio.h>
    
    int main()
    
    { int stars;
    
    for(stars=5; stars>=1;stars--) // also for stars in range(6,0-1)
        printf("*\n"*stars);
        
    system("pause");
    
    return 0;
    
    }

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Re: clueless

    Originally posted by apoc632
    I've absolutley no idea how to write a for loop. Does this website explain them well. I've written while loops if that is any help.

    LEARN FOR / WHILE LOOPS HERE

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why doesn't this code work?
    Because it has syntax errors and undefined behavior. If you clean it up so that it works, you get this:
    Code:
    // starts declining
    # include <stdio.h>
    
    int main()
    {
      int stars;
      
      for(stars=5; stars>=1;stars--) // also for stars in range(6,0-1)
        printf("*\n");
      
      return 0;
    }
    My best code is written with the delete key.

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Prelude
    [B]>why doesn't this code work?
    Because it has syntax errors and undefined behavior. If you clean it up so that it works, you get this:
    Your code doesn't work;

    I assume you didn't understand my question. The code should output what apoc wanted it to.


    that is why
    Code:
    printf("*\n" * stars);
    I believe the problem is in that statement.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Your code doesn't work;
    It works just fine, you just weren't expecting what you got. But let's play on your assumptions a bit if you don't mind:
    Code:
    printf("*\n" * stars);
    Even if this were to print "*\n" stars times, you would still only have one * per line, so it wouldn't work the way you want even if it were legal.

    >I assume you didn't understand my question.
    Actually, I did. But to answer it would be to solve the problem for you, and that takes all of the fun out of it.

    >The code should output what apoc wanted it to.
    While it's possible to only use one loop (I've posted code that does so on this forum before), such a solution isn't meant for beginners and is primarily a curiosity for experienced C users to while away the time. A beginner's solution will always use nested loops.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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