# Outline of diamond using for loop

Page 1 of 1

## 1 Replies - 12084 Views - Last Post: 01 September 2008 - 07:54 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=62399&amp;s=ecadea9eaeab02766dd1e2fd57c4892b&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 praktikal

Reputation: 0
• Posts: 16
• Joined: 01-September 08

# Outline of diamond using for loop

Posted 01 September 2008 - 07:13 AM

```#include<iostream.h>
void main()
{

int i , j, height;

cout<<"------------------------------\n"
<<"		 Diamond Shape		\n"
<<"------------------------------\n"
<<"Enter a Height of the Diamond: ";
cin>>height;
cout<<endl;
if ((height%2)!=0)
{
for(i = 0; i < (height/2 + 1); i++)
{
for(j = i; j < (height/2); j++)
cout << " ";
for(j = 1; j <= (i*2 + 1); j++)
cout << "*";
cout << endl;
}
for(i = (height/2); i > 0; i--)
{
for(j = (height/2 + 1); j > i; j--)
cout << " ";
for(j = (i*2 - 1); j > 0; j--)
cout << "*";
cout << endl;
}
}
else
cout<<"Height should be Odd Number\n";

cin.ignore();cin.ignore();
}

```

I already have this code and it is for a shaded diamond. I can't think any idea to change it into an outline.

Is This A Good Question/Topic? 0

## Replies To: Outline of diamond using for loop

### #2 Martyr2

• Programming Theoretician

Reputation: 5178
• Posts: 13,898
• Joined: 18-April 07

## Re: Outline of diamond using for loop

Posted 01 September 2008 - 07:54 AM

The idea is that you print asterisks only when the value of your inner loop is 1 or at the max value that j can be. Anything in between will be a space.

```void main()
{

int i , j, height;

cout<<"------------------------------\n"
<<"         Diamond Shape        \n"
<<"------------------------------\n"
<<"Enter a Height of the Diamond: ";
cin>>height;
cout<<endl;
if ((height%2)!=0)
{
for(i = 0; i < (height/2 + 1); i++)
{
for(j = i; j < (height/2); j++)
cout << " ";
for(j = 1; j <= (i*2 + 1); j++) {
// Print stars when j is 1 or at the max value
// All others are spaces.
if ((j == 1) || (j == (i*2 + 1))) { cout << "*"; }
else { cout << " "; }
}

cout << endl;
}
for(i = (height/2); i > 0; i--)
{
for(j = (height/2 + 1); j > i; j--)
cout << " ";
for(j = (i*2 - 1); j > 0; j--) {

// Same here, print starts only when j is at max value and 1
// Spaces otherwise
if ((j == (i*2 - 1)) || (j == 1)) { cout << "*"; }
else { cout << " "; }
}
cout << endl;
}
}
else
cout<<"Height should be Odd Number\n";

cin.ignore();cin.ignore();
}

```

You can see my changes with the comments in the code above. Enjoy!

"At DIC we be star printing code ninjas... heck we are just stars in the world of programming."