JavaRush/Java Blog/Random EN/Harvard CS50: Week 1 Assignments (Lectures 3 and 4)
Masha
Level 41

Harvard CS50: Week 1 Assignments (Lectures 3 and 4)

Published in the Random EN group
members
Harvard CS50: Week 1 Assignments (Lectures 3 and 4) - 1Friends, you can glean basic theoretical information from the seminar notes . There, in addition to the basics of C, it describes how to connect to a special cloud IDE CS50 (this must be done to execute and check tasks), and describes the basic necessary Linux commands and language structures. If you don't find enough material about C in the lecture and notes, turn to other sources. For example, those listed at the end of this article. In the topic " Additional materials "
  • First week goals
  • IDE CS50
  • Command line and workbench update
  • Working in the IDE
  • Hello, C!
  • Bugs?
  • Validation check: check50 test
  • C Basics: Comparison with Scratch
  • Basic Data Types in C
  • Libraries C
  • Hello C again: analysis of the syntax of the simplest programs
  • A little more about I/O in C
Materials in this topic:
  • Validated data entry: special functions of the cs50.h library
  • Task 1. Smart calculation of water consumption
  • Task 2. Mario is with us!
  • Task 3. Time to get change
  • How to validate your code and get marks
  • Code resource
  • additional literature

Validated data entry: special functions of the cs50.h library

To make this course more convenient, we have developed a special CS50 library, which, in particular, has very useful functions for processing user-entered data.
  • GetString()reads the string entered by the user;

  • GetInt()reads the string entered by the user and checks whether it contains an integer;

  • GetFloat()reads the string entered by the user and checks whether it contains a floating point number;

  • GetLongLong()reads the string entered by the user and checks whether it contains a long real number.

Task 1. Smart calculation of water consumption

Harvard CS50: Week 1 Assignments (Lectures 3 and 4) - 2It’s logical: the longer you take a shower, the more water is spent on this process. Let's figure out how much? Even if your shower is barely open, approximately 6 liters of water flow out of it per minute. And this is 12 bottles of water that you carry with you for drinking. Usually a person takes a shower for about 10 minutes. In total, to wash, you need 120 half-liter bottles. Quite a lot! Create a file water.cin your ~/workspace/pset1. The program should count how many bottles of water are used for a shower depending on the time. That is:
  1. The program asks the user for the number of minutes spent in the shower
  2. User enters a positive integer
  3. The program displays the number of bottles used by the user.
username:~/workspace/pset1 $ ./water
minutes: 10
bottles: 120
For simplicity, this time we will assume that the user always enters the number of minutes correctly, that is, we do not check whether the number entered is positive and integer. Later we will learn how to write checks, but for now this is enough. To check that the program is running correctly using check50,you need to enter the following line in the terminal:
check50 2015.fall.pset1.water water.c
And if you want to see how the program waterwritten by the course staff works, run the following command:
~cs50/pset1/water

Task 2. Mario is with us!

Harvard CS50: Week 1 Assignments (Lectures 3 and 4) - 3Do you know the most famous plumber in the world? With the help of Nintendo, the fictional mustachioed and slightly plump guy in a red cap has become a hero for several generations of gamers. If you don't know who we're talking about, here's a link to the classic 1985 game : trust me, it's still good and worth checking out! You can also find a version of the classic Super Mario for smartphones or offline emulators. We need all this for general development, this, unfortunately, is not a task yet ;). And the task is this. At the end of the first Mario level, every player saw this half-pyramid: Create a file mario.cin your ~/workspace/pset1. Our program will draw a half-pyramid similar to the one you see, but directly in the console, without graphics: each of the blocks will consist of a hash icon (#). Even if you haven't figured out how to do it yet, believe me: it's easy. To make the problem more interesting, let's add to it the ability to set the height of the half-pyramid using a non-negative integer from 0 to 23. The height of the pyramid in the picture is considered to be in the highest place, that is, equal to 8. If the user enters the number incorrectly, you need to ask him to do it again once. Then generate (using printf a pyramid). Take care to align the bottom left corner of your half-pyramid with the left edge of the terminal window, as in the example below. The underlined text is what the user enters themselves.
username:~/workspace/pset1 $ ./mario

height: 8
       ##
      ###
     ####
    #####
   ######
  #######
 ########
#########
Notice that the two rightmost columns are the same height. It's not worth generating pipes, clouds and Mario himself yet =). At least for this task. If the user entered incorrect data (entered not a number, or entered a number that is less than one or greater than 23), the program should ask him to enter the data again, as in the example below, where the underlined text is what the user entered from the keyboard. To read the entered string, use GetInt. It can help check for incorrect input, but not in all cases.
username:~/workspace/pset1 $ ./mario
Height: -2
Height: -1
Height: foo
Retry: bar
Retry: 1
##
To compile the program, enter the line in the terminal:
make mario
or a more transparent but longer version:
clang -o mario mario.c -lcs50
After that, run the program for execution:
./mario
If you want to check that the program is running correctly, run check50:
check50 2015.fall.pset1.mario mario.c
And if you want to play with the course assistants' version of mario, type the following line:
~cs50/pset1/mario

Task 3. Time to get change

Harvard CS50: Week 1 Assignments (Lectures 3 and 4) - 4In our latitudes, we have not seen this, but in the USA, it seems, there is such a toy, shown in the photo: the cylinders are designed for coins of different diameters (and denominations), they are released by a spring mechanism, and the unit itself can be attached to the belt of a child cashier. However, what happens if someone pays the cashier with a large bill? Imagine how much trouble it will be to count coins for change. To minimize the number of issued coins, you can use so-called “greedy” algorithms. They, as defined by the National Institute of Standards and Technology (NIST), always find the optimal solution at each step in solving a problem, assuming that the final solution (obtained from the totality of such steps) will also be optimal. What does it mean? Let's imagine that a cashier owes a customer 41 cents in change, and he has cylinders of coins on his belt for change in denominations of 25, 10, 5 and 1 cent. A cashier guided by a “greedy” algorithm will immediately want to give out the maximum at the very first step. At this point, the optimal or best solution would be to give out 25 pence. 41-25 = 16. 16 pence left to pay out. Obviously, 25 pence is too much, so that leaves 10. 16-10 = 6. Now we give out 5 pence using the same principle, and then 1. Thus, the buyer will receive only four coins in denominations of 25, 10, 5 and 1 pence. It turns out that the “greedy” step-by-step instructions for issuing money are optimal not only for this case, but also for US currency denominations (and the European Union too). That is, if the cashier has enough coins of any denomination, the algorithm will work best, that is, it will issue the minimum number of coins from all possible cases. So, what is the minimum number of coins we need to give change? This is our third task. Create a file greedy.cin your directory ~/workspace/pset1. Given: coins in denominations of 25, 10, 5, 1 cent The program should:
  1. Ask the user how much change to give out
  2. Calculate the minimum number of coins with which you can do this
Note:We will use a function GetFloatfrom the CS50 library for input and printffrom the standard I/O library for output. In addition, the program must check the correctness of the input. We asked you to use GetFloat, to allow the user to enter a value in dollars and cents separated by a dot. For example, if we owe $9.75, the user should enter 9.75, but not $9.75 or 975. You must ensure that the user enters a number that makes sense. GetFloatLet's say non-negative; the function itself will not help with this . If the user makes an incorrect input, you need to ask him to repeat it and run the program only with the correct data. Beware of the inaccuracies inherent in floating point numbers. For example, 0.01 cannot be represented directly as float. Try using formatted output, for example with 50 decimal places, using the code below:
float f = 0.01;
printf("%.50f\n", f);
By the way, before counting anything, it would be logical to convert the entire amount into cents (and at the same time convert it from floatto int), which will help to avoid a lot of errors and difficulties. To ensure that our automatic code analyzer can correctly check your problem, make sure that the last line of your program's output does not contain any other information other than the minimum number of coins: an integer with a \n after it (those who learn JavaRush are well aware of what are we talking about here =)). Below is an example of what the result of your program should look like.
username:~/workspace/pset1 $ ./greedy
O hai! How much change is owed?
0.41
4
Given the nature of floating point numbers, you can ignore the zero and enter such a number in the form .41. Of course, users who want to check the program for the possibility of entering incorrect data in full should see something like:
username:~/workspace/pset1 $ ./greedy
O hai! How much change is owed?
-0.41
How much change is owed?
-0.41
How much change is owed?
foo
Retry: 0.41
4
Based on these requirements and the example you saw above, your code should most likely contain some kind of loop. If, while testing the application, you realize that the loop does not stop, you can interrupt the execution of the program with the ctrl-c combination (sometimes multiple times). You already know how to compile and run a program. If you want to check if your program is working correctly, using the utility check50, enter the following line in the terminal:
check50 2015.fall.pset1.greedy greedy.c
And if you want to play with this program made by course assistants, write the following command:
~cs50/pset1/greedy

How to validate your code and get marks

  1. Option 1

    If it is important for you to check the correctness of the code, and not to get a final grade, you can check and correct it using the command.

    check50 2015.fall.pset1.name name.c

    entered in the terminal line of CS50 IDE? where nameis the name of your task file.

  2. Option 2

    If you want to get grades (essentially the same as running check50, but with remembering the result and filling out some forms in English, then follow these steps:

    • Step 1 of 2

      1. When the applications are ready, log into the CS50 IDE.
      2. In the upper left corner of the CS50 IDE, within its file browser, not the terminal window, left-click or right-click on your hello.c file (the one located in the pset1 directory) and click Download. You should find that the browser has loaded hello.c.
      3. Repeat for water.c.
      4. Repeat for mario.c.
      5. Repeat for greedy.c.
      6. In a separate tab or window, log in to CS50 Submit .
      7. Click Submit in the lower left corner of the window.
      8. Under Problem Set 1 in the window that appears, click on Upload New Submission.
      9. In the window that appears, click Add files…. A window called Open Files should appear.
      10. Navigate to the location where hello.c is downloaded. It is usually located in the Downloads folder or in the folder that is assigned to you by default for downloads. Once you find hello.c, click on it once to mark it, then click Open.
      11. Click Add files... again and the Open Files window will appear again.
      12. Now find the file water.c in the same way. Click on it, then click Open (or “Open”).
      13. Now find mario.c. And also click and open in the same way.
      14. Everything is the same with the greedy.c file.
      15. Click Start upload to begin uploading your files to the CS50 servers.
      16. On the screen that appears, you will see a window labeled No File Selected. If you move your mouse cursor to the left side of the screen, you will see a list of files that you have downloaded. Click on each to confirm the contents of each. (No need to click on other buttons or icons). If you are sure that you are ready to send the file for verification, consider that you are done! If you want to check your code again yourself or fix anything, go back to CS50 Submit and repeat these steps. You can resend as many times as you want; Only the most recent submission will be evaluated.
  3. Step 2 of 2 (it is not required for evaluation, if that =))

    Now go to the link https://www.edx.org/course/cs50s-introduction-computer-science-harvardx-cs50x where you will find special forms. In them you need to answer several theoretical questions, and then click Submit under them.

Questions with asterisks are required:
  • Alright, should've seen this one coming! In just a few sentences, what's a library? * (Briefly describe what a library is)
  • In just a few sentences, what role does #include <cs50.h> play when you write it atop some program? *(what is the role of the #include <cs50.h> line that appears at the top of some programs?)
  • About how many hours would you say you spent on Problem Set 0: Scratch?
  • About how many hours would you say you spent on Problem Set 1: C?
  • What's your opinion of CS50x thus far? *(Your opinion about the CS50 at the moment, choose the option like or dislike)
  • Have you asked for help from classmates or staff via CS50s Facebook Group at http://www.facebook.com/groups/cs50? *(have you asked other students or assistants for help in the facebook group)
  • Have you asked for help from classmates or staff via CS50s Subreddit at http://www.reddit.com/r/cs50 *(have you asked for help from other students or assistants via Subreddit)
  • Have you asked for help from classmates or staff via Twitter using @cs50 or #cs50? *(have you asked other students or TAs for help on Twitter using @cs50 or #cs50).
Friends, if you have any questions, write them in the comments under this guide. If you have not reached JavaRush level 5 to receive an invitation to info, we recommend that you do so. It's free, interesting and not very difficult.

Code resource:

  1. Lecture three

    http://cdn.cs50.net/2015/fall/lectures/1/w/src1w.zip

  2. Lecture four

    http://cdn.cs50.net/2015/fall/lectures/1/f/src1f.zip

    http://cdn.cs50.net/2015/fall/lectures/1/f/src1f/

additional literature

http://cpp.com.ru/kr_cbook - Russian version of the classic book on C from the authors of the language - Brian Kernighan and Dennis Ritchie. Widely known in narrow circles as K&R. The translation, however, is not of the newest edition. Read the first three chapters. There will be a little more material than you need, but enough to solve problems. https://computer.howstuffworks.com/c.htm is a resource recommended by the authors of CS50. In English. Page 1-7, 9 and 10.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet