DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: diagrams

  1. #1
    Join Date
    Oct 2002


    Hi i am trying to make a program that prints a symmetrical figure like so: (excluding the periods)

    but,, the problem is, i need it to ask the user how many lines the figure has, and print a figure like the above, but with as many lines as the user requires,, the above figure has 5 lines for example,, in the pattern 1 *, 3 *, 5*, 3* , 1* ........ but i cannot figure out how to make the program print out the stars and lines according to the amount of lines a user inputs...
    any help would be appreciated..

  2. #2
    Join Date
    Nov 2002
    Well to begin with I take it that the user can only input odd line numbers? As it would not be possible to create such a shape with an even number of lines.

    You need to work out a) how many spaces you need beforehand and b) how many stars are required on each line:

    E.g. the user enters 5 lines; to print...
    • line 1 you need 2 spaces and 1 star
    • line 2 you need 1 space and 3 stars
    • line 3 you need 0 spaces and 5 stars
    • line 4 you need 1 space and 3 stars
    • line 5 you need 2 spaces and 1 star

    The best way is probably to start from the middle, so to print n lines you need to:
    (n+1)/2 = middle line, so 0 spaces and n stars
    ((n+1)/2) +1 = line below middle, so 1 space and n-2 stars
    and ((n+1)/2) - 1 = line above middle, so the same
    and ((n+1)/2) + x = xth line below middle, so x spaces and (n-(2*x)) stars

    So (if you're still following this!), you need a for loop that puts n stars on a string (the middle line) then inside a while loop, you need to create some more "for" loops that add the correct number of stars and spaces to each line either side of the middle and exit when you get to n lines.

    Have a go, and if you get stuck I can probably post some more specific code!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.