d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Triangle Class
Add Reply New Topic New Poll
Member
Posts: 3,277
Joined: Sep 13 2009
Gold: 0.00
Sep 11 2014 11:53am
working on a project on building a class called triangle and i'm having a tough time with a member function that draws the actual triangle
it is supposed to create an equilateral triangle with a border character and a fill character

here is my code that I'm using just for the equilateral triangle, i've made the triangle but have no idea how to make it fill with a character. can someone point me in the right direction?

int main()
{
const int size = 6;
const char border = '#';
const char fill = '*';
const char BLANK = ' ';

for (int i = 0; i < size; i++)
{
for (int k = 0; k < size - i; k++)
{
cout << BLANK;
}
for (int j = 0; j <= i; j++)
{
// if (() || ()) <---- condition that will make any outside character be a border ?
cout << border << BLANK;
// else
// cout << fill << BLANK; <---- makes all other characters fill?
}


cout << '\n';
}
}

any pointers will be greatly appreciated :thumbsup:
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Sep 11 2014 12:33pm
j == 0 -> left border of the triangle
i == size-1 -> bottom
j == i -> right

Code
int main()
{

const int size = 6;
const char border = '#';
const char fill = '*';
const char BLANK = ' ';

for (int i = 0; i < size; i++)
{
for (int k = 0; k < size - i; k++)
{
cout << BLANK;
}
for (int j = 0; j <= i; j++)
{
if(j == 0 || i == size-1 || j == i ) cout << border << BLANK;
else cout << fill << BLANK;

}
cout << '\n';

}

}
Member
Posts: 3,277
Joined: Sep 13 2009
Gold: 0.00
Sep 11 2014 12:41pm
Quote (ShadowFiend @ Sep 11 2014 11:33am)
j == 0 -> left border of the triangle
i == size-1 -> bottom
j == i -> right

Code
int main()
{

const int size = 6;
const char border = '#';
const char fill = '*';
const char BLANK = ' ';

for (int i = 0; i < size; i++)
{
  for (int k = 0; k < size - i; k++)
  {
  cout << BLANK;
  }
  for (int j = 0; j <= i; j++)
  {
  if(j == 0 || i == size-1 ||  j == i ) cout << border << BLANK;
    else cout << fill << BLANK;
   
  }
  cout << '\n';

}

}



thank you! :hug:
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll