help needed with loops pls x

This is a discussion on help needed with loops pls x within the C Programming forums, part of the General Programming Boards category; Hi I need to write a program that will print a diamond shape using astericks, i can only use printf ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Unhappy help needed with loops pls x

    Hi

    I need to write a program that will print a diamond shape using astericks, i can only use printf for one character so i will have to use loops. The user needs to be able to input the amount of rows they would like. and then the program print the diamond with that amount of rows.

    I made this program below to just print a diamond with 9 rows for another piece of work. Is there any way i can just alter it so that i can input the amount of rows without having to change the code to much?

    any help very much appreciated thankyou,

    Code:
    #include <stdio.h>
    
    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");
        }
     
       getch(0);
       return(0);
      
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Create a variable.

    int n;

    Then request the user to enter a number.

    STore into that variable. Replace 9 with n.
    You may have to adjust any arithmetic with caution, so it fits a general case scenario

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    You may also want to create another variable

    int o;

    And this can be used to control the bottom half. Maybe, o = n - 2;

    Test it and see what needs to be done.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Talking

    hi thanks really aprreciated that worked, didnt need the second variable just altered code slighty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed for sorting nested loops...segmentation error
    By starvinmarvin in forum C Programming
    Replies: 17
    Last Post: 12-01-2008, 07:13 PM
  2. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 09:46 AM
  3. C programming - pls pls help me
    By sally arnold in forum C Programming
    Replies: 10
    Last Post: 01-16-2002, 03:55 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:46 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

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