CMPT 102 - Assignment 5

Out: Friday, Nov. 18, 2005
Due: Friday, Dec. 2, 2005 at 5:00pm
Weight: 10%
Total: 100 marks


Question 1 [50]

Write a program to test whether a grid of numbers is a magic square.


Question 2 [25]

Write a program to replace all occurrences of a string in a text file by another string.


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:
 
tar cvf a5.tar magic.c, q1.out, replace.c, q2.out, query.c, q3.out
gzip a5.tar
Then submit the file a5.tar.gz

Hard-copy submission

You should submit the printout of the files you submit electronically. Also, don't forget to print out a copy of the sample cover page, fill it out, and sign it.