[prog] controlling print out of a program
wolf
wolf at wolfrising.net
Mon Dec 22 19:18:40 EST 2003
Hello again : ) I am trying to work on this sample question and having
a limited amount of luck : )
write a program that prints the following patterns separately one below
the other. Use for loops to generate the patterns. All astericks (*)
should be printed by a single statement of the form cout << '*" ( this
causes the astericks to print side by side). The last two
patterns require that the program begin with the appropriate number of
blanks. Combine your code from the four separate problems
into a single program that prints all four patterns side by side making
a clever use of nested for loops.
I'm not sure if these patterns will show up correctly in email but
basically it's four triangles made out of astericks I didn't know if it
was acceptable to include an attachment so I tried to type them out : )
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** ****
*******
******** *** ***
********
********* ** **
*********
********** * *
**********
Here's the bit of code I have so far, it prints the first two correctly
but I can't get it to print the second two. It seems like it should
be something simple as in just having to have two additional for loops
and switch things around a bit, but I'm having no
luck at all. And I'm completely baffled how you'd get them to print
side by side. I'm using C++. It's not a trick question
is it? And it's actually not possible to do?
#include <iostream>
#include <iomanip>
int main ()
{
int i,j; // loop controls
for (i=1; i<=10; i++) {
for (j=1; j<=i; j++) {
std::cout<<"*";
}
std::cout<<std::endl;
}
std::cout<<std::endl<<std::endl;
for (i=10; i>0; i--) {
for (j=i; j>0; j--) {
std::cout<<"*";
}
std::cout<<std::endl;
}
std::cout<<std::endl<<std::endl;
return 0;
}
More information about the Programming
mailing list