CPSC 225, Spring 2002
Quiz #2, February 11

This is the second quiz in CPSC 225: Intermediate Programming.


Question 1: Suppose that Point is the name of a class in C++. Explain the following line of C++ code. What is being done here? What has to be in the class, for this to be legal?

Point  pt(25,15);

Answer: This is a variable declaration. It is declaring a variable named pt. The type of this variable is Point. That is, pt holds an object of type Point. A constructor is called, with parameters (25,15), to initialize the object. For this to be legal, the class Point must contain a public constructor that has two parameters of type int.


Question 2: Suppose you want a data type to represent dates, where a date consists of a month, a day, and a year. Give the definition of a simple C++ data type to do this. (It can be either a struct or a class.)

Answer:

       struct Date {                     class Date {
          int month;                        public:
          int day;          OR                 int month;
          int year;                            int day;
       };                                      int year;
                                         };

(Note: In a class, members are private by default. If you want to have public members, you have to make them public explicitely. In a struct, members are public by default. A common mistake is to forget the semicolon at the end of the definition.)


Question 3: Suppose that a two dimensional array named data is defined as follows. Write a C++ code segment that will fill every spot in the array with the value zero.

double  data[100][50];

Answer:

            for (int row = 0; row < 100; row++) 
               for (int col = 0; col < 50; col++)
                  data[row][col] = 0;

Question 4: Write a complete C++ function definition. The function will search in an array of ints to determine whether or not a given int value occurs in the array. The function needs three parameters: the array, the number of items in the array, and the int value that the function is to search for. It can return a value of type bool.

Answer:

          bool search( int A[], int items, int searchFor ) {
             
                // Searches for searchFor among the array elements
                // A[0], A[1], ..., A[items-1].  If searchFor is found,
                // the return value is true; otherwise, it is false.
                
             for (int index = 0; index < items; index++) {
                if ( A[index] == searchFor ) {
                   return true;  // The desired item has been found.
                }
             }
             
             // At this point, ALL the elements of the array have been
             // examined, without finding searchFor.  We can conclude 
             // that searchFor is not in the array, so we can return
             // false as the value of the function.
             
             return false;
             
          }

David Eck, eck@hws.edu