Introduction to Computer Science and Programming in C, Homework 4. Instructor: Bert Huang Due 12/4/08 before class begins at 1:10 PM. ******************************************************************************** Submission procedure: From this assignment on, you must follow this procedure or you will receive a grade of 0 for the whole assignment. ******************************************************************************** Keep your written section separate from your programming files. Include a README file that lists the files you are submitting and what each one is. Move the writeup for the written questions and your code for the programming problems you plan to submit (writeups and code) into a separate directory named "homework4". Then, in the directory that contains your homework4 directory, compress homework4 using the command: tar -czvf bch2104_homework4.tgz homework4 Replace Bert's uni (bch2104) with yours. This identifies the submitted file as yours. This creates a single file (bch2104_homework4.tgz) that contains a compressed copy of your homework4 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 #2 folder in the Shared Files section. Post your file in the Homework #2 folder. This folder is only viewable by the TA's and the instructor, so don't worry about other students seeing your submitted file. ******************************************************************************** 1. Written Section (Short answers). Write these electronically in a text file that you will include in the homework submission. ******************************************************************************** a) Look at the function ia_locate() in ia.c on page 311 of Practical C Programming (or download it at http://examples.oreilly.com/pcp3/ in the ia subdirectory of the examples archive). Write what this function does in pseudocode. Make the pseudocode as understandable as possible to someone who understands computer programming but not necessarily C, while keeping precise and concise. b) Describe in your own words why modular programming and object-oriented programming are useful. c) In what ways are linked lists more useful than arrays? In what ways are arrays more useful than linked lists? ******************************************************************************** 2. Linked List ******************************************************************************** Write a program that reads in a text file and stores each line as a string in a linked list (feel free to doubly-link if you prefer) data structure. Then ask the user for keyboard input and reply if the entered text is in your linked list. Provide output indicating how many lines were read. For example, if we input a file "example.txt" containing the four lines: This is a test we should see ./linkedlist example.txt Read 4 lines Enter text to search ('quit' to quit): This Found 'This' at line 0. Enter text to search ('quit' to quit): testing 'testing' not found. Enter text to search ('quit' to quit): is Found 'is' at line 1. Enter text to search ('quit' to quit): quit Deleted 4 lines. Goodbye. As always, feel free to embellish the interface or add extra features. ******************************************************************************** 3. 15 Puzzle ******************************************************************************** Write a program that lets the user play variants of the 15 puzzle game, where the goal is to take scrambled numbers and put them in order. Your program should take command line input for the size of the board, and create an NxN puzzle for the user to solve. The numbers should go from 1 to N^2-1 (leave one empty space for maneuvering). At each step, prompt the user for what move to make: up, down, left right (or u, d, l, r). Finally, check after each move to see if the user solved the puzzle. An example run should look like the following: ./puzzle 3 4 2 5 1 3 7 8 6 Enter move (u,d,l,r): u 4 1 2 5 3 7 8 6 Enter move (u,d,l,r): r 4 1 2 5 3 7 8 6 Enter move (u,d,l,r): d 1 2 4 5 3 7 8 6 Enter move (u,d,l,r): l 1 2 4 5 3 7 8 6 Enter move (u,d,l,r): l 1 2 4 5 3 7 8 6 Enter move (u,d,l,r): u 1 2 3 4 5 7 8 6 Enter move (u,d,l,r): u 1 2 3 4 5 6 7 8 Nice Job! Implement this program however you wish. Stay organized to make it easier. Here are some ideas that you are free to ignore: - Use malloc() to generate your 2d array with variable size - Write functions for the moves and maybe one for printing the board. - Make sure you check for invalid moves (like moving up when the space is on the bottom edge of the baord). - Provide enough output so that it is clear how to play. - Use tab ("\t") to space out the numbers evenly. Feel free to add features if you think of something that will make the program better!