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++)
{
}
}
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
#include <iostream>
usingnamespace 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?
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
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
}
}
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!