ahh i can't figure out this loop

This is a discussion on ahh i can't figure out this loop within the C Programming forums, part of the General Programming Boards category; ok i have a loop that will make a diamond, i can't get the freaking dimensions right, i dontk now ...

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

    Question ahh i can't figure out this loop

    ok i have a loop that will make a diamond, i can't get the freaking dimensions right, i dontk now what to change, i've been fiddlin around with the variables but can't figure out what to change by how much, can anyone help? heres the code:

    Code:
    //top of diamond or pyramid
    			for (row = 1; row <= height1 - (height1/2); row++)
    			{
    				for (space = height1; space >= row; space--)
    					printf(" ");
    				for (col = 1; col <= (row * 2 - 1); col++)
    					printf("*");
    				printf("\n");
    			}
    			//bottom of diamond
    			for (row = 1; row <= height1 - 1; row++)
    			{
    				for (space = 1; space <= row + 1; space++)
    					printf(" ");
    				for (col = 1; col <= height1 * 2 - (row * 2 + 1); col++)
    					printf("*");
    				printf("\n");
                   }
    and that makes a diamond with height 5 that looks like:
    Code:
       *
      ***
     *****
    *******
     *****
      ***
       *
    but i need one with height 5 that looks like:
    Code:
      *
     ***
    *****
     ***
      *
    can anyone help? i know this may sound stupid but i've gone trhough and i just can't figure this out.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It appears as if you were trying to be too clever with your loops, try something like this:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int space;
      int height1 = 5;
      int row, col;
    
      //top of diamond or pyramid
      for (row = 1; row <= (height1/2 + 1); row++)
      {
        for (space = height1; space >= row; space--)
          printf(" ");
        for (col = 1; col <= (row * 2 - 1); col++)
          printf("*");
        printf("\n");
      }
    
      //bottom of diamond
      for (row = height1 / 2; row >= 1; row--)
      {
        for (space = height1; space >= row; space--)
          printf(" ");
        for (col = 1; col <= (row * 2 - 1); col++)
          printf("*");
        printf("\n");
      }
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  4. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 12:31 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

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