Hey Everyone, in class we are learning about for loops, which are very easy. However the project the teacher gave us is very difficult. Here is the assignment:

Write a program that prints the following patterns seperately one below the other. Use for loops to generate the patterns. All asterisks(*) should be printed by a single statement of the form "cout << '*';". Hint: The last two patterns require that each line begin with an appropriate number of blanks.

(A)               (B)                      (C)                      (D)
*                   **********      **********                    *
**                  *********        *********                   **
***                 ********          ********                  *** 
****                *******            *******                 ****
*****               ******              ******                *****
******              *****                *****               ******
*******             ****                  ****              *******
********            ***                    ***             ********
*********           **                      **            *********
**********          *                        *           **********

I figured out how to make each image separately. But I am confused as to how I am supposed to make them all with just ONE statement of "*".

BTW, this program can only use the heading code of #include<iostream>

Edited 7 Years Ago by restrictment: n/a

oh yeah, I forgot to show the code that I have made so far...this code prints out the first two triangle examples..however I stopped there because I used 2 "cout << '*'", and I only need 1...

#include <iostream>
using namespace std;
int main()
{
	int counter = 0;
	for(int time=0; time <= 9; time++)
	{
		counter++;
		for(int time2 = 1; time2 <= counter; time2++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cout << endl << endl;
	counter = 0;
	for(int time=11; time >= 0; time--)
	{
		counter++;
		for(int time2 = 10; time2 >= counter; time2--)
		{
			cout << "*";
		}
		cout << endl;
	}

}

Edited 7 Years Ago by restrictment: n/a

The spacing between A and B is changed because it was huge but other than that its the same.

#include <iostream>
using namespace std;

int main()
{
	for( int i = 1; i <= 10; i++ )
	{
		for( int c = i; c >= 1; c-- )
			cout << "*";
		for( int c = 15-i; c >= 1; c-- )
			cout << " ";
		for( int c = 10; c >= i; c-- )
			cout << "*";
		for( int c = 5; c <= 6+i*3; c++ )
			cout << " ";
		for( int c = 10; c >= i; c-- )
			cout << "*";
		for( int c = 26-i*2; c >= 1; c-- )
			cout << " ";
		for( int c = i; c >= 1; c-- )
			cout << "*";
		cout << endl;
	}

	system("PAUSE");
	return 0;	
}

The spacing between A and B is changed because it was huge but other than that its the same.

#include <iostream>
using namespace std;

int main()
{
	for( int i = 1; i <= 10; i++ )
	{
		for( int c = i; c >= 1; c-- )
			cout << "*";
		for( int c = 15-i; c >= 1; c-- )
			cout << " ";
		for( int c = 10; c >= i; c-- )
			cout << "*";
		for( int c = 5; c <= 6+i*3; c++ )
			cout << " ";
		for( int c = 10; c >= i; c-- )
			cout << "*";
		for( int c = 26-i*2; c >= 1; c-- )
			cout << " ";
		for( int c = i; c >= 1; c-- )
			cout << "*";
		cout << endl;
	}

	system("PAUSE");
	return 0;	
}

Thanks for the code, however I can only use a single statement of cout << "*";.

Edited 7 Years Ago by restrictment: n/a

So the way I did it was wrong? I don't see how you could do it with only one cout << "*"; statement for all the shapes.

Yeah, neither can I. This is an extra credit assignment, and the teacher deemed it extremely difficult..but there must be a way. I got this assignment from dietel&dietel third edition, p154, assignment 2.47.

Edit: It's possible that I just misunderstood it wrong. The quote in the thread is directly from the book, and is what I am basing this off of.

Edited 7 Years Ago by restrictment: n/a

@OP
Your teacher probably meant to use only one cout statement per shape. It doesn't seem possible to print 4 different figures with only one cout statement in the entire program. But, hey I can be wrong.
You should send an email to your teacher to clarify the problem.

Edited 7 Years Ago by Grn Xtrm: n/a

@OP
Your teacher probably meant to use only one cout statement per shape. It doesn't seem possible to print 4 different figures with only one cout statement in the entire program. But, hey I can be wrong.
You should send an email to your teacher to clarify the problem.

Alright, I will talk with him tomorrow about the program. If there is a solution that will print out all shapes with one statement, I will be sure to post it after the answer is revealed. Thanks again sfuo for the code.

Well are things wrong with my code:

1) I based it off the picture and the 3rd shape was slopped and now I see you edited that so my output is wrong now.
2) The shapes you showed are all side by side but if you read what the assignment says it wants them one below another.

I am rewriting it now so it should work.

Took me a while but I got it.
This uses only one cout << '*'; and according to the first post and my last post the outputs are not side by side but one below another.

I know there are no comments but the whole thing was confusing enough for myself.

#include <iostream>
using namespace std;

int main()
{
	for( int i = 0; i < 4; i++ )
	{
		for( int c = 0; c < 10; c++ )
		{
			int sadd = 0, fadd = 0;
			int xstart, xfinish;
			switch(i)
			{
				case 0:
					xstart = 1;
					xfinish = 10;
					
					fadd = -(9-c);
					break;
				case 1:
					xstart = 10;
					xfinish = 1;
					
					sadd = c;
					break;
				case 2:
					xstart = 1;
					xfinish = 10;
					
					sadd = c;
					
					for( int x = 10; x > 10-c; x-- )
						cout << ' ';
					break;
				case 3:
					xstart = 10;
					xfinish = 1;
					
					fadd = -(9-c);
					for( int x = 1+c; x < 10; x++ )
						cout << ' ';
					break;
			}
			
			if( xstart > xfinish )
			{
				xstart *= -1;
				xfinish *= -1;
			}
			
			for( int x = xstart+sadd; x <= xfinish+fadd; x++ )
				cout << '*';
			cout << endl;
		}
		cout << endl;
	}
	
	system("PAUSE");
	return 0;
}
Comments
Great programmer!

Took me a while but I got it.
This uses only one cout << '*'; and according to the first post and my last post the outputs are not side by side but one below another.

I know there are no comments but the whole thing was confusing enough for myself.

#include <iostream>
using namespace std;

int main()
{
	for( int i = 0; i < 4; i++ )
	{
		for( int c = 0; c < 10; c++ )
		{
			int sadd = 0, fadd = 0;
			int xstart, xfinish;
			switch(i)
			{
				case 0:
					xstart = 1;
					xfinish = 10;
					
					fadd = -(9-c);
					break;
				case 1:
					xstart = 10;
					xfinish = 1;
					
					sadd = c;
					break;
				case 2:
					xstart = 1;
					xfinish = 10;
					
					sadd = c;
					
					for( int x = 10; x > 10-c; x-- )
						cout << ' ';
					break;
				case 3:
					xstart = 10;
					xfinish = 1;
					
					fadd = -(9-c);
					for( int x = 1+c; x < 10; x++ )
						cout << ' ';
					break;
			}
			
			if( xstart > xfinish )
			{
				xstart *= -1;
				xfinish *= -1;
			}
			
			for( int x = xstart+sadd; x <= xfinish+fadd; x++ )
				cout << '*';
			cout << endl;
		}
		cout << endl;
	}
	
	system("PAUSE");
	return 0;
}

Wow, that is awesome. Thanks a lot for the coding, I will be sure to use it as a reference in the future. Bravo!

This article has been dead for over six months. Start a new discussion instead.