Learning to Code: Week 4 – Algorithms

Algorithms Summary

6/1/16 – Continued

6/2/16

  • Shawn helped me look into making a function for the water.c program. I worked on it for a bit.
  • Error 1 – 0 arguments instead of 2.

image08

  • Error 2 – variable bottles but nothing there. Call the function INTO that variable?

image28

  • Boom, got it to work. That was pretty cool.
    • Now I can account for variable shower heads.
    • I think the input question could be better to explain what the metric is for waterflow etc.
  • I didn’t think of rounding errors.

Code Here: https://gist.github.com/CraigRodrigues/aeff70c00abf8fd4628f19e57c837a55

I put a function in Caesar.c!

Code Here: https://gist.github.com/CraigRodrigues/6e3724fbe3bd5fed9d05d2cafcedbc5c

  • Not sure about the names of the variables though. I think it works okay.
  • Also not sure if functions should be in printf or on a separate line going into another variable?
    • Example: printf(“%c”, encryptCode(code[i], k));

Continue reading →

Learning to Code: Week 3 – Vigenere Cipher

Week 3 lecture

Another week down! Here is what happened.

5/26/16

Randomly looked at Slack’s job requirements:

I worked on greedy.c

PROGRAM GOAL:

Write a program that first asks the user how much change is owed and then spits out the minimum number of coins with which said change can be made. Assume that the only coins available are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢).

We ask that you use GetFloat so that you can handle dollars and cents, albeit sans dollar sign. In other words, if some customer is owed $9.75 (as in the case where a newspaper costs 25¢ but the customer pays with a $10 bill), assume that your program’s input will be 9.75 and not $9.75 or 975.

However, if some customer is owed $9 exactly, assume that your program’s input will be 9.00 or just 9 but, again, not $9 or 900.

Using GetFloat alone will ensure that the user’s input is indeed a floating-point (or integral) value but not that it is non-negative. If the user fails to provide a non-negative value, your program should re-prompt the user for a valid amount again and again until the user complies.

MY THOUGHT PROCESS/STEPS:

  • Ask user for non-negative float number.
  • Reject if negative or not a number.
  • Take the float number into a variable called amount.
  • Multiply amount by 100 to turn dollars and cents into just a variable called cents.
  • Cast that number to an int (how?) –http://cs50.stackexchange.com/questions/2356/roundf-function-gives-error-in-clang-but-runs-smoothly-in-g
  • Have a loop that checks if the number > 25, then > 10, then >5, then adds in the rest as pennies.
  • Start with quarters (25)
  • Divide number by 25.
  • Take integer part of the number and add to a coin counter variable called change.
  • Take the remainder (modulo?), add it to the coint counter, and move down the loop until the end.
  • Print the final coin counter variable.
  • Math.h library can round and what else can it do?
  • How do I deal with rounding?
  • I like to test stuff with printf after a loop.
  • Actually using modulo from what I remember from the lectures/shorts I don’t actually NEED any loops to do this problem set.
  • Not sure how to overcome a rounding error for really larger numbers though (although it’s not checked for and probably out of the scope right now)

Solved!

See screenshot of my solution below.

I wanted to add multi-line comments so I looked at Harvard’s style guide here: https://manual.cs50.net/style/

Submitted Problem Set 1.

Shawn mentioned functions. Can I write a function for greedy?

Functions: Saves time in programming

In your water program, you could have extracted the calculation to a function that would be reusable in other programs, and would be easy to put in automated unit tests. As a general rule, if I can see using something more than once, I’ll extract to function. That saves copy/paste and maintenance hell later.

On the typo in the for loop, welcome to the rest of your career. Won’t be the last time that you beat your head against the wall over a typo. I’ve had apps where my issue was a spelling error that it takes hours to see.

-Shawn

Continue reading →

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.

Continue reading →

Learning to Code: Week 1 – “hello, CS50”

I’ll try and keep my posts brief with just some notes and thoughts I’ve written over the week. I’ve started with Harvard’s CS50 intro course. The stuff I write is more for me than anything else. I can never forget that.

5/12/16

  • I will study at Panera Bread daily.
  • Looked through instructions and FAQs
  • Will try to finish in 1 month, but that will take some hardcore work.
  • $90 for a CS50 certificate (proof I finished) = good motivation?
  • Finished the pre-course survey
  • Downloaded first lecture

5/13/16

  • Week 0 Lecture completed (@ 150% speed)
  • Transcribed RIVAS goal process in notebook
  • Binary Numbers
  • Algorithm – how to solve a problem step by step.
  • Pseudocode
  • They have PDF of notes!
  • Scratch

Continue reading →