Thread: repetition with the "for" structure

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Question repetition with the "for" structure

    thanks for your help the last time, guys. I now have a question re: the "for" structure.

    This code for printing a diamond has more than one "for" statement and I don't understand how to interpret it. I have seen the output, but I can't follow it from the code.

    Could someone please explain?

    here goes the code...
    Code:
    int main ()
    {
       int  line, space, asterisk;
    
       for (line = 1; line <= 9; line += 2)	          /* 	Top half	*/
       {
       for (space = (9 - line) /2; space > 0; space--)
             printf (" ");
    
       for (asterisk = 1;  asterisk <= line;  asterisk++)
             printf("*");
    
       printf ("\n");
        }
    
       for (line = 7;  line >= 0;  line -= 2)	          /*Bottom half	*/
       {
       for (space = (9 - line) /2; space > 0; space--)
             printf (" ");
    
       for (asterisk = 1;  asterisk <= line;  asterisk++)
             printf("*");
    
       printf ("\n");
        }
    
       return (0);
    }



    Code Tags added by Kermi3
    Last edited by Lillian; 10-19-2002 at 04:23 PM.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    humblest apologies

    i'm totally green. I didn't intend to flout your rules.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, first we'll indent the code better than your post has it. Then ignore the numbers for now, just read the flow of the code in blocks:
    Code:
    #include <stdio.h>
    
    int main ()
    {
      int  line, space, asterisk;
    
      /* Print the first 5 lines */
      for (line = 1; line <= 9; line += 2)
      {
        /* Print a decreasing number of spaces per line */
        for (space = (9 - line) /2; space > 0; space--)
          printf (" ");
        
        /* Print an increasing number of *'s per line */
        for (asterisk = 1;  asterisk <= line;  asterisk++)
          printf("*");
    
        /* Move to a new line */
        printf ("\n");
      }
      
      /* Print the last 4 lines */
      for (line = 7;  line >= 0;  line -= 2)
      {
        /* Print an increasing number of spaces per line */
        for (space = (9 - line) /2; space > 0; space--)
          printf (" ");
        
        /* Print a decreasing number of *'s per line */
        for (asterisk = 1;  asterisk <= line;  asterisk++)
          printf("*");
        
        /* Move to a new line */
        printf ("\n");
      }
      
      return (0);
    }
    Now you can figure out how the numbers interact to print the correct format, which is the easy part.

    >I didn't intend to flout your rules.
    No worries, kermi3 does that every time someone forgets to use code tags.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. I could use help with "for" structure
    By ruger in forum C++ Programming
    Replies: 14
    Last Post: 09-25-2003, 10:18 PM
  3. Controlling Repetition in a Structure
    By Silence in forum C Programming
    Replies: 2
    Last Post: 08-23-2002, 05:35 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM