Get a 2 input from user, first one for count of step and 2nd one for direction (only right and left). Find the position after moving by left or right by the number of steps
What is a 2D coordinate system?
A coordinate system lets us describe positions using two numbers: (x, y)
Key components:
In our problem:
(0, 0)
💡 This is why Y always remains 0 in our solution!
🤔 The confusing part: Why initialize y = 0 if it never changes?
The Answer:
(x, y)
Visual representation:
What if the problem allowed UP/DOWN?
Then we'd add:
if (ch == 'U') y += a; // Move UP
if (ch == 'D') y -= a; // Move DOWN
💡 Key insight: We initialize y = 0
because it's part of the coordinate system, even though this specific problem doesn't use vertical movement!
The ||
(OR) operator returns TRUE if at least one condition is true.
Basic Syntax:
if (condition1 || condition2) {
// Executes if EITHER or BOTH are true
}
Truth Table for OR:
Condition 1 | Condition 2 | Result (||) |
---|---|---|
TRUE | TRUE | ✅ TRUE |
TRUE | FALSE | ✅ TRUE |
FALSE | TRUE | ✅ TRUE |
FALSE | FALSE | ❌ FALSE |
In our problem:
ch == 'l' || ch == 'L'
→ User entered lowercase OR uppercase Lch == 'r' || ch == 'R'
→ User entered lowercase OR uppercase RWhy we need this:
Users might type:
10 L
(uppercase) ← Valid10 l
(lowercase) ← Also valid10 R
(uppercase) ← Valid10 r
(lowercase) ← Also valid💡 This makes our code case-insensitive and user-friendly!
Short-circuit evaluation:
If ch == 'l'
is TRUE, the second condition ch == 'L'
is never checked! The result is already TRUE.
Compound assignment operators explained:
1. x += a (Add and assign):
x = x + a
a
to current value of x
2. x -= a (Subtract and assign):
x = x - a
a
from current value of x
In our coordinate system:
Direction | Operation | Effect on X | Example (a=10) |
---|---|---|---|
LEFT | x -= a | Decreases (negative) | 0 → -10 |
RIGHT | x += a | Increases (positive) | 0 → 10 |
Why this works:
x += 10
→ x = 0 + 10 = 10x -= 10
→ x = 0 - 10 = -10Other compound operators you'll learn:
x *= a
→ x = x * a (multiply)x /= a
→ x = x / a (divide)x %= a
→ x = x % a (modulo)💡 Compound operators save typing and make code cleaner!
Example 1: Input "10 L" (Move 10 steps left)
long long a; char ch;
cin >> a >> ch;
x = 0, y = 0
(at origin)if (ch == 'l' || ch == 'L')
'L' == 'l'
→ FALSE'L' == 'L'
→ TRUE ✅x -= a;
→ x = 0 - 10 = -10if (ch == 'r' || ch == 'R')
'L' == 'r'
→ FALSE'L' == 'R'
→ FALSE(-10,0)
Example 2: Input "10 R" (Move 10 steps right)
'R' == 'l' || 'R' == 'L'
→ FALSE || FALSE → FALSE ❌
'R' == 'r' || 'R' == 'R'
→ FALSE || TRUE → TRUE ✅x += a;
→ x = 0 + 10 = 10(10,0)
Example 3: Input "5 l" (lowercase, Move 5 steps left)
'l' == 'l' || 'l' == 'L'
→ TRUE || ? → TRUE ✅
x -= a;
→ x = 0 - 5 = -5(-5,0)
Example 4: Starting from non-origin (conceptual)
If we started at (5, 0) and moved 3 LEFT:
x -= 3
→ x = 5 - 3 = 2💡 This shows how movement is relative to current position!
How character comparison works:
1. Character literals use single quotes:
'L'
→ Character (single letter)"L"
→ String (text, even if one letter) ← WRONG for char2. Characters are case-sensitive:
Comparison | Result | Reason |
---|---|---|
'L' == 'L' | TRUE ✅ | Exact match |
'L' == 'l' | FALSE ❌ | Different case |
'R' == 'R' | TRUE ✅ | Exact match |
'R' == 'r' | FALSE ❌ | Different case |
3. Why we check both cases:
Without OR operator:
if (ch == 'L') // Only accepts uppercase L
if (ch == 'l') // Only accepts lowercase l
With OR operator:
if (ch == 'l' || ch == 'L') // Accepts BOTH!
Behind the scenes (ASCII values):
'L'
has ASCII value 76'l'
has ASCII value 108Alternative approach (converting to one case):
ch = toupper(ch); // Convert to uppercase
if (ch == 'L') ... // Now only check uppercase
💡 But using OR is more direct and doesn't modify the input!
🤔 Critical question: Why not use else-if?
Our code structure:
if (ch == 'l' || ch == 'L')
x -= a;
if (ch == 'r' || ch == 'R') // Separate if!
x += a;
Why this works perfectly:
Could we use else-if?
if (ch == 'l' || ch == 'L')
x -= a;
else if (ch == 'r' || ch == 'R')
x += a;
✅ Yes! This would work too!
Why two separate ifs is also valid:
When to use else-if vs separate ifs:
Use else-if when: | Use separate ifs when: |
---|---|
Categories are mutually exclusive | Conditions are independent |
You want to skip remaining checks | Multiple conditions might be true |
Efficiency matters (many conditions) | Each check has different purpose |
In this problem: Both approaches work! Two separate ifs is fine because:
💡 Best practice for this problem: else-if is slightly better because it makes the mutual exclusivity explicit!
Where this pattern is used in real programming:
1. Game Development - Character Movement:
if (key == 'W' || key == 'w') y += speed; // Move up
if (key == 'A' || key == 'a') x -= speed; // Move left
if (key == 'S' || key == 's') y -= speed; // Move down
if (key == 'D' || key == 'd') x += speed; // Move right
2. Robot Control Systems:
3. GPS Navigation:
4. Drawing Applications:
5. Map Applications:
6. Terminal/Console Navigation:
💡 This is fundamental to any system that tracks position or movement!
💡 Key Takeaways: (1) Y stays 0 because we only move horizontally, (2) OR operator handles both lowercase and uppercase, (3) Coordinate systems are fundamental to positioning in programming!
#include <iostream>
using namespace std;
int main() {
long long a;
char ch;
cin >> a >> ch;
long long x = 0, y = 0;
if (ch == 'l' || ch == 'L')
x -= a;
if (ch == 'r' || ch == 'R')
x += a;
cout << "(" << x << "," << y << ")";
return 0;
}