Week 7

By now everyone should be done with skittles and will be moving on to Problem 1-5: Greedy and 1-6: Pennies.

One thing to keep in mind as you begin to work on more complex programs is good design. The program may pass check50 and style50, but do you use repetitive code to accomplish this? Can you use loops instead?

This Friday will be our first quiz.

Quiz0Review – These will be the topics on the quiz. Try to complete these before checking the answer document Quiz 0 Review Answers.

Advertisements

Week 6

A few student’s asked me to upload the slides to each day’s lessons, so here you go!

Monday: slides1-17 Imprecision
Tuesday: slides1-18 Using C References
Wednesday: slides1-19 Casting
For those who were out on Monday, Oct 19, we talked about integer overflow and floating-point imprecision (something you will deal with in Greedy!).

Integer Overflow

  • Integer overflow is a problem with limited sizes of variables. If we had one byte to store an integer that looked like this:
       128    64    32    16     8     4     2     1
    
         1     1     1     1     1     1     1     1
  • This number represents 255, but if we added 1 to this number, the value would carry over to become 0:
       128    64    32    16     8     4     2     1
    
         0    0    0    0     0    0    0    0
  • The example above demonstrates this concept with 8-bits, but in reality, the int datatype uses 4 bytes or 32 bits. One of these bits is reserved to keep track of whether the number is negative or positive, which leaves 31 bits to store the integer value. This means that the largest positive number we can represent with an int datatype is 2 to the 31st power minus 1. Since ints store both positive and negative numbers, the total number of numbers we can represent is twice this amount plus zero which equals 2 to the 32nd power.

Integer Division

1 #include <stdio.h>
2
3 int main(void)
4 {
5    float f = 1 / 10;
6    printf("%.1f\n", f);
7 }

The program should print out .1, but instead outputs 0.0.

This is because the value of 1 and 10 in line 5 is assumed to be of an integer, and the decimal value we expect is truncated, or thrown away. We still see 0.0, but only because %.1f, for one decimal point, is specified in line 6 to printf.

Floating Point Imprecision

Floating-point imprecision happens because the computer can’t represent an infinite number of real numbers. Instead, it gives us the closest number it can represent. Let’s look at the program below:

If we fix the code to:

1 #include <stdio.h>
2
3 int main(void)
4 {
5    float f = 1.0 / 10.0;
6    printf("%.1f\n", f);
7 }

We get 0.1.

Let’s see what happens when we print 28 decimal places:

1 #include <stdio.h>
2
3 int main(void)
4 {
5    float f = 1.0 / 10.0;
6    printf("%.28f\n", f);
7 }

We expect something like 0.100000000000…​ but we get: 0.1000000014901161193847656250

The video below shows what happens when we have what seem like very small errors that can add up.

Week 5

All students should now have a shared dropbox folder set up labeled with your name, period number, and “Submit”. This should be shared with me at mtanzosh@gmail.com. If you have a grade of “M” for this process, it is very likely you shared a file or perhaps created a link to your folder. Once the folder is shared it should look like this:

Screen Shot 2015-10-14 at 7.58.22 AM

Only with your name and period number instead of “Submit_Test”. I will change your grade to give you full credit for this once I see the folder is shared correctly.

The process of being graded and submitting work is as follows:

  • Show me in your terminal window that your program passes check50 and style50.
  • Control-click on the “.c” file icon on the left side of your IDE window to download the source code to your computer.
  • Log onto Dropbox and click on the shared folder icon to open the folder.
  • Drag the “.c” file to your dropbox window.
  • Delete your “.c” file from the computer.

Everyone should be finished with hello.c by now. If you don’t have a grade for this, please be sure to show me your check50 and style50 for this program. Remember that each program (except for skittles) has it’s own unique check50, while there is only one style50 to be used with all “.c” files.

The deadlines I am posting can be considered soft deadlines for now. You should be close to being finished with Fahrenheit, however you will not be penalized if you are still working on it. Once you have it working, move on to Skittles. And don’t forget to watch the videos if you have any questions on how to implement conditionals and loops.

And as your programs get more complex, it is important to style your code properly. Tommy does a great job in the video below, explaining how to use comments and style your code in C.

You can also refer to CS50’s Style Guide manual for more details on styling your code.

Week 4

We should now have our CS50 IDE set up and be ready to start programming in C. If you have not yet completed this, please do this as the first part of Problem 1-2: Hello.

So this week you should be completing: Problem 1-2: Hello. Make sure to follow instructions to check50 and style50 when you complete your program.

Then open Problem 1-3: Fahrenheit. Watch the videos (available through orange colored links) before attempting to solve this problem. This will use a new function and a new way to display data. Again follow instructions to use check50 and style50 when you believe your program is perfected.

Finally when the above is totally complete, move on to Problem 1-4: Skittles.