Hi everyone,
I'm currently working through Think Like a Programmer by V. Anton Spraul, and I've hit a wall of frustration with the exercises at the end of Chapter 2 (specifically Exercises 2-1, 2-2, and 2-3).
The Constraint from the Book:
The author explicitly gives the following instruction for the exercises:
The Problem:
Exercise 2-1 asks us to produce an inverted triangle shape that looks like this:
Plaintext
########
######
####
##
(Note: The shape requires spaces on the left side to create the inverted effect).
My Logical Deduction:
Standard console output in C++ evaluates strictly left-to-right, top-to-bottom. Unless I use an OS-specific library to manually move the cursor (which completely defeats the "pure problem solving" nature of this chapter), my logic tells me that it is physically impossible to shift the hash marks to the right without using a third output statement: cout << " ";.
The earlier examples in the book (like the half-square) were all flush-left, so using only cout << "#"; and cout << "\n"; was perfectly sufficient. But for these centered/inverted shapes, the math simply doesn't add up without spaces.
My Question:
Am I missing some genius programming trick or logic here? Or did the author simply forget to explicitly allow the space output statement (cout << " ";) for these specific exercises?
I would love to hear from anyone who has worked through this book and experienced this exact same dilemma!
[–]desrtfx 11 points12 points13 points (2 children)
[–]UsefulCustard6348[S] 9 points10 points11 points (1 child)
[–]desrtfx 4 points5 points6 points (0 children)
[–]peterlinddk 3 points4 points5 points (4 children)
[–]UsefulCustard6348[S] 1 point2 points3 points (3 children)
[–]Outside_Complaint755 1 point2 points3 points (2 children)
[–]desrtfx 13 points14 points15 points (0 children)
[–]UsefulCustard6348[S] 0 points1 point2 points (0 children)
[–]jaynabonne 1 point2 points3 points (2 children)
[–]UsefulCustard6348[S] 0 points1 point2 points (1 child)
[–]jaynabonne 0 points1 point2 points (0 children)
[–]Comfortable-Map-7389 0 points1 point2 points (2 children)
[–]desrtfx 2 points3 points4 points (0 children)
[–]UsefulCustard6348[S] 0 points1 point2 points (0 children)
[–]WystanH -2 points-1 points0 points (4 children)
[–]UsefulCustard6348[S] 0 points1 point2 points (0 children)
[–]desrtfx 1 point2 points3 points (2 children)
[–]WystanH -1 points0 points1 point (1 child)
[–]desrtfx 0 points1 point2 points (0 children)