CS 112b
Spring 2001
Ileana Streinu

Midterm Exam

Thursday, March 14, 2001 8:00 - 10:20am

Write your name and class account on this exam sheet NOW.

General description

There are 2 parts. Part I is theory, and you have to write the answers on the exam sheet. This part takes about 1 hour and 20 minutes, but this may vary. Part II is done on the computer, and submitted in the directory Mid by 10:20. You can start on Part II whenever you are done with Part I. When done, turn in the filled in exam sheet to Prof. Mihaela Malita in McConnell 215, by 10:20 SHARP (no extensions allowed, not even 5 minutes).

Be generous with comments on your Part II program. Part of the grade will be based on how readable and well organized your code is.

Part I
Basic C++ object oriented programming concepts and basic data structures (60 points)

  1. (3 points) What are the values printed by the cout statements in the following program? Write those values on the right of the page next to the program code. Ignore (possible) syntax errors that this (incomplete) program may contain and concentrate on its logic.

    main(){
    int a=0, b=1, c=2;
    int choice;

    ....
    // the choice variable gets a value somewhere here
    ...


    switch(choice)
    case 0: func0(a,b,c); cout << a << b << c; break;
    case 1: func1(a,b,c); cout << a << b << c; break;
    case 2: func2(a,b,c); cout << a << b << c; break;
    }

    func0(int a, int b, int c)
    {
    a=b;
    b=c;
    c=a;
    }

    func1(int &a, int b, int c)
    {
    a=b;
    b=c;
    c=a;
    }

    func2(int &a, int &b, int c)
    {
    a=b;
    b=c;
    c=a;
    }








  2. (3 points) Write (in the space below) the definition and full code for a C++ function (NOT a main program) to compute the position in a sorted array of n elements where a certain value x is stored, or to insert it (in the appropriate place, i.e. maintaining the property that the resulting array is still sorted), if not already in. The function returns the index of that position, if found, or -1 otherwise (if it was not found but has now been inserted), and it has three parameters: the array, its number of elements and the value x. For each parameter, specify whether it is IN, OUT or IN/OUT.







































  3. (2 points) Templates. Extend the previous C++ function to a template, capable of finding the position of an element that is of any type that can be compared with the usual C++ comparison operator < (such as int, char, float).































  4. (3 points) Functions as parameters. Extend the previous C++ templated function to one capable of working with other data types, which cannot be compared directly with <. Hint: use a function parameter called Compare and rewrite the code accordingly.































  5. (2 points) For the previous situation, give an example of a specific Compare function, which will compare the ages in two records of type CustRec, defined by the following struct:

    struct CustRec{
    int id;
    int balance;
    int age;
    }
















  6. Classes. (2 points) Define a class named Book. Each Book has an id (integer), a color (recorded as an integer code) and a size (denoting its number of pages) (integer). In the space below, start writing the class definition, as it would appear in the header file - only the class variables, no functions yet.



















    Now finish the definition of the class, as well as make use of this class, by answering the following questions. Use the space below each question to write the answer.
    1. (2 points) For an implementation meant to lead to code reuse and which obeys standard software engineering discipline, the Book class description should be in a file named .........................

    2. (2 points) For an implementation meant to lead to code reuse and which obeys standard software engineering discipline, the code for the functions in the Book class should be in a file named .........................

    3. (3 points) Describe the structure of a Makefile for compiling and testing your class Book. The Makefile should obey the rules of structured object-oriented programming, which each class compiled separately. Do not worry about spaces and tabs, but give the right dependencies and separate compilations.









    4. (3 points) Describe the structure of a Makefile for compiling and testing the class Book, assuming that you are only writing the testdriver and get the Book class from a library of pre-compiled code. More precisely, you should assume that I have written and compiled the Book class separately, and have made available to you the necessary files for using it, but not the class code (.cpp file).







    5. (2 points) Write the code for an empty Book constructor. By "empty", I mean that it should be used in constructing an object of this type without giving it any specific values provided by the user - but rather initializing the variables with some defaults values (e.g. all 0's).




    6. (2 points) Write the code for a Book constructor that takes in three integers (corresponding to id, color and size values) and initializes the current object with those values.









    7. (2 points) Write the code for a Print method, which prints on the standard output the values of the three variables of the current Book object.









    8. (2 points) Write a statement that declares a variable of type Book.




    9. (2 points) Write a statement that declares a variable of type Book with initial values (35, 2, 97).




    10. (2 points) Write a statement that applies the method Print on the object of type Book, which has been created at the previous step.




    11. (2 points) Write a C++ function to assign a color to the current Book object. Separately, write a statement showing how you would use this function on the variable defined at step 8 above.














  7. Class templates
    The following class definition restricts the type of information associated to each object of type Point to be an integer.

    class Point
    {

    public:

    Point(); // empty constructor
    Point(int a, int b, int c); // general constructor
    Read(); // read from standard input
    Print(); // print to standard output
    Move(int a, int b); // move a point to a new location
    ChangeInfo(int info); // change the information stored with the point

    private:

    int x,y; // x and y coordinates
    int information; // other information
    }
  • (3 points) Sorting Run the Insertion Sort algorithm on the following array of integers:

    { 15, 3, 2, 1, 13, 9}

    I mean: I do not want to see only the final sorted array!! You should work in stages, following the Insertion Sort algorithm. At each stage, write down how the array currently looks. You do not need to explain what the algorithm does. You do not need to write the code for the algorithm. Just show the array, after each stage.























  • (3 points) Arithmetic expressions in postfix form. Write the prefix and postfix forms for the following fully parenthesized arithmetic expression: (((4+3)/(6-1))*((2+1)*(3+4)))











  • (3 points) Evaluating arithmetic expressions in postfix form. Find the value of the following arithmetic expression written in postfix form (assumming that we only use one-digit decimal integers): 23+2*42--2/ Apply the algorithm we discussed in class. Trace the execution of the algorithm, and show in sequence how the stack looks like at each step (i.e. after each push and pop). Redraw the stack, do not overwrite.




































    Part II: hands-on programming (40 points)

    Your friends are also college students, but they do not take programming classes. They are arguing that they spend more time than you do on homework assignments. You do not agree, and decided to research the question. You send out a survey to all houses on campus, asking each student to report her major (recorded as an integer code between 1 and 99) and how much time she spent last week doing her homework (in hours).
    The data is collected in a file named data. Each line of the file contains the integer code of a major and the time reported by a student (also an integer). The last line of the file is 0 0 and indicates the end of the data. For example:
    1 5
    2 10
    2 15
    1 20
    3 14
    7 3
    4 6
    4 25
    3 12
    0 0

    Your task is to design a program to read, process and print this data. The file is read in line by line and stored internally in a data structure of your choice. After that, the information is sorted twice: once by the major, then by the time, and printed after each sorting.
    You can reuse any code that you have written so far in this class. You can write the whole program in one file. You do not need to use classes for this program, but you should use struct if appropriate.

    The program will receive the maximum number of points if it obeys the same rules for writing good and readable code as for the homeworks. Remember, a program that runs but may not have all the features implemented is better than one which has all functions implemented but full of bugs, or does not complile. Therefore develop your program in stages and submit the latest version that works.

      To submit:
    1. A file named mid.cpp containing your code.
    2. A typescript showing what you have accomplished.
    3. All submitted by 10:20 as submit Mid.



    Ileana Streinu