V22.0101 Intro to CS I Fall 1998


The exam is Weds. Dec 23, from 2:00 to 3:50pm. Section 2 (Boppana) will be in the MAIN BUILDING on the second floor, room 207. The Marateck sections will be on the 4th floor of the MAIN BUILDING. (Section 3: Rooms 406 and 408; Section 4: Room 407).

Sample Exam Questions

  1. Write a procedure using the while statement which reads integers, one per line, from the input and adds them up, stopping when the number 0 is read. It should then print the sum and the number of integers which were read, not counting the 0.

  2. Write another procedure which does the same thing using the repeat statement.

  3. Write a Pascal program to print out the Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21 (each number being obtained by adding the previous two together). Stop when the numbers exceed 1000.

  4. Write an integer-valued function which takes as a parameter an integer n and returns the value 1 + 2 + ..+ n. For example, if n has the value 5 the function should return the value 15, and if n has the value 2 the function should return the value 3. If n = 0 return the value 0.

  5. Write a function named sum_divisors which takes an integer parameter N, and returns the sum of all integers in the range 1 to N-1 that divide into N exactly (with zero remainder). For example, suppose N has the value 6. The numbers 1, 2 and 3 all divide exactly into 6, but 4 and 5 do not. So, since 1 + 2 + 3 = 6, sum_divisors(6) should be 6. Now suppose N has the value 9. The numbers 1 and 3 divide exactly into 9, but no other number less than 9 does. So, since 1 + 3 = 4, sum_divisors(9) should be 4. You can assume that N >1.

  6. Assume the declaration
        type IntArray = array[1..max] of integer;  
            {assume max has been declared in a const statement}
    
    Write a procedure which takes one parameter: a variable parameter X of type IntArray. Assume that all of the elements (1 through max) of the array have been assigned values. The procedure should reverse the order of the elements of the array X. Thus the first element should be interchanged with the last, the second with the second-to-last, etc. Be sure the program works in both the case that max is even and the case when it is odd. For example, if the array has elements 5, 3, 17, 21, 2 on entry, then on exit it should have elements 2, 21, 17, 3, 5.

  7. Assume the declaration
    type BingoBoard = array [1..N, 1..N] of Boolean;
       {assume N has been declared in a const statement}
    

    Write a procedure named ReadBoard with one parameter B of type BingoBoard, which reads a pattern from an input file (consisting of X's and blanks, just as for the game of life data files) and sets each array element to either true or false depending on whether the corresponding file entry was an X or a blank. Just as in the game of life, do not assume that the file is padded with blanks. You must use while not eoln and while not eof.

  8. Write a Boolean function named RowBingo with one parameter B of type BingoBoard, and which returns the value true if B has a row with all N elements in the row set to the value true. Otherwise, the function returns the value false. Remember that the first index of the array is the row index. 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 false.

  9. Using the same declarations as you used in the final database homework, write a procedure which searches for a given name in the database, and doubles the hourly pay for this person. Note: there will certainly be a question using arrays of records on the exam. Other possible sample questions would be similar procedures to the ones you wrote for the final homework.

  10. Consider the declarations
       type airport = (jfk, lga, ewr, dfw, sfo, lax);
            AirArray = array[airport, airport] of integer;
    
    Write a procedure which takes as input a variable miles of type AirArray, which stores the mileage between airports. For example, miles[lga,jfk] is the distance in miles between La Guardia and JFK. The procedure should return two parameters giving the pair of airports which are furthest apart.







Sam Marateck
Sat Dec 5 21:49:23 EST 1998