0

im having a hard time trying to understand how to print the hashes in increasing order. So far i know how to do the spaces(decreasing order) This is my code right now:

/*
* Ask the user for a height greater than 0 lower than 23
* Prints a mario castle tower.
*/

#include <stdio.h>
#include <cs50.h>

int main (void)
{
    int size;
    string hash = "#";  // hashes to print in increasing order
    string space = "*"; // for testing purposes

    do
    {
        size = get_int("Height: "); // gets the info from the user

    }while(size < 0 || size > 23);

    int count = 0;
    printf("Height is: %i\n", size);

    while(size  > count)
    {
        for(int i = 0; i <= size; i++)
        {

            for(int j = size -1 ; j > 0; j--)
            {
                printf("%s", space);
            }

            for(int k = 0 ;k > count ; k++)
            {
                printf("%s", hash+1);
            }
         }     printf("\n");

       // printf("\n");
        count++;
        size--;
    }

 }

What is the logic behind printing the hashes? SOS!

Regards.

1

Its funny because most people can print the hashes but not the spaces. Let's take a look at what you're printf statement is doing:

            printf("%s", hash+1);

My instinct says that you're trying to print a hash twice. However, remember that hash is really an address of a string, which contains a hash in the first position and the end of string marker, \0, or 0x00, in the second position. This statement says "print the string starting at the address stored in hash plus 1 byte." In other words, print \0. So, it'll never print anything. You need to print what is stored at hash, not hash+1, given your code.

You just need to work out the right formula for how many to print on each line. Remember that the first line has two hashes. You just need to tweak the start and stop values in your for loops. If it helps, try just printing the correct number of hashes without printing the spaces first. Then merge the working code. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thank you, this really helped me to finish this problem. Now I run check50 and is all green! – Michel Ortega Sep 11 '17 at 9:43

You must log in to answer this question.

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