Thread: hashTag halfshape Problem Contraints Ignored

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    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;
    
    }
    Attached Files Attached Files
    Last edited by Salem; 07-31-2019 at 08:03 AM. Reason: Added code tags to see the real pattern

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,016
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,016
    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.
    Last edited by laserlight; 07-31-2019 at 09:24 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    So if we assume the constraint is that you can use two output statements...the code above works as a solution

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,016
    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.
    Last edited by laserlight; 07-31-2019 at 09:44 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,376
    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, "########" );
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. Replies: 4
    Last Post: 10-16-2008, 07:30 PM

Tags for this Thread