V22.0101 Introduction to Computer Science I Fall 1996
Final Exam
Profs. Marateck and Overton

There are 7 questions, all worth the same number of points. Please read carefully and write clearly. If you finish early, it is a good idea to check your work carefully before you leave. Good luck!

  1. Write a function factorial which, given an integer parameter N, returns the product . If N is zero or negative, the function should return the value 1.

  2. Assume the declarations
    const max = 30;
    type ArrayOfStrings = array [1..max] of string[15];
    Write a procedure ReadNames with parameters Data (type ArrayOfStrings), NumNames (type integer) and TooMany (type boolean). It should read names (strings with max length 15 characters) from the keyboard input, one name per line, until the end of file is reached, storing them in the array Data. The names do not have to be added in alphabetical order. The procedure should also return the integer NumNames which is the number of lines which were read from the file

  3. Assuming the same declarations as in the previous question, write a procedure Delete with parameters Target (type string), Data, NumNames and Success (type boolean). It should delete the name Target from the array Data, moving the other names up one position to take its place. If Target appears more than once in the array, the first occurence should be deleted. The procedure should also modify NumNames accordingly, and should also return the boolean flag Success indicating whether or not the name was found and deleted. This procedure should not read from the input or write to the output.

  4. Assuming the same declarations as in the previous question, write a procedure DeleteAll with parameters Target, Data, NumNames and NumDeleted (type integer). It should repeatedly call Delete to delete all occurrences of Target from Data. As well as modifying NumNames, it should also return the parameter NumDeleted, giving the number of occurrences of Target which were found and deleted. This should be zero if the name Target is not in the array Data. This procedure should not read from the input or write to the output.

  5. Assume the declaration
    type BingoBoard = array [1..N, 1..N] of Boolean;
      {assume N has been declared as a constant}

    Write a Boolean function AllTrue with one parameter of type BingoBoard, which returns true if every array element in the two-dimensional array has the value true. Otherwise, it should return the value false.

  6. Assuming the same declarations as in the previous question, write a Boolean function named DiagonalBingo with one parameter B of type BingoBoard, and which returns the value true if B has a diagonal from top left to bottom right, or from bottom left to top right, with all N elements in the diagonal set to the value true. Otherwise, the function returns the value false. For example, if N = 5 and the board has the configuration
           F F T T T
           T F T T T
           F F T F F
           F T T F F
           T F T F F
    the function should return the value true.

  7. Suppose a company uses the following type declarations to keep track of its employees:
        const NumEmps= 30;     {Number of Employees}
        type  DayType = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
              RecType = record
                          name: string[15];
                          hourly_pay: real;
                          hours_worked: array[DayType] of real;
                          total_pay: real
              ArrayType = array[1..NumEmps] of RecType;

    The company has decided it will pay employees double time for work they do on the weekend. Write a procedure which sets the total_pay field in all the records taking this into account. The array of records should be passed to the procedure as a parameter. On entry, each employee record in the array already contains values for the hourly pay and the hours worked, with separate hours worked reported for each day of the week. However, the total_pay fields are undefined. On exit, the total_pay fields should be set to the total pay, using the hourly pay for hours worked Monday to Friday and double the hourly pay for hours worked Saturday and Sunday. Notice the use of the special enumerated type DayType in the definition of the hours_worked field. The procedure should not contain any read or write statements.

End of Exam - Enjoy Your Holiday!

Michael Overton
Wed Dec 3 21:42:38 EST 1997