# Tutorial 2 Numbers, Variables, Simple I/O --- ![](https://i.imgur.com/wHOBHt8.png) --- **Questions from last week** - What does dcc do? - How do the autotests work? - What is the point of pair programming? - Do lab partners both need to submit the exercises? - What do error messages mean? - Where can I look up more information about a command (or just more resources about C)? - When do we use int or double? - What does #include<stdio.h> do? --- ### Recap - printf - scanf - integers - operators --- ### Conditionals **What is the syntax of C if statements? What is the role of if statements in programs?** --- ### Conditionals **What is the syntax of C if statements? What is the role of if statements in programs?** If statements allow branching in programs, i.e., selecting and executing different blocks of code according to certain conditions. --- Write a program **pass_fail.c** that reads in an integer and prints out "PASS" if the integer is between 50 and 100 inclusive and fail if it is between 49 and 0, inclusive. It should print out ERROR if the number is less than 0, more than 100, or if the user does not enter a number. For example: ``` $ ./pass_fail Please enter your mark: 42 FAIL $ ./pass_fail Please enter your mark: 50 PASS $ ./pass_fail Please enter your mark: 256 ERROR ``` --- ``` $ ./pass_fail Please enter your mark: 42 FAIL $ ./pass_fail Please enter your mark: 50 PASS $ ./pass_fail Please enter your mark: 256 ERROR ``` **HINT**: You might need printf, scanf, and some if statements --- Write a program rectangle_area.c that reads in 2 integers which are the side-length of a rectangle, and then prints the area of the rectangle. For example: ``` $ ./rectangle_area Please enter rectangle length: 3 Please enter rectangle width: 5 Area = 15 $ ./rectangle_area Please enter rectangle length: 42 Please enter rectangle width: 42 Area = 1764 ``` --- ``` $ ./rectangle_area Please enter rectangle length: 3 Please enter rectangle width: 5 Area = 15 $ ./rectangle_area Please enter rectangle length: 42 Please enter rectangle width: 42 Area = 1764 ``` **HINT**: You might need printf, scanf, if statements, and some maths --- ### Representation of data **What is a bit?** A bit is a binary value (0 or 1). The word bit is derived from binary digit. **What is a byte?** In modern computing a byte is simply 8 bits. --- ### Representation of data **How many distinct values can be represented with a single byte?** --- ### Representation of data **How many distinct values can be represented with a single byte?** 2^8 = 256 possible bit patterns --- ### Representation of data **Give a representation scheme that could be used to encode a subset of integers in 4 bits.** --- ![](https://i.imgur.com/iocGNVl.png) --- ### Two's complement You can convert a binary number to two's complement by: 1. Take the 1's complement, then add 1 OR 2. Read from left to write, keep the first 1, then invert the rest --- ### Files and Directories **What is a file?** --- ### Files and Directories **What is a file?** A file is basically an array (sequence) of bytes stored in a semi-permanent way. --- ### Files and Directories **Give a few examples of information that is commonly stored inside the bytes of a file, and specify an encoding that might be used for the byte values.** --- |File name | Contents | Byte encoding | |----------|----------|---------------| | README | text (English) | **ASCII** | | a.out | machine code | ELF | | banner.jpg | Image | JPEG | | chessboard.bmp | Image | **BMP** | | main.c | C program | **ASCII** | | song.mp3 | Sound (music) | MP3 | **Theses ones in bold will be covered in COMP1511** --- ### Files and Directories **What is a directory?** --- ### Files and Directories **What is a directory?** A directory is basically a set of files or directories. --- ### Do I need to know all this for the exam? --- ### Potentially yes! (Unless the lecturers say otherwise)
{"metaMigratedAt":"2023-06-14T17:22:54.748Z","metaMigratedFrom":"Content","title":"Tutorial 2","breaks":true,"contributors":"[{\"id\":\"3a8692fe-89b9-4b4e-824d-00af489f87f1\",\"add\":6213,\"del\":2222}]"}
    153 views