0

Okay friends this is actually my school work.
My teacher asked me to make a diamond asteriks.

I just try and with my code

#include<iostream>
using namespace std;
int main()
{
char b='*';
for(int i=10;i>0;i--)
         {
          for(int j=0;j<i;j++)
          {
           cout<<" ";
          }
          for(int c=10;c>i;c--)
          {
           cout<<b;        
          }
          for(int a=10;a>i;a--)
          {
           cout<<b;        
          }
          cout<<""<<endl;
         }
for(int f=0;f<10;f++)
        {
         for(int d=0;d<f;d++)
         {
          cout<<" ";       
         }
         for(int g=10;g>f;g--)
         {
          cout<<b;        
         }
         for(int g=10;g>f;g--)
         {
          cout<<b;        
         }
         cout<<""<<endl;             
        }
 cin.ignore(cin.rdbuf()->in_avail()+1);
 return 0;   
}

I can displayed something like this

How can I displayed like this


with only one asteriks at the start.

Thanks.

Attachments 2010-02-25_222346.jpg 2.04 KB 2010-02-25_232714.jpg 2.28 KB
4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by ne0_komodo
1

Line #12: Initialize 'c' to 9 instead of 10.

Line #32: Initilize 'g' to 9 instead of 10.

Edited by Clinton Portis: &quot;Diamonds on my neck. diamonds on my neck. Smokin' pounds of green. Pounds of green.&quot; -A famous rapper

0

By not having TWO loops to print *'s

Calculate how many * each row should have, and then have one loop to do that.

0

Wow... you answered quickly than I imagined...

Thanks bro... you help me a lot.

0

Hello, I know you did not ask for this, but I just wanted to point out that you don't actually need three 'for' loops within both of your main 'for' loops. You can actually just use two.
As well, you can make it so you don't need so many different variables. This will allow you to create a 'master' value which will change your whole triangle size by just editing one number. This may come in handy in the future if you plan on getting a user's input to determine the size of the diamond.
Test out this code that I just typed, to see what I mean. I hope this helps you. =)

#include<iostream>
using namespace std;
int main()
{
	int r=10,i=10; //i is the 'master' value. Change this to change the whole diamond.
	for(i;i>0;i--)
	{
		for(int j=0;j<i;j++)
			cout << " ";
		for(int c=9;c>r;c--)
			cout << "*";        
		r-=2;
		cout << endl;
	}
	for(i;i<10;i++)
	{
		for(int d=0;d<i;d++)
			cout << " ";       
		for(int g=9;g>r;g--)
			cout << "*";  
		r+=2;
		cout << endl;             
	}
}

Edited by restrictment: n/a

Votes + Comments
Isn't that what Salem said 6 hours ago? And don't give out homework answers.
0

Hello, I know you did not ask for this, but I just wanted to point out that you don't actually need three 'for' loops within both of your main 'for' loops. You can actually just use two.
As well, you can make it so you don't need so many different variables. This will allow you to create a 'master' value which will change your whole triangle size by just editing one number. This may come in handy in the future if you plan on getting a user's input to determine the size of the diamond.
Test out this code that I just typed, to see what I mean. I hope this helps you. =)

#include<iostream>
using namespace std;
int main()
{
	int r=10,i=10; //i is the 'master' value. Change this to change the whole diamond.
	for(i;i>0;i--)
	{
		for(int j=0;j<i;j++)
			cout << " ";
		for(int c=9;c>r;c--)
			cout << "*";        
		r-=2;
		cout << endl;
	}
	for(i;i<10;i++)
	{
		for(int d=0;d<i;d++)
			cout << " ";       
		for(int g=9;g>r;g--)
			cout << "*";  
		r+=2;
		cout << endl;             
	}
}

Okkay bro but why I must put a new variable on the second loop instead we can use the same variable from above loop (int c, int j)?

This is my code... (from yours actually)

#include<iostream>
using namespace std;
int main()
{
 int r=10,i=10;
 for(i;i>0;i--)
 {
  for(int j=0;j<i;j++)
  {
   cout<<" ";
  }
  for(int c=9;c>r;c--)
  {
   cout<<"*";
  }
  r-=2;
  cout<<endl;           
 }
 for(i;i<10;i++)
 {
  for(int j=0;j<i;j++)
  {
   cout<<" ";        
  }          
  for(int c=9;c>r;c--)
  {
   cout<<"*";        
  }
  r+=2;
  cout<<endl;     
 }
 cin.ignore(cin.rdbuf()->in_avail()+1);   
}

And can you explain me what the variable r used for?

Thanks bro... hehehe... i just curious.. :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.