cs3157 - Advanced Programming Summer 2006, Lab #4, 30 Points June 15, 2006
cs3157 - Advanced Programming Summer 2006, Lab #4, 30 Points June 15, 2006
Follow these step-by-step instructions. This lab must be submitted electronically (see instructions at the end of the lab) by Wednesday June 21, 4 pm. You need to include a README file with information about each file submitted and any other comments you want the TAs to see. You are required to create a single Makefile to run each of the steps of the lab. (Example, make step1 should compile and run step1 of the lab.) In addition you need to have a make clean step to clean up the object files, and executables generated by your make steps. Feel free to add comments to your makefiles
Define the function initSquare(MagSquare_PTR) which takes the magic square pointer argument (as above) and fills it with values in the range [1 - 9]. These values must be assigned RANDOMLY, but make sure that no number appears more than once in the array. Also Define the function printSquare(MagSquare_PTR) which takes the magic square pointer argument (as above) and prints out its contents to the screen in a nice 3 rows by 3 columns format. You should test your code with just these 2 functions to make sure each is working (before adding the rest of the functions). Hint: To get random numbers look up lookup rand() and srand() from the stdlib libary. 1. maintest1.c this will contain the main function 2. magicsquare.h this will contain the functions declarations that you will be using. 3. magicsquare.c this will contain the function definitions from above including the following methods: a. void initSquare(MagSquare_PTR) (see above) b. void printSquare(MagSquare_PTR) (see above) c. int sumColumn( int column, MagSquare_PTR ) The function takes the 2-dimensional magic square array and a column number and returns the sum of the 3 values in that column d. int sumRow(int row, MagSquare_PTR) see above. e. int sumDiagonal( int diagonal, MagSquare_PTR ) The function takes the 2-dimensional magic square array and a diagonal number and returns the sum of the 3 values in that diagonal. Use diagonal = 0 to refer to the diagonal that runs from the northwest corner down to the southeast corner. Use diagonal = 1 to refer to the diagonal that runs from the northeast corner down to the southwest corner. f. Create a function called int isMagic(MagSquare_PTR) which takes the magic square pointer as an argument and checks each of the rows and columns and diagonals to see if all the sums equal 15. The function should return 0 if the argument is not a magic square and 1 if it is a magic square. g. Modify the above main() so that it calls isMagic() and prints out whether the square is a magic square or not. h. Modify the main() program so that after it prints out the square, it computes (using the functions above) and prints out the sum of each of the 3 rows and each of the 3 columns and each of the 2 diagonals. Compile and test your program. Here's a sample output:
the square is not a magic square here is your square: 2 8 3 1 4 5 7 9 6 sum of row 1 = 13 sum of row 2 = 10 sum of row 3 = 22 sum of column 1 = 10
of of of of
= = 0 1
21 14 = 12 = 14
} // end of main()
Shell Programming Section. In this lab, youll use several UNIX tools which we discussed in class on Wednesday. The tools are: cut outputs sections of each line in input grep outputs lines matching a pattern sed stream editor sort outputs sorted concatenation of input wc outputs the number of bytes, words and/or lines in input ps outputs process id of all running processes The man pages for each of these can be accessed by typing, for example man cut, at the UNIX prompt. We mentioned (briefly), how send the output of these commands to a file as well as to stdout (which is the default). We also discussed how you can pipe the output of one command into the input of another. Heres an review/overview in brief: The greater-than sign > is used to redirect the output of a command from stdout to a file. This is like opening a file for writing in C. If the file exists, it will be overwritten. If the file does not exist, then it will be created. Heres an example: grep "hello" myfile > hello.dat This example will search for all lines containing the word hello in a file called myfile, and it will output those lines (containing the word hello) to the output file hello.dat. The double greater-than sign >> is used to append the redirected output to the end of an existing file, rather than creating a new file. If the file does not exist, then it will create it. This: grep "hello" myfile >> hello.dat will behave just like the example above, except that the output will be added to the end of the hello.dat output file. The vertical bar | is used in between commands to pipe the output of the first command into the input of the second command. For example: grep "hello" myfile | wc -l will look for all the lines in myfile that contain the word hello and will output those lines, but instead of outputting them to the screen, the lines will be read as input by the wc command, which (since it is invoked with the -l option) will output the number of lines it receives.