### Dont know how to make this loop work

hey all, so im really confused as to how to solve a loop like this nor as to how it would look. Can anyone attempt to code this?

Consider the following triangular grid that grows indefinitely. You start at the
bottom left corner (0, 0) and you follow a path by making a series of UP and
RIGHT steps on the grid. Your goal is to get to a point (n, n).

(7,7) o
|
(6,6) o---o
| |
(5,5) o---o---o
| | |
(4,4) o---o---o---o
| | | |
(3,3) o---o---o---o---o
| | | | |
(2,2) o---o---o---o---o---o
| | | | | |
(1,1) o---o---o---o---o---o---o
| | | | | | |
(0,0) o---o---o---o---o---o---o---o

Write a program to do the following:
(a) reads from the input the value of n.
(b) reads 2n characters, each of which is either u or U (for UP), or r or R (for RIGHT)
(c) outputs exactly one of the following in order of priority:
• “the path contains invalid steps” if any of the 2n characters is not in the
set {’u’, ’U’, ’r’, ’R’}.
• “you fell off the edge”, if the path takes you outside the grid.
• whether the path takes you to your destination (n, n) or not.

Thank you so much in advance!
I think you'll want to use the loop to read the data out of the file, then just keep an ordered pair to represent the position. Whenever you read a u or U, increment the y, and on a r or R increment the x. Then you just need to include checks for the other conditions mentioned.
Okay, I am going to assume that the grid actually looks like this:
 ``` (7,7) o | (6,6) o---o | | (5,5) o---o---o | | | (4,4) o---o---o---o | | | | (3,3) o---o---o---o---o | | | | | (2,2) o---o---o---o---o---o | | | | | | (1,1) o---o---o---o---o---o---o | | | | | | | (0,0) o---o---o---o---o---o---o---o```

Part a) should be easy.

For part b), just follow along the path as the user is entering it. Basically, just have two variables for the x and y coordinate (starting at (0,0)), and when the user enters a U, increment the y coordinate, and when the user enters an R, increment the x coordinate.
You would also need an extra `bool` variable or two to keep track of whether the user entered any invalid characters and whether the user went off the grid (see below).

For part c):
-- The first bullet should be easy (just check if the user ever enters anything other than a u, U, r, or R).

-- For the second bullet, if at any point the x coordinate exceeds n or the y coordinate exceeds the x coordinate, then the path goes outside of the grid.

-- The third bullet should be easy if you kept track of the x and y coordinates.
Just check if x and y are both n at the end.
Topic archived. No new replies allowed.