Craig Rodrigues!

Learning to Code: Week 2 – mario.c

5/20/16

  • Loops: while, do-while, for
  • Boolean values: and is &&, or is ||, not is !
  • Compilers: Pre-processor, compilation, assembly, linking
  • Functions: Saves time in programming
  • Libraries: CS50 has their own library.
  • Make & Clang: “Make” is made by CS50
  • Precedence: Which operations go first? ++i vs i++
  • Style: Comments and in-line vs multi-line comments
    • Indentation of 4 spaces
    • Don’t need { if only 1 line
    • Variable names (don’t make them hard to understand for someone else), don’t make them too long and use underscores to separate words in variables.
    • * next to pointer’s type and not the variable. No idea what a pointer is yet.

5/21/16

Week2Lecture2

5/22/16

Worked on program pset1 water.c

// Goal:
// A program that prompts the user for the length of his or her shower in minutes
// (as a positive integer) and then prints the equivalent number of bottles of water 
// (as an integer) per the sample output below, wherein underlined text represents
// some user’s input.
// Output Example:
// minutes: 10
// bottles: 120
// Givens:
// 192oz of water used per minute when showering.
// 16oz of water in a regular water bottle.

Thought Process:

  1. Prompt the user for the number of minutes showered.
  2. If not a positive integer then retry.
  3. Once we have a positive integer showertime then take that number and multiply by 192 and then divide by 16 to get the number of bottles.
  4. Round up or down when calculating the number of bottles?
  5. Print that number of bottles used to the screen.
  6. Done.

My solution below:

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

int main(void)
{
    printf("How long did you shower today?\n");
    int time = GetInt();
    int bottles = (time * 192)/16;
    printf("minutes: %i\nbottles: %i\n", time, bottles);
}

Next program task is itsamario.c

// Goal:

// Write, in a file called mario.c in your ~/workspace/pset1 directory,
// a program that recreates this half-pyramid using hashes (#) for blocks.
// However, to make things more interesting, first prompt the user for the 
// half-pyramid’s height, a non-negative integer no greater than 23.
// (The height of the half-pyramid pictured above happens to be 8.) 

// If the user fails to provide a non-negative integer no greater than 23,
// you should re-prompt for the same again. Then, generate (with the help of
// printf and one or more loops) the desired half-pyramid.

// Take care to align the bottom-left corner of your half-pyramid with
// the left-hand edge of your terminal window, as in the sample output below,
// wherein underlined text represents some user’s input.

// Note that the rightmost two columns of blocks must be of the same height.
// No need to generate the pipe, clouds, numbers, text, or Mario himself.

// By contrast, if the user fails to provide a non-negative integer no
// greater than 23, your program’s output should instead resemble the below,
// wherein underlined text again represents some user’s input.
// (Recall that GetInt will handle some, but not all, re-prompting for you.)
// Output Example:

height: 8
       ##
      ###
     ####
    #####
   ######
  #######
 ########
#########

5/23/16

No laptop power, so I wrote out my thought process and code for itsamario on paper for an hour or so.

5/24/16

Unfortunately had no time to code. Tried to read a bit of the Code book. Frustrating. 😞

5/25/16

Worked on and completed itsamario.c

Problems/Thoughts:

  • In for loop I used a comma instead of a semi-colon which I couldn’t figure out for a bit.
  • If I declare a variable inside of a loop it can’t be used outside of that loop? Had to put the height outside of the do-while loop and then it worked.
  • I created my first loop for the spaces first and used 0 instead of a space so I could actually see what was being printed.
  • Changed 0 to a space after the program fully worked.
  • Getting the loops to work for the spaces and #’s was kind of a pain the ass, but basing it off of the row number i made it much easier and cleaner than when I did it before.
  • I will post what my old solution was from whenever.
  • My do-while loop conditions weren’t correct at first.
  • Fixed after the check program gave me the errors.

My solution below:

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

int main(void)
{
    // have to initialize the variable outside the do-while loop
    int height = 0;

    // get the height from the user within the limits
    do
    {
        printf("Height:");
        height = GetInt();

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

    // loop to control the number of times a line is printed
    for (int i = 0; i < height; i++)
    {
        // this loop controls the spaces
        for (int j = 0; j < height-i-1; j++)
        {
            printf(" ");
        }

        // this loop controls the #s
        for (int k = 0; k < i+2; k++)
        {
            printf("#");
        }

        printf("\n");
    }

}

Learning to Code: Week 3 - Vigenere Cipher

Learning to Code: Week 1 - "hello, CS50"