Problem Set 1: Mario (More/Less Comfortable)

Evidence of a 5-point submission

  • do-while loop; the code should feature a do-while loop to get the pyramid’s height. It should begin with a declaration, not an initialization: with int height, not int height = 0, to avoid initializing height with an arbitrary value. The integer defined in the do-while loop really ought to be called something that clearly distinguishes that it has something to do with height, or the pyramid, or the pyramid’s height.
  • Getting height; the get_int function should include an argument that lets users of the program know what it is they’re supposed to input.
  • Concise arithmetic; a good submission should have relatively uncomplicated arithmetic for figuring out the bounds for each of the inner loops. There are multiple ways to achieve this. The staff solution provides the upper bound for complexity; whatever their solution, nothing should look more complicated than j < height - i - 1, for instance. Adding or subtracting one is completely fine. Solutions that tinker a bit to avoid having to write -1 or +1 are also appreciated, as long as they do not interfere with correctness or clarity along the way.
  • Nested loop; a single doubly-nested loop should print out the pyramid; any extraneous or unnecessary parts should be removed. The best names for the loop variables are i and j; others might be distracting. It’s OK if the inside of the loop is done in two parts (or more, if we’re talking about the More Comfortable version), like so:
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < height - i - 1; j++)
    {
        printf(" ");
    }

    for (int j = 0; j < i + 1; j++)
    {
        printf("#");
    }
}

…or if it’s done in a single inner loop with conditionals, like so…

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < height; j++)
    {
        if (j < height - i - 1)
        {
            printf(" ");
        }
        else
        {
            printf("#");
        }
    }
}

Evidence of a 4-point submission

  • Comparable to a 5-point submission, but is marred with a few small design mistakes, such as:
    • Initializing the height variable before the do-while loop
    • The arithmetic to get the loop bounds is unnecessarily convoluted
    • The get_int function might lack an argument
    • If the student uses a single inner loop with conditionals to print out the pyramid, they might use two if statements instead of an if-else.

Evidence of a 3-point submission

  • Comparable to a 4-point submission, but with one or two more serious errors, such as:
    • Using a while loop instead of a do-while loop to get the user’s input
    • Using an unnecessarily complicated structure to print out the pyramid
    • The arithmetic to get the loop bounds is extremely convoluted
  • Still, a 3-point submission should have the structure of a good solution; a while/do-while loop to ask for the height, and a doubly nested loop to print the pyramid out

Evidence of a 2-point submission

  • Comparable to a 3-point submission, but with enough small mistakes and/or serious errors to make awarding a 3 impossible
  • Might include a wrong approach to a part of the problem; e.g., if the student tried to print the pyramid in a single loop instead of a doubly nested loop

Evidence of a 1-point submission

  • Solution is rife with incorrect approaches that reflect misunderstandings of the week’s concepts
  • Might not leverage the power of loops to print the pyramid
  • Might hard-code different cases for each potential height

Example Implementations (Worse vs. Better)

Getting the Height

Worse Implementation

Vague variable name, magic number ‘0’, missed opportunity for do-while loop, no argument given to get_int:

int n = 0;
while (n < 1 || n > 8)
{
    n = get_int(" ");
}

Better Implementation

Meaningful variable name, no magic numbers, uses a do-while, and clear argument given to get_int:

int height;
do
{
    height = get_int("Height?: ");
}
while (height < 1 || height > 8);

Printing the Pyramid

Worse Implementation

Unidiomatic loop variable names, two ifs when if-else is preferable, convoluted math

for (int x = 0; x < height; x++)
{
    for (int y = 0; y < height; y++)
    {
        if (y < height - 1 - x)
        {
            printf(" ");
        }
        if (x > y - 1)
        {
            printf("#");
        }
    }
}

Better Implementation

i-j loop variable names, relatively simple math, logically clean conditionals

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < height; j++)
    {
        if (j < height - i - 1)
        {
            printf(" ");
        }
        else
        {
            printf("#");
        }
    }
}