Introduction to Computer Science and Programming in C, Homework 1. Instructor: Bert Huang Due 9/23/08 before class begins. ******************************************************************************** 1. Written Section (Short answers) ******************************************************************************** a) Write the following base-10 numbers in base-2: 1, 10, 1024, and 1025. b) Explain whether the following are analog or digital and why: - Handwritten arithmetic (for example: 1+1, 0.5+0.8, 1/3 + 1/5, etc.) - counting by tally marks - A sundial. c) Define "algorithm" and describe why it is useful to have algorithms. d) Define "abstraction" and describe a non-programming example of abstraction. ******************************************************************************** 2. Hello World (and a small tutorial) ******************************************************************************** - Using ssh or PuTTY, connect to "cunix.cc.columbia.edu" - Log in using your UNI. - You should see: Sun Microsystems Inc. SunOS 5.9 Generic May 2002 $ This is simply a welcome message telling you that you've logged in. - The "$" is a "command prompt" or "command line". You can type commands into the prompt and the system will execute your commands. Type "pwd" and hit the "enter" (or "return") key. (This stands for Print Working Directory). This will display the current directory you are working in. Since you just logged in, you should be in your home directory by default. You should use this command whenever you are unsure which directory you are currently working in. - Type "ls" to list the contents of the directory you are in. - If you have not already done so on your own, let's create a directory to keep your files for this course. The command for creating a directory is "mkdir". For example, you could type: $ mkdir 1003 Feel free to name the directory something other than 1003. Now if you type "ls", you will see the new directory you just created. - To move into the directory you created, use the command "cd". For example, to move into the "1003" directory we made in the previous step, we type: $ cd 1003 We can type "pwd" to verify that we are in "1003" now. - Now let's create a C code file. Type one of the following: emacs hello.c pico hello.c vi hello.c Each of these commands will start a text editor (either emacs, pico, or vi) to edit a file named "hello.c". Using the text editor, write the following: #include int main() { printf("Hello, World!\n"); return 0; } Now add comments to indicate what each line does. - Save and exit the text editor. Type "ls" to verify that the file was created. To read the text file without opening an editor, use the command "more", which will display a text file directly in the shell. For example: "more hello.c". - Now we will compile the program you just wrote. We will run the compiler program "gcc". Type gcc hello.c Now type "ls". If there were no errors, there should be a new file in your directory named "a.out". This is the default output for gcc. To execute your program, type ./a.out To change the output of gcc, use the flag "-o". For example: gcc hello.c -o hello This will create a program called "hello" (note there is no ".c" suffix) instead of the less intuitive "a.out". We can execute this with ./hello Submit your "hello.c" file (which should be commented). ******************************************************************************** 3. Types (based on Exercise 4-5 in Practical C Programming) ******************************************************************************** - Write a program that deliberately makes the following mistakes: * Prints a floating point number using the %d conversion. * Prints an integer using the %f conversion. * Prints a character using the %d conversion. - Also have the program print the correct conversions for comparision. - The program output should clearly label each example. - In your writeup, write a short explanation for the output of the deliberate mistakes. ******************************************************************************** 4. Command Line Input ******************************************************************************** - Write a program "round.c" that will take a decimal number and print that number rounded to the nearest integer. For example, $ ./round 5.5555 5.5555 rounded is 6 $ ./round 5.3 5.3 rounded is 5 $ ./round 9 9 rounded is 9 (hint: one way to do the rounding is to take advantage of types.) (NOTE: Do this exercise without using the include file "math.h".) ******************************************************************************** Submission procedure: ******************************************************************************** Move the files you plan to submit (writeups and code) into a separate directory named "homework1". Compress the directory using the command: tar -czvf bch2104_homework1.tgz homework1 Replace Bert's uni (bch2104) with yours. This identifies the submitted file as yours. This creates a single file (bch2104_homework1.tgz) that contains a compressed copy of your homework1 directory (and its contents). (For more information on the "tar" command, type "man tar"). Copy your work from your cunix account using an SFTP (secure file transfer protocol) program. Upload it to Courseworks. From the Courseworks homepage, go to the Class Files. There should be a Homework #1 folder in the Shared Files section. Post your file in the Homework #1 folder. This folder is only viewable by the TA's and the instructor, so don't worry about other students seeing your submitted file.