5 Replies - 1703 Views - Last Post: 20 November 2012 - 09:48 PM Rate Topic: -----

#1 dmg59912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-November 12

i need help with printing a hollow cetered triangle in c++/...

Posted 20 November 2012 - 03:21 PM

for( int row =1;row <= size; ++row )
			{
				for( int count=row;count<=size;++count)
					{
						cout << " ";
					}
					 for(int col =1;col <=row;++col)
					 {
						cout << " *";
					 }
					cout << endl; 
			}


this is what I have so far, I cant find the logic in making his triangle hollow please need some guidance
.

This post has been edited by macosxnerd101: 20 November 2012 - 03:23 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0
  • +

Replies To: i need help with printing a hollow cetered triangle in c++/...

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 728
  • Joined: 27-June 09

Re: i need help with printing a hollow cetered triangle in c++/...

Posted 20 November 2012 - 04:01 PM

//replace this
for(int col =1;col <=row;++col)
{
     cout << " *";  
}  

//with this
for(int col =1;col <=row;++col)
{
     if(/*figure out what goes here*/)
     {cout<<"  ";}
     else
     {cout << " *";}  
}  



Think about when you need to print stars and when you need to print spaces
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: i need help with printing a hollow cetered triangle in c++/...

Posted 20 November 2012 - 04:49 PM

Three for loops just muddies up the logic. Common though. Try two loops - the outer one for the row counter and it's logic, and the inner for loop for the printing of the char itself at the column level.

After that, use if statements to guide you. You have variables - row counter, column counter, and you know where the edge of the diagram is - and that's the only place you will print a visible char. Everything else will be an interior space char.

Become the printhead or cursor, and block out everything else with paper and pencil. Draw the figure yourself, as the cursor. Repeat a couple of times, and you WILL see the patterns start to form up in your mind. Those patterns are the backbone of the logic you need, for this program.

A little subtraction and addition of variables, completes the job.

This is too good a puzzle for you to work on, for me to give an answer. Get out that paper and pen and maybe a shovel too -- time to dig, dig, dig into it! ;)
Was This Post Helpful? 0
  • +
  • -

#4 dmg59912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-November 12

Re: i need help with printing a hollow cetered triangle in c++/...

Posted 20 November 2012 - 05:30 PM

[quote name='mojo666' date='20 November 2012 - 04:01 PM' timestamp='1353452504' post='1750952']
//replace this
for(int col =1;col <=row;++col)
{
     cout << " *";  
}  

//with this
for(int col =1;col <=row;++col)
{
     if(/*figure out what goes here*/)
     {cout<<"  ";}
     else
     {cout << " *";}  
}  



Think about when you need to print stars and when you need to print spaces

ok thanks I got this far im still having some trouble
if (row > 1 && row < size && col > 1 && col < size )

with that im able to make work something like this
*
*
*
* * * *
I just can figure out the left right side

[quote name='dmg59912' date='20 November 2012 - 05:28 PM' timestamp='1353457711' post='1750986']

View Postmojo666, on 20 November 2012 - 04:01 PM, said:

//replace this
for(int col =1;col <=row;++col)
{
     cout << " *";  
}  

//with this
for(int col =1;col <=row;++col)
{
     if(/*figure out what goes here*/)
     {cout<<"  ";}
     else
     {cout << " *";}  
}  



Think about when you need to print stars and when you need to print spaces

ok thanks I got this far im still having some trouble
if (row > 1 && row < size && col > 1 && col < size )

with that im able to make work something like this
=----*
----*
- *
* * * *
I just can figure out the left right side

Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: i need help with printing a hollow cetered triangle in c++/...

Posted 20 November 2012 - 05:36 PM

Use if statements inside the inner for loop. Let the loops work for you! Simplify the initial top line of the for loop. Let that loop work for you.
Was This Post Helpful? 0
  • +
  • -

#6 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 728
  • Joined: 27-June 09

Re: i need help with printing a hollow cetered triangle in c++/...

Posted 20 November 2012 - 09:48 PM

Quote

if (row > 1 && row < size && col > 1 && col < size )


Very close. The issue is with "col < size". Let's look at the for-loop condition: "for(int col=1;col<=row;++col)"
Are we printing spaces whenever col is less than "size" or something else?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1