Using a for loop to make a diamond...

This is a discussion on Using a for loop to make a diamond... within the C++ Programming forums, part of the General Programming Boards category; Hi, I have to write a program that reads in a number from 5 - 25. That number corresponds to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Using a for loop to make a diamond...

    Hi,

    I have to write a program that reads in a number from 5 - 25. That number corresponds to the number of rows that the program will display in the shape of a diamond using asterisks (*). For example, if the user types in '7', then the program would display something like this:

    Code:
             *
            * *
           *   *
          *     *
           *   *
            * *
             *
    (I couldn't get the first * to line up, but you get the point)

    I was planning on using a 'for loop' to implement this, but I'm not quite sure where to begin, any suggestions on setting up the loops would be great.

    Thanks, I'll post back with progress.
    Last edited by Salem; 11-06-2007 at 11:22 AM. Reason: Use correct tags

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Presumably, you were planning to loop through each row. If so, what is the formula for printing out spaces and as a function of the row?

    You can also loop through each column, or each square (thus making two loops). In this case the procedure is the same; Express the printed value as a function of the loop variable(s).

    In this case looping through rows is probably simplest.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    My professor stated that we would need 4 loops to execute this properly, but is it possible to implement with only one?

  4. #4
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Very curious...

    Can you post all of the code you have for this project? I am very new in C++ and this looks like a good next step for me to look at. Thank you.
    Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    I would implement it with one loop. Possibly reprompting the user could take another one or two loops.

    But it depends on what your loop variables are. Pick whatever loop variables you think will provide the simplest function. Write out in words how to draw that diamond.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    From what I can tell a simple algorithm would look like this:

    1. Input the number of rows from 5-25
    2. Implement a loop

    a. Beginning at row '0', the loop will show one asterisk (*)
    b. At row '1' the loop will implement a space and a second asterisk (*)
    c. The loop will continue implementing spaces until (rows/2 - 1), which is when the middle row is reached
    d. The loop will reverse, which is why I believe two loops are necessary

    3. Display the resulting diamond

    Here's a first shot:

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    
    int    counter=0, rows;
    
    cout << "Enter the number of rows" << endl;
    cin >> rows;
    
    for (counter=0; counter <= rows; counter++)
    I'm not sure how to display the asterisks, some help?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Yes, it is quite possible using a single loop. This somewhat recent thread discusses a very similar problem. With some modification, you'd be to eliminate the interior *'s...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Don't forget to calculate how many spaces to output before the first star.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    I've been trying to adapt the code, but I'm still having a few problems, anyone with any advise is great. Here's the code:

    Code:
    //CSE 121, Diamonds
    
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    
    int spaces, stars, rows, counter;
       
    cout << "Please enter the number of rows you would like..." << '\n' << endl;
    cin >> rows;
    
    counter = rows / 2;
    
    for (int line = 1; line <= counter + 1; line++)
    {
          spaces = abs (line - 5);
          
          for (int loop = 1; loop <= spaces; loop++)
    		cout << " ";
            int stars_2 = line - 2;
             
          for (int loop = 1; loop <= stars_2; loop++)
    		cout << "*";
    		cout << endl;  
       }
       
    //Bottom half of the diamond
    
       for (int line = 1 ; line <= counter + 1; line++)
       {
          spaces = line;
    
          for (int loop = 1; loop <= spaces; loop++)
    		cout << " ";
    		stars = line + (line - 1);
    
          for (int loop = 1; loop <= stars; loop++)
    		cout << "*";
    		cout << endl;
       }
    
       
       return (0);
    }

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Still stuck, any help?

  11. #11
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Let me give you some tips. This assuming all row values are going to be odd.

    the first and last '*' will have (rows/2) spaces in front of it. if row is an int the decimal will be ignored I'd leave those outside all your for loops as the rows will always be >=5.

    For the rest of the rows remember this: you're calculating the number of spaces before the first '*' and the number of spaces before the second '*'.

    You should have a loop that prints spaces depending on what line it's on(rows/2 - line) Int's ignore decimals so they always round down. Then Print an '*'. Print the number of spaces to the next loop (rows- previous loops # of spaces printed - 2 to account for the '*' you already printed and the '*' you will print).Then print '*'. Put a for loop around the two for loops you just made that decrements line (assuming you initiate line to equal rows as all odd diamonds are square).

    Reverse for bottom.

    Hope that helps.

    what I wrote above is slightly flawed. You'll easily be able to fix what I told you once you compile an look at the output though.
    Last edited by A10; 11-07-2007 at 09:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 08:51 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM

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