COS 109: Problem Set 6

Wed Nov 11 09:51:29 EST 2009

Due 5:00 PM, Wednesday Nov 18. Answers need not be long, merely clear so we can understand what you have done. Please submit typed material, not hand-written, if at all possible, and keep a copy for yourself just in case something goes astray. Thanks.


1. A Small Matter of Programming

(a) This Javascript program is supposed to print a table of Celsius temperatures from 0 to 100 inclusive in steps of 10, and their corresponding Fahrenheit temperatures. It is also supposed to print a line above the table that labels the two columns. Not surprisingly, the program contains errors, although the first document.write statement and the formula it contains are correct. Find and fix the errors. (This is a question about logic, not about syntax, but if you put the code into a file and run it, you can be sure that you have identified the errors.)

	var cels = 0
	while (cels < 100) {
	    cels = cels + 1
	    document.write("<br> " + cels + "  " + (9 / 5 * cels + 32))
	}
	document.write("<br> Fahrenheit  Celsius")

(b) The original Fortran language of 1958 used "goto" statements, very much like the goto instruction in our toy assembler language, to describe the flow of control in a program; statements can have numeric labels if they are to be the target of a goto statement (and the specific numeric value has no significance). For example, "goto 10" causes control to transfer to the statement labeled "10" in the fourth line of the following excerpt.

	if (x > y) goto 10
	m = x
	goto 20
  10	m = y
  20	print m
Code that uses goto's is usually convoluted and hard to understand, so goto is rarely used other than in assembly languages; some languages, including Javascript, don't provide a goto statement at all. Rewrite this Fortran-like sequence with a single if-else statement in Javascript, and state what computation it performs. We're much more interested in seeing that you have the right idea than that you are fluent in Javascript syntax, though you should be as precise as you can, and you can certainly test your code from a file.

2. File Systems

Suppose that part of the file system on a computer looks like this, where names beginning with D are directories (folders), and names beginning with F are files:
     D1
         D2
             D3
                 F4   2007 bytes
             F3        100 bytes
             F4       2007 bytes
         D3
             F5       5121 bytes
         D4
             D6
             F4       2007 bytes
             F6         11 bytes
         F1           3072 bytes
         F2           2008 bytes
That is, directory D1 contains directories D2, D3 and D4 and files F1 and F2; directory D1\D2 contains directory D3 and files F3 and F4; directory D1\D2\D3 contains file F4; and so on.

Assume that no other files are involved, directories occupy one disk block each, and all disk blocks are 1024 bytes long. Each question assumes that reading a file or directory into RAM begins by reading the block that contains directory D1 and works down the hierarchy until the desired information is found, reading only blocks that must be read. Be careful to distinguish between files and directories -- a directory contains the names of files and other information about them, but not their contents.

(a) How many disk blocks in total are used by these directories and files?

(b) How many blocks would the file system have to read from disk (including reading D1) to read all the blocks of file D1\F1 into RAM?

(c) How many blocks must be read from disk to read all the blocks of file D1\D2\D3\F4 into RAM?

(d) How many blocks must be read from disk to decide whether D1\F1 and D1\F2 have exactly the same contents?

(e) How many blocks must be read from disk to determine whether or not a file named D1\D2\D4\D6\F4 exists?

(f) Most files waste some space at the end because their size isn't an integral number of blocks. Which file wastes the most, and how much?

(g) Which file wastes the least, and how much?

3. How do you get to Carnegie Hall? Practice, practice, practice.

A lot of questions are basically the same thing: how many bits does it take to hold numeric values up to a maximum value (number of freshmen, number of undergrads, number of cars, number of people, number of instructions, ...). These are all the same question! Figure out how many things there are, then figure out what's the smallest power of two that is at least that big. That's the minimum number of bits needed to assign a distinct bit pattern to each item. This power is also the log base 2 of the original number (rounded up to an integral value). The number of bytes is just the number of bits divided by 8, but rounded up (no fractional bytes).

For these practice questions, it is highly recommended that you work out the answer by using familiar powers of two, not by reaching for your calculator.

(a) How many bits and how many bytes for

  1. 6.8 billion people
  2. 310 million Americans
  3. 34 million Canadians
  4. 500,000 people at the Simon and Garfunkel concert in Central Park
  5. $83,000 median household income in Princeton (2007)
  6. 37,000 New York City marathoners
  7. 5,200 Princeton undergrads
  8. 535 members of Congress
  9. 200 countries in the United Nations
  10. 110 students in COS 109

(b) Going the other way, the number of bits determines how many different things could be encoded with that number of bits, though "bits" is often disguised, for instance as dots and dashes in Morse code, or fingers of a hand, or miles on a binary odometer. The number of different things you can encode with N bits is 2N, but the encoding will start at 0 (all zeros) and go up to 2N-1 (all 1's).

For each integer N from 1 to 10, state how many things could be encoded with N bits, and give a plausible real-world example of some set of things that would require at least N bits but not N+1 bits. For example, for N=1, male/female; for N=2, frosh/soph/junior/senior. Your sets do not have to contain exactly 2N items. (The list in part (a) should give you ideas.)

(c) A COS 126 midterm a few years ago asked this question: "A certain recent Princeton CS class printed their [4-digit] class year on T-shirts in binary. The binary number is all 1's except for two adjacent 0's. Which class was this (in decimal)?" I have long believed that COS 109 people can do this stuff just as well as 126 can. Prove me right.

(i) What is the class year number in binary?
(ii) In decimal?
(iii) In hex?

(d) Unix systems maintain time as the number of seconds since 00:00 on January 1, 1970; among other things, this is used to keep track of when files were last changed. The time is stored in a 4-byte integer, but one bit of that integer is used to indicate the sign of the number (negative numbers indicate times before 1970). Given this representation, in what year will time overflow (i.e., wrap around)?