Hey everyone, so i have a program that i found in the textbook im currently studying which leaves me a bit confused. Theres no examples of it nor can i find any examples online which can address how the code itself looks. I understand the concept of it and i am willing to explain the steps i would like to take to solve it, but i do not know how to write the code out itself as i have not learned this portion of the code yet. May anyone be willing to help me write out the code if i provide the steps that i wish to take? Here is the problem in any case:
Problem 1:
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).
1 2 3 4 5 6 7 8 9 10 11 12
|
(7,7) o
|
(6,6)
|
| |
(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).
Note 1: while this will require a loop to execute a cin statement 2n times, you
can still input all the characters in one shot as a string of length 2n. Regardless
of how you do it, the program will block until you have entered 2n characters.
(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.
Now, i can explain my thought process here for how i want to write out the code, i just dont know the syntax of it as i cant find any examples to get myself going. Here is what i want to do:
-for part a) i understand im simply inputting an (int n) and making that value equal to whatever the user wants to input from the start. How would i make int n look after i have used my generic, #include <iostream> and using namespace etd; ?
- i understand this is where follow along the path as the user is entering it. Basically, i think i 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. I based this on my prior chapters information but i dont know how to implement this in code.
Also, would i use an extra bool function here to check for invalid inputs by the user?
lastly for part c)
i know i should check if the user ever enters anything other than a u, U, r, or R but i am unaware of how to check this.
for the second bullet, this is where i should probably check if at any point the x coordinate exceeds n or the y coordinate exceeds the x coordinate right? otherwise the path goes off the grid? Once again, i dont know how to write this.
I think i would be done at this point and i would probably check if x and y have both reached the end?
Im hoping someone could atleast give me a portrayal of what i mapped out? I simply cant visual an example of this code as i have not found it anywhere else. Thanks so much for the help.