Question 1 [50]
Write a program to test whether a grid of numbers is a magic square.
- A magic square of order is an grid where the numbers from 1 to n2 are arranged in a way such that the sum of the numbers in every row, column, and diagonal is equal to the magic constant . For example, the following are two magic squares:
8 1 6 16 3 2 13 3 5 7 5 10 11 8 4 9 2 9 6 7 12 4 15 14 1 - Your program should take two command-line arguments n and file, where n specifies the order of the given grid, and file specifies the name of the data file which stores the given grid. A grid of numbers is stored in a data file in the obvious way with each row on a separate line. You can assume that the grid in the data file is of the specified order, and there are no errors in the data file.
- Your program should first print the grid of numbers and then it should print out a message indicating whether the grid is a magic square. Keep in mind that to check whether a grid is a magic square, you need to check whether every number from 1 to n2 appears once in the grid.
- Your program should use dynamic memory allocation to allocate memory to store the grid of numbers.
- You should not use any subscript notation in your program. Instead, you should use pointers. The main idea is this: to refer to the array element a[i][j], use the expression *(p+i*ncols+j), where p contains the address of the first element of the array, and ncols is the number of columns in the array.
- Save your program in a file called magic.c.
- Download the four test files: grid1, grid2, grid3, grid4. Test your program with these files, and save the results in a file called q1.out.
Question 2 [25]
Write a program to replace all occurrences of a string in a text file by another string.
- Your program should take three command line arguments: file, from, and to, where file specifies the name of the text file, from is the string to be replaced, and to is the string to be replaced with.
- Your program should print the text file resulting from the replacement to the screen.
- You can assume that the text file consists of a number of lines, and each line consists of no more than 200 characters. All lines except possibly the last one end with a newline symbol. Thus you can do the replacement line by line.
- You may want to use the strstr function.
- Other than when declaring an array to store a line of characters, you should not use any subscript notation in your program. Instead, you should use pointers.
- Save your program in a file called replace.c.
- Test your program with three short or medium size C programs. For each test, you should first display the original file by using the cat command. Save the testing results in a file called q2.out.
Question 3 [25]
Suppose you are given a data file concerning the movies an actor/actress acted in, and another data file concerning the directors of movies. Write a program to answer the user's query regarding the list of directors an actor/actress has worked with, by which we mean the actor/actress has acted in a movie directed by the director.
- The two data files are called actedin.dat and directed.dat.
Each line of the file actedin.dat consists of the name of an actor/actress and the title of a movie he/she acted in. Different parts of the name or the title are connected by the underscore symbol. For example, the following is a line in the file:
Russell_Crowe A_Beautiful_Mind
Each line of the file directed.dat consists of the title of a movie and the name of its director. For example, the following is a line in the file:A_Beautiful_Mind Ron_Howard
- Your program should repeatedly ask the user to enter the name of an actor/actress and then print out the list of directors he/she has worked with. In case the user inputs a name which does not appear in the file actedin.dat, your program should print "No record!". The user can also input the string "quit", in which case your program should exit.
- Your program should define the following three structures:
typedef struct { char actor[N]; char movie[N]; } actedin; typedef struct { char movie[N]; char director[N]; } directed; typedef struct { char actor[N]; char director[N]; } workedwith; - Your program should read the information in the file actedin.dat and store the data in an array of structure actedin, and similarly for the file directed.dat.
- Your program should generate an array of structure workedwith as follows: for each element of structure actedin and each element of structure directed, if they share the same movie title, then an element of structure workedwith should be generated. Now you can use this generated array to answer all the queries.
- Save your program in a file called query.c.
- Download the two data files: actedin.dat, directed.dat. Run your program with four queries and save the results in a file called q3.out. One of the queries should be a name not appearing in the file.
How to submit
Electronic submission
You should submit the files magic.c, q1.out, replace.c, q2.out, query.c, q3.out. You can do this by typing these commands:Then submit the file a5.tar.gztar cvf a5.tar magic.c, q1.out, replace.c, q2.out, query.c, q3.out gzip a5.tar