Link back to syllabus

Setup

Download the source code for this file (it’s in the labs directory on our GitHub repo) so that you can answer the questions in it. As you continue through the lab, edit the file with whatever editor is handy – for example, Atom, available in the Programming submenu of the Applications menu on the lab machines. (We’ll use emacs later. But we need to start somewhere!)

GitHub

Start the first homework assignment by following the first part of the submission instructions. This will allow us to debug any GitHub hiccups.

Working with the command line

Before we get to C programming, let’s do a warmup around working with the command line.

Download this cli.tar file. (CLI stands for command-line interface.) Expand its contents by saying tar xvf cli.tar.

Answer these questions:

  1. What is your home directory? (Hint: pwd)

  2. Where did you download the cli.tar file to?

  3. How many .txt files were expanded from cli.tar?

  4. How many directories were expanded from cli.tar?

Now do this:

  1. Move (mv) all the .txt files out from their directories, putting them all in the cli directory.

  2. Run cat *.txt. This will print out the contents of all the files. What message do you see?

emacs

Complete this tutorial on emacs, the recommended editor for this course. If you already know emacs, please help your partners!

C

Today’s lab will ask you to shake the rust off your programming skills by writing several small programs in C. Although the assigned reading did not yet cover loops, rest assured that the loops you know from Java work just as well in C. If you need a refresher, here is a short tutorial on loops.

Hint: Remember that we use == to test for equality – never =!

  1. Write a “Hello, world!” program in a new file hello.c that simply prints “Hello, world!” using printf. Compile your program with gcc -o hello hello.c and run it with ./hello.

  2. Write a program (in age.c) that asks the user for their age and tells them what age they will turn on their next birthday. Compile with gcc -o age age.c and run with ./age.

  3. Write a program (in max2.c) that asks the user for two numbers and tells them which one is bigger.

  4. Write a program (in max.c) that uses a loop to ask the user for many numbers, stopping only when the user enters 0. At that point, your program reports the maximum number that the user entered.

  5. Write a program (in count.c) that asks the user for two numbers. It then prints out all the numbers starting from the first and going to the second. It should not crash if the second number is less than or equal to the first. (You can design the behavior in this case.)

  6. Write a program (in count_not_5.c) that is like the previous program but skips counting any number that is a multiple of 5. C uses % to compute modulus, like Java and Python. The modulus of two numbers is the remainder when the first is divided by the second. So, 15 % 4 is 3. If you want to know whether a is divisible by b, check if a % b == 0.

  7. Write a program (in fizzbuzz.c) that plays FizzBuzz: Your program asks the user for two numbers, and it starts counting at the first and counts up to the second, one line at a time. Instead of any number that is divisible by 3, print Fizz. Instead of any number that is divisible by 5, print Buzz. Instead of any number that is divisible by 3 and 5, print FizzBuzz.

  8. Write a program (in prime.c) that asks the user for a number and then reports whether that number is prime. An easy (but not terribly efficient) way to check for primality of n is to check all numbers m such that 2 <= m < n to see if m evenly divides n. If no such m divides into n, then n is prime.

  9. Write a program (in euler1.c) that solves Project Euler problem 1. You do not have to make an account at this site if you don’t want to… but the site is really fun!

  10. Write a program (in euler2.c) that solves Project Euler problem 2.

  11. Write a program (in leibniz.c) that uses doubles to calculate π by adding together terms of the Gregory-Leibniz series. Like Java, dividing numbers that look like integers will yield integers, so you probably want 1.0 as your numerator, not 1. Have your program add together enough terms of the series to get 5 digits after the decimal point correct.

  12. Similar to the previous program, but use the Nilakantha series (in nila.c). The series is given here under the subsection “Rate of convergence”.

Before end of lab

Upload your work to our classwork repo in the lab01 directory. Make a new directory for yourself and upload your files into that directory. (Git doesn’t allow for empty directories. To create an almost-empty directory, create a file named .dir_exists. The file itself can be empty.)