# Using a for loop to make a diamond...

• 11-06-2007
wco5002
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.
• 11-06-2007
King Mir
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.
• 11-06-2007
wco5002
My professor stated that we would need 4 loops to execute this properly, but is it possible to implement with only one?
• 11-06-2007
SRS
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.
• 11-06-2007
King Mir
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.
• 11-06-2007
wco5002
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?
• 11-06-2007
hk_mp5kpdw
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... ;)
• 11-06-2007
Daved
Don't forget to calculate how many spaces to output before the first star.
• 11-07-2007
wco5002
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); }```
• 11-07-2007
wco5002
Still stuck, any help?
• 11-07-2007
A10
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.