1 Replies - 9729 Views - Last Post: 01 September 2008 - 07:54 AM Rate Topic: ***** 1 Votes

#1 praktikal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Please please help me.My deadline is on Thursday already Sept.4. :S

Is This A Good Question/Topic? 0
  • +

Replies To: Outline of diamond using for loop

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • 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.

So here is your function with the added pieces...

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." :snap:
Was This Post Helpful? 3

Page 1 of 1