Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loop help

Status
Not open for further replies.
Mar 29, 2002
33
US
I’m trying to create a program that will output a pyramid/triangle. The pyramid/triangle will use the character 'X' as found below in the both programs. The maximum height of the pyramid/triangle is 20 as found below in both programs. The first program creates a one sided pyramid/triangle, the second program outputs the correct height, but increments the spaces and characters incorrectly. I know that each level of the pyramid will increment the character count by two and I’m also aware that the number of spaces needed is one less then the height and then decreases by one space per line (Example: height equals 5; spaces on line 1 equals 4, character equals 1 - spaces on line 2 equals 3, character equals 3 and son on…). Can anyone help me with examples or suggestions for completing this task?
Thank-

Output should look like the following for height 5:

x
xxx
xxxxx
xxxxxxx
xxxxxxxxx

----------------------------------------------------------------------------------------------------------------------
include<iostream>
using namespace std;

int main()
{
int h, n, s, j;
do
{
cout<<&quot;Enter pyramid height, must be fewer than 20 lines: &quot;;
cin>>n;
}
while (n>24);
j=h;
cout<<&quot;\n&quot;;
//j=h;
for(h=0; h<n; h++)
{
for(j=h; j>0; j--)
{
cout<<&quot; &quot;;
}
for(s=0; s<1; s++)
{
cout<<&quot;X&quot;;
}
cout<<&quot;\n&quot;;
}
cout<<&quot;\n&quot;;
return 0;
}


---------------------------------------------------------------------------------------------------------------------


#include<iostream>
using namespace std;

int main()
{
int h, n, s, j;
do
{
cout<<&quot;Enter pyramid height, must be fewer than 20 lines: &quot;;
cin>>n;
}
while (n>24);
j=h;
cout<<&quot;\n&quot;;
//j=h;
for(h=0; h<n; h++)
{
for(j=0; j<34; j++)
{
cout<<&quot; &quot;;
}
for(s=0; s<1; s++)
{
cout<<&quot;X&quot;;
}
cout<<&quot;\n&quot;;
}
cout<<&quot;\n&quot;;
return 0;
}

 
This will take me a little while to work out, but I can tell you right now, using variables that control loops inside the loop is generally a bad practice. I don't know yet if that is your problem however. Using the them inside the loop under some circumstances can cause unpredictable events. Some languages don't even allow it.

-Bones
 
This program I re-did is extremely close to what you want I believe. The major difference in mine is I avoided using the loop variables inside the loop. Is it possible for you to make the necessary modifications to get it to work your way from here?

#include <iostream>
using namespace std;

int main()
{
int n;
int currentRow = 0;

cout << &quot;Enter the number of rows: &quot;; cin >> n;
int l = n;
for (int i = 0; i < n; i++)
{
for (int k = 0; k < l; k++)
{
cout << &quot; &quot;;
}
l--;
currentRow++;
for(int j = 0; j < currentRow; j++)
{
cout << &quot;XX&quot;;
}
cout << &quot;\n&quot;;
}
return 0;
}


My output was nice except I didn't bother thinking on how to make it do two at a time:

XX
X
XXX
XXXXX
XXXXXXX
XXXXXXXXXXXX

when a 5 is entered. Let me know if it is a problem to take it from there, I didn't bother to check it out, sorry :/

-Bones
 
Have a string of spaces as wide as the pyramid will be.
For every iteration put a X at (middle-i) and one at (middle+i), and output the string.

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Whoops the X's messed up on my last post after I posted it for some reason. It basically looked like what you wanted except the top starts off with 2 X's.

-Bones
 
I’ll rebuild my program and try it again.

Thanks for all the help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top