a question on nested loops
for an excersize in a book i''m trying to make a program that uses nested loops to put the following on the screen
*
***
*****
*******
*********
***********
*************
...and so on...
i figure that i need an outer loop that goes down each line and two inner loops to print spaces and asteriks...
do i use for loops for all these? i''ve messed around with it but just can''t figure it out... a push in the right direction would be great, thanks!
Do you mean this:
*
***
*****
*******
*********
***********
*************
or this
Because the first can be done with a single for loop, as can the second. But the second contains spaces, I don''t see where the spaces would be in the first.
------
Shop for the Lowest Price!
Then, Check a Reseller''s Rating Before You Purchase!
*
***
*****
*******
*********
***********
*************
or this
* *** ***** ******* ********* ************************
Because the first can be done with a single for loop, as can the second. But the second contains spaces, I don''t see where the spaces would be in the first.
------
Shop for the Lowest Price!
Then, Check a Reseller''s Rating Before You Purchase!
I apologize, I meant the second one with the spaces
I''m an idiot and I forgot to use the HTML to format it properly.
Thanks,
I''m an idiot and I forgot to use the HTML to format it properly.
Thanks,
You mean something like this?
int i;
for(i = 1; i <= 25; i++)
{
int j;
for(j = 1; j <= i; j++)
printf("*";
printf("\n";
}
int i;
for(i = 1; i <= 25; i++)
{
int j;
for(j = 1; j <= i; j++)
printf("*";
printf("\n";
}
Here's a version that does work.
EDIT: Even better version
[edited by - aNonamuss on June 1, 2002 5:04:03 PM]
[edited by - aNonamuss on June 1, 2002 5:05:00 PM]
EDIT: Even better version
[code]#include <iostream>#include <cstdlib>using namespace std;void main(void){ for (int astr = 1, astr <= 13; astr += 2) { for (int spaces = 1; spaces < ((23 - astr) / 2); spaces++) { cout << " "; } for (int totalAstr = 1; totalAstr < astr; totalAstr++) { cout << "*"; } for (int spaces = 1; spaces < ((23 - astr) / 2); spaces++) { cout << " "; } cout << endl; } system("PAUSE");} [/code]
[edited by - aNonamuss on June 1, 2002 5:04:03 PM]
[edited by - aNonamuss on June 1, 2002 5:05:00 PM]
I have just found the right algorithm for your request, take this code below and compile it...
#include <iostream>using namespace std;void main(void){ int columns; cout << "Enter how many columns there are(keep this value under 30): "; cin >> columns; int star_indi = 1; for (int i = columns; i > 0; --i) { for (int i = columns; (i-1) > 0; --i) { cout << " "; } for (int k = 0; k < star_indi; k++) { cout << "*"; } cout << endl; star_indi += 2; columns--; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement