I wanted to share a problem solving exercise from the
second chapter in the book "Think Like A Programmer".
The program below is mean't to produce this pattern
from hashtags:
The constraints are that - only two output statementsCode:######## ###### #### ##
can be used - one that outputs the hashtag and one that
outputs the end-of-line
With at in mind i believe I have cheated with the code
below by using the string variable "spaceAllocated" with
the second output statement.
Does anyone see an alternative solution that strictly
fits within the problem constraints?
Many thanks,
J
Code:#include <iostream> using namespace std; int main() { int hashNum; int row; char spaceFor1stRow[] = " "; char spaceFor2ndRow[] = " "; char spaceFor3rdRow[] = " "; string spaceAllocated; int i = 0; for(row = 1; row <= 4; row++) { if (row == 1) { spaceAllocated = spaceFor1stRow; } if (row == 2) { spaceAllocated = spaceFor2ndRow; } if (row == 3) { spaceAllocated = spaceFor3rdRow; } for (hashNum = 1; hashNum <= 10 - (row*2); hashNum++) { cout << "#"; } cout << "\n" << spaceAllocated; } return 0; }