1

I started with mario.c from hacker1 problem set. I found a tricky and fast solution (when building up really high pyramids > 1000 rows) to build up the pyramids using printf's align functionallity combined with strcat function.

Code:

/**
 * Program     : mario.c
 * Edition     : hacker1
 * Version     : 2.0
 * Author      : Frank Geister
 * Contact     : frank.geister@t-online.de
 * Description : prints out mario pyramids using printf align and strcat
 */

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

#define MIN_HEIGTH 0
#define MAX_HEIGTH 23

int main (void)
{
    int heigth = 0;

    do
    {
        printf("Heigth: ");
        heigth = GetInt();
    }
    while ((heigth < MIN_HEIGTH) || (heigth > MAX_HEIGTH));

    //calculate max size of array to hold all hashes, 2 spaces and string terminator
    char hashes[(MAX_HEIGTH * 2) + 3] = "#";

    for (int i = 0; i < heigth; i++)
    {
        printf("%*s  %-*s\n", heigth, hashes, heigth, hashes);
        strcat(hashes, "#");
    }

    return 0;
}

However, I got two questions.

  1. Which output does check50 expect for heigth 2 and 23, program builds pyramids correctly?:

~/workspace/hacker1/ $ check50 2015.fall.hacker1.mario mario.c

...

:( handles a height of 2 correctly

\ expected output, but not " # # \n## ##\n"

:( handles a height of 23 correctly

\ expected output, but not " # # ..."

...

  1. I'm an absolute beginner in C programming, normally I do a lot of Perl scripting and I don't have to care about memory segmentation faults, so my second question is, is the following statement correct and will the program add \0 automatically to the array?

    //calculate max size of array to hold all hashes, 2 spaces and string terminator
    char hashes[(MAX_HEIGTH * 2) + 3] = "#";
    

Thanks a lot!

CS50 rocks :-)

0
0

I think I already got an answer to question 1.

The check50 checks for spaces after last hash from right site (trailing spaces). Changing the statment to the following will fix expected output messages:

    printf("%*s  %-*s", heigth, hashes, 1 + i, hashes);
0

Not sure you got it right, but to me, the problem looks like you have one space between the left half and the right half when there should be two spaces, per the instructions.

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

1
  • Thanks for your answer. I already got it, it was a problem of trailing spaces, see my answer below. Can you say something about question 2? Aug 25 '16 at 23:41
0

@FrankGeister: that is a sweet algorithm.

As for your second question, you should not need to worry about adding a NULL terminator to an array. That's something you'll worry about later, when you start dealing with strings and char* pointers. CS50 makes all these things pretty painless at first. In Module 1, you definitely won't need to worry about the difference between the Stack and the Heap.

1

You must log in to answer this question.

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