C++ progrm help (visual studio 2013)

I need help with a program i have to write.
"Your program should ask the user for a number and then print two squares and two triangles out of stars whose size is equal to the number input. Here is an example dialog:
Input number: 4

****
****
****
****

****
***
**
*

****
 ***
  **
   *

****
*  *
*  *
****

Note that your program has to work for an arbitrary integer, not just 4. You can use either while or for looping constructs."
I have the first two figures down, but i cannot for the life of my figure out the second two figures.

#include <iostream> 
using namespace std;

int main()
{
	int a;
	cout << "Enter your number: ";
	cin >> a;
	for (int b = 0; b<a; b++)
	{
		for (int c = 0; c<a; c++)
			cout << "*";
		cout << endl;
	}
	cout << endl;
	for (int b = a; b > 0; b--)
	{
		for (int c = 0; c<b; c++)
			cout << "*";
		cout << endl;
	}
	cout << endl;
	for (int b = 0; b < a; b++)
	{

	}


}
Last edited on
Could you use the code tags and output tags, because:
* The code is then easier to read and refer to.
* Case 3 looks now identical to case 2.
sorry about that
closed account (2UD8vCM9)
For the third one, it looks like you'll have a nested for loop.

You'll have to first print the number of spaces for each line, and then print the number of stars.

So if we pass in 4, for the first line, you'll have 0 spaces, 4 stars
next line is 1 space, 3 stars
next line is 2 spaces, 2 stars
next line is 3 spaces, 1 star


You could do it like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream> 
using namespace std;

int main()
{
	int a;
	cout << "Enter your number: ";
	cin >> a;
	for (int b = 0; b<a; b++)
	{
		for (int c = 0; c<a; c++)
			cout << "*";
		cout << endl;
	}
	cout << endl;
	for (int b = a; b > 0; b--)
	{
		for (int c = 0; c<b; c++)
			cout << "*";
		cout << endl;
	}
	cout << endl;
	for (int b = 0; b < a; b++) //b = number of spaces
	{
		int NumberOfStars = a - b; //Calculate number of stars
		for (int i = 0; i < b; i++) //For loop to print spaces
			cout << " "; //print space
		for (int i = 0; i < NumberOfStars; i++) //for loop to print stars
			cout << "*"; //print star
		cout << endl;//new line
	}
	cout << endl;


	return 0;
}


Do you think you can figure out the logic for the last one by yourself or do you not know how to approach it?
closed account (2UD8vCM9)
For the last one, here is a possible approach.

For this example, let's assume that N is the number that they type in. I know you use a, but that can be a bit confusing for this example.

So let's say the user types in 4.
N=4

The first line will be N stars (4 stars)
The lines between the first and last will be 1 star with N-2 spaces and 1 star at the end
The last line will be N stars (4 stars)

It's actually even simpler than the 3rd case, so hoping you can get the code figured out for this on your own.
Thank you for your help! I understand the logic for the third figure now, but im having trouble with the fourth because I'm not sure how to write it out.
If it is the first line - then print a star
else if it is the last line - then print a star
else if it is the first column - then print a star
else if it is the last column - then print a star
else print a space
closed account (2UD8vCM9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (int b = 0; b < a; b++) //for each line
{
	int NumberOfSpaces = a - 2; //Calculate number of spaces
	if (b == 0 || b == (a - 1)) //If we are on first or last line
	{
		for (int i = 0; i < a; i++) //for loop to print stars
			cout << "*";
		cout << endl; //new line after row is done
	}
	else //If we are not on first or last line
	{
		cout << "*"; //Print first star in row
		for (int i = 0; i < NumberOfSpaces; i++) //for loop to print spaces
			cout << " ";
		cout << "*"; //Print last star in row
		cout << endl; //new line after row is done
	}
}
maybe i am confused because it is not supposed to look like the first three figures, or is it? im not sure
OHHH alright, that first if statement was what got me. I have no clue there was an actual way to write to differentiate the first and last line like that. thank you so much for your help!
Topic archived. No new replies allowed.