# Thread: hashTag halfshape Problem Contraints Ignored

1. ## hashTag halfshape Problem Contraints Ignored

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:

Code:
```########
######
####
##```
The constraints are that - only two output statements
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;

}``` 2. Yes, there are alternative approaches within the problem constraints. Instead of enumerating the rows, observe a pattern in the decreasing number of symbols per row, and thereby write the nested loops to produce this pattern. 3. I guess you see an alternative nested loop to the one already there?

I need to enumerate four rows?
The pattern I observe in the decreasing symbols is that it decreases by two on a new row?
I just can't see how a space can occur on the left hand side of the symbol without using " " or ' '? 4. Oh wait, I just saw that your example output has changed. Unfortunately, this task is impossible: there appears to be spaces before the hex symbols that must be printed, but you are not allowed to print spaces, regardless of how many print statements you use.

If you are allowed to print spaces, then this can be solved with only one print statement; you can construct a string and print it at the end of each outer loop iteration. 5. So if we assume the constraint is that you can use two output statements...the code above works as a solution  6. Well, the problem seems to have been stated badly, so I'd just move on. If the constraint is purely the number of output statements to produce that particular output, then the best solution is incredibly simple: just output the string with that exact final output. No loops required.

EDIT:
Basically, I'd expect "thinking like a programmer" to follow a process along these lines:
• Can I do the simplest thing that could possibly work, i.e., just print the whole thing as a string constant? Presumably there's some variable aspect to make this impossible, which leads to:
• Okay, I can use an outer loop to print the rows, and for each row one nested loop to print spaces and another to print the hex symbols. Oh, keep the output statements to a minimum? That leads to:
• Fine, instead of printing character by character, I'll construct a string for each row and print that.

Your idea of using preconstructed strings consisting of spaces works (although it could be improved by storing them in an array of arrays), but why stop there? You could preconstruct entire rows, or just output the final result at once. 7. s/thinking like a programmer/thinking up stupid magic tricks with contrived requirements/

Code:
```#include <stdio.h>
int main ( ) {
for ( int row = 4 ; row >= 1 ; row-- ) {
int hashes = row * 2;
int spaces = 4 - row;
printf("%.*s%.*s\n", spaces, "    ", hashes, "########" );
}
}``` Popular pages Recent additions #### Tags for this Thread

char, constraints, int, output, problem 