• Advertisement

Archived

This topic is now archived and is closed to further replies.

a question on nested loops

This topic is 5742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You mean something like this?

int i;
for(i = 1; i <= 25; i++)
{
int j;
for(j = 1; j <= i; j++)
printf("*";
printf("\n";
}

Share this post


Link to post
Share on other sites
Here's a version that does work.


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]

Share this post


Link to post
Share on other sites
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--;
}
}

Share this post


Link to post
Share on other sites

  • Advertisement