Week 5

New Resource: Companion Web Site for Molay (includes code) http://wps.prenhall.com/esm_molay_UNIXProg_1/7/2040/522376.cw/index.html ------------------ Read: K&R 8.1-8.4 ex1 : K&R ex 8-1 Background on Systems Programming and how the book works: Read: Molay 1.1 - 1.5 (and the summary at the end of 1.7.3, p.29) How to Unix works and where to find answers to your questions Read: Molay Ch. 2 ex2 : Write your own version of the 'od' command. Read the man page to see what it does. For experimentation try this (obviously, working in a directory where you have access to decl.txt): %> od decl.txt %> od -c decl.txt %> od -h decl.txt %> od -h abcdefghijklmno ^d Your od command must support the -c and -h flags. It does not have to support reading from standard input - just reading a file will suffice. ex3: Treat a file as a matrix of integers. Underneath it all, a file is just a list of bytes. If an integer is 4-bytes then it should take 512-bytes to store 128 ints, for example. Furthermore, you can "mentally" impose any kind of topography on this data. That is, you can treat it as a linear array of 128 ints, or a 16x8 2D-array, or a binary tree of ints with 7 levels! Thus, even though we envision a file as a linear array of bytes, there's no reason we can't use lseek creatively to locate things in the file. So, here's what to do: 0. We'll treat a file as a 10x10 matrix of 4-byte integers (you can make it bigger later on if you want). 1. Write a little program that creates a file big enough to hold this data (that is, you'll need to write 100 ints into the file as raw data, NOT as formated printed text). 2. Test that step 1 worked, by running 'od' (or hexdump) on the file. You may want to write the numbers 1-100 into the file to confirm your data placement. Write the following functions (names are suggested): 3. void printMatrix(FILE *, int numRows, int numCols) // prints the bytes represented in the file to standard output as a 10x10 array of ints. // Use formatting characters to maintain even spacing. Make reasonable assumptions about the number of digits in the ints you're storing. 4. int matrixGet(FILE *, int row, int col) // returns the number at the given row,col where 0<=row<=col<10 Confirm that it works. 5. int matrixSet(FILE *, int row, int col, int value) //returns the number previously at row,col. 6. void diagonalShift(FILE *, int numRows, int numCols) //shifts all the numbers in the matrix down and to the right. New entries (around the top and left borders) should be set to zero. 7. Write a main program that uses your functions to fill a matrix with some numbers, and then repeatedly call diagonalShift, printing out the matrix after each step. It should look like an invasion of zeros!
How to Submit