Homework 2

Due: October 24, 2019
Points: 100


UNIX/Linux Questions

Please put the answers to these two questions in a text file called linux.txt or a PDF file called linux.pdf.

  1. (8 points) Please list all the directories on the CSIF containing the program gcc.
  2. (12 points) Suppose I have a file called x in my current working directory. File y is a (hard) link to this file, and file z is a symbolic link to it also. I now give the command “mv x a”. What would the outputs of the following commands be, and why?
    1. cat x
    2. cat y
    3. cat z

C Programming

  1. (30 points) Write a recursive function that prints all permutations of the characters in a string. Your program is to prompt the user with “> ” (note the blank space after the “>”). The user then enters a string of letters or digits or both, and the program lists the permutations. It then reprompts the user for a new string. It continues doing this until the user enters either a blank line or types an end-of-file (control-D as the first character).
    Here is an example. The user types what is in red type; what the computer types is in black type. The “%” is a shell prompt. The “↵” means the user typed return or enter (or otherwise entered a newline). Your listing of the permutations need not be in the same order, but it must include all permutations of the input.
    > 123↵
    123
    231
    312
    132
    213
    321
    > ab↵
    ab
    ba
    > 
    
    To turn in: Put this program into a file called permute.c; turn it in as directed below.
  2. (30 points) Throughout the rest of the term, we will build a program to play the game of life, devised by John Conway. It uses a board that is a two-dimensional rectangle divided into small, equal-sized squares. Each square may be populated or unpopulated. The neighbors of a cell are the cells that adjoin it vertically, horizontally, or diagonally.
    At each step, the following transformations are applied to all the cells.
    1. If a populated cell adjoins less than 2 other populated cells, or more than 3 populated cells, it becomes unpopulated.
    2. If a populated cell adjoins 2 or 3 populated cells, it remains populated.
    3. If an unpopulated cell adjoins exactly 3 populated cells, it becomes populated.
    As a first step, you will read the dimensions of the board from the standard input. This will be given as two integers separated by an “x” (the 24th letter of the alphabet) and possibly white space before and after the numbers and the “x”. Your output is to be a square composed of “-”es and “|”es. If the numbers read are “2 x 8”, your square should enclose a 2×8 rectangle. The inside of the rectangle is to be blank.
    To turn in: Put this program into a file called life.c; turn it in as directed below. If you want to submit two files, that is, have this in a separate file, call the main one life.c and the other file(s) readrectn.c, where n is a single digit (for example, readrect0.c). Turn it in as described below.

C Program Debugging

  1. (20 points) The following asks you to analyze a (deliberately) poorly written program. Please comment the following program, expanding each argument of the printf in your comment so that anyone can understand what each argument is in simplest form. Your comments need to explain why the program does what it does. Just make one header comment, and do not clean up the program!
    
    main() { printf(&1["\021%six\012\0"],(1)["have"]+"fun"-0x60);}
    
    Note: If you compile this program on the CSIF, you will get numerous warnings. You don’t need to fix them (in fact, you should not) or explain them.
    To turn in: Put this program into a file called weird.c; turn it in as described below.

Extra Credit

  1. (10 points) The ratio of a circle’s circumference to its diameter, is approximately
    π = 3.1415926535897932384626433832795028841971693993751058209
    Write a program that prints this value to 11 decimal places, then adds π to itself 10 times, and prints that value to 11 places. What do you notice when you compare the two values? Why does this occur?
    To turn in: Put this program into a file called pi.c; turn it in as described below.

Turning In Your Homework

For this assignment, you are to create a tar(1) archive and a Makefile.

The Makefile is to have 3 or 4 targets: permute, life, weird, and (if you do the extra credit) pi. So, when I type make permute, the Makefile will execute the command to compile your permute.c program, and save the executable as permute.
Put the answers to the Linux questions (in a text or PDF file), the Makefile and the programs you wrote into a directory called hw2-ans. Next, create a tar archive:

tar cvf hw2-ans.tar hw2-ans
or whatever files you wish to submit. Then submit the tar archive to Canvas.


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming and Problem Solving
Version of October 21, 2019 at 3:48PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh