Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The user decides on the stem size for the X (i.e., how long one "leg" of the X is). Use a validation loop to ensure their input is between 1 and 10 (inclusive).  Use nested loops to make the X appear on the screen.

For example, if the user decides on having a stem size of 3 for the X, your loops need to produce the following picture:


\    /
 \  /
  \/
  /\
 /  \
/    \


What I have tried:

int stemSize = 0;
			cout << "Enter the stem size for the X: ";
			cin >> stemSize;
			while (!(stemSize >= 1 && stemSize <= 10))
			{
				cout << "Enter the stem size for the X: ";
				cin >> stemSize;
			}
			for (int row = 1; row <= stemSize; row++){
				for (int col = 1; col <= stemSize; col++){
					if(row == col || col ==(stemSize +1) - row)
						cout << "\\";
					else
						cout << " ";
				}
				cout << endl;
			}
Posted
Updated 7-Oct-22 13:11pm
v3
Comments
Richard MacCutchan 7-Oct-22 15:37pm    
The first n rows need \/, and the last rows need /\. So you need two loops to print the relevant characters and their spaces.
gkau 7-Oct-22 15:42pm    
For the last rows loop, would it be the same, except changing the output?
Richard MacCutchan 7-Oct-22 15:45pm    
That is what I stated in my comment.

Think about what you are trying to do, and look at what patterns you have to work with.
There are two patterns:
1) The top half and the bottom half are mirrored.
2) Each line is mirrored around it's centre.

So start by writing a function that prints a line given it's total length (same for all), the line number (which decides how many spaces to print each time), the start and end characters.
In your example:
Line 0:   \    /    0 space, '\', 4 space, '/', 0 space
     1:    \  /     1 space, '\', 2 space, '/', 1 space
     2:     \/      2 space, '\', 0 space, '/', 2 space
     3:     /\      2 space, '/', 0 space, '\', 2 space
     4:    /  \     1 space, '/', 2 space, '\', 1 space
     5:   /    \    0 space, '/', 4 space, '\', 0 space

Given that, it's pretty easy to see that there are sequences here that can be exploited to print using two loops that call the same method: one going from 0 to N-1, and one going from N-1 to 0

Needless to say, the "trailing spaces" don't really need to be printed at all.

Think about it, and if you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
The basics given by OriginalGriff how to determine the positions as well as the hint that there are mirrored outputs helps already. Here is a suggestion how to implement it:
C++
char legchar[2] = { '\\', '/' };
int  step = 1;
bool lpart = false;			// draw lower part
for (int row = 0; row >= 0; row += step) {

// TODO: draw spaces and lines

}

When switching for the lower part, you could mirror the characters like this:
C++
swap(legchar[0], legchar[1]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900