Write a C++ program with a nested loop structure that displays a pyramid/ isosceles triangle in "*".
And so far I've only been able to make an equilateral triangle like this:
*
***
*****
*******
*********
***********
*************
***************
Right now I'm trying to make the spaces to make it a pyramid. Here's my current code:
#include <iostream>
using namespace std;
int main ()
{
int i, j;
for (int i=0; i<16; i+=2)
{
for (int j=0; j<=i; j++)
{
cout << "*";
}
cout << endl;
}
system ("pause");
return 0;
}
I've been thinking that I need to add a third variable 'k' to determine the spaces, but I'm not sure where to put its statement in my for loop or how I get it to interact with the other variables (or if I need it to interact with the other ones at all)! So far when I try to add variable k with a statement like:
for (int k=8; k>=0; k--)
{
cout << " ";
}
I usually get never-ending *s! I once got a rectangle, but I can't remember how I got that now...
Any hints would be awesome, no spoilers on the code please! I really want to learn how to do this! Even a hint on how to make it right justified would be uber-awesome! Please and thanks!
- C++ newb.

New Topic/Question
Reply



MultiQuote





|