0

I have been working on this problem set for a couple of days now and am still having issues with:

  1. getting the pyramid to print the right number of # marks

  2. getting the right number of spaces to print

when I check on check50 the errors returned are the following and I'm not sure how to interpret these.

:( handles a height of 0 correctly
   \ expected an exit code of 0, not output of "\n"
:( handles a height of 1 correctly
   \ expected output, but not "#\n##\n"
:( handles a height of 2 correctly
   \ expected output, but not "*##\n###\n####\n"
:( handles a height of 23 correctly
   \ expected output, but not "**********************#################..."
:( rejects a height of 24
   \ expected output, but not "**********************#################..."

Here is my code for this portion where l=line number, h=height, s=spaces c=hashtags

//print out each line  
for(l = 0; l < h + 1; l = l + 1)
  {

//to align right
     for(s = h - l - 1; s > 0; s = s -1)
     {   
         printf("*");
     }
 //to print hashtags
     for(c = 2; c <= (h - 1 + l); c = c + 1) 
       { 
           printf("#");
       } 
       printf("\n");
0

Don't know if you understand how the output is reading, so I'll go over it. For example:

 :( handles a height of 0 correctly
   \ expected an exit code of 0, not output of "\n"

On the first line, the frown (and that it is in red) means this part failed. the text says what it was testing.

The second line is present when there's an error. It says what it expected, in this case, an exit code of 0 followed by the output from your program, in this case "\n" It will print out the literal of "\n" instead of generating a line feed. Anyways, that's how it tells you what it saw.

Now, for the code.

First, you're printing the wrong number of lines. If you run your program and look at the output, height=1 produces 2 lines. Check your starting index.

Next, you are printing asterisks where you should be printing spaces, but let's just assume that this is temporary so that you could count them. (It's hard to count spaces.)

As for number of spaces and number of hashtags, you have an 'off by 1' problem in both.

Basically, you've made your math more complicated than it needs to be. You have a lot of 'off by 1' errors going on. Think about it this way. For height of 1, how many hashtags? For two? and so on? Is there a simple formula to determine the maximum number of hashtags for x lines? Same logic applies to spaces, sort of.

Also, did you notice that the total spaces + hashtags on each line is significant? That may or may not help you later.

If you're satisfied with this answer, please mark this question as answered. Let's keep up on forum maintenance. ;-) If not, please add comments.

| improve this answer | |
  • Thank you Cliff B! After reading through your answers and fumbling around a little bit more I FINALLY got the program to run correctly and it successfully went through the check. – HMiddleton Apr 22 '15 at 17:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .