5 Solved Problems on Data Structures and Software Principles - Exam | CS 225, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Summer 2005;

Typology: Exams

2010/2011

Uploaded on 06/14/2011

koofers-user-jzq
koofers-user-jzq 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
First Examination
CS 225 Data Structures and Software Principles
Summer 2005
3:00pm 4:15pm Tuesday, July 5
Name:
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. No electronic aids are allowed, either.
You should have 7 sheets total (the cover sheet, plus numbered pages 1-13). The last sheet
is scratch paper; you may detach it while taking the exam, but must turn it in with the
exam when you leave. The back of the second-to-last page contains some class and function
declarations you have seen before; you can use this sheet as reference while taking the exam.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any
compiler error is an exam typo (though hopefully there are not any typos), and (2) assume
you are NOT allowed to write any helper methods to help solve the problem, nor are you
allowed to use additional arrays, lists, or other collection data structures unless we have said
you can.
Problem Points Score Grader
1 20
2 15
3 20
4 20
5 15
Total 90
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download 5 Solved Problems on Data Structures and Software Principles - Exam | CS 225 and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

First Examination

CS 225 Data Structures and Software Principles

Summer 2005

3:00pm – 4:15pm Tuesday, July 5

Name:

NetID:

Lab Section (Day/Time):

  • This is a closed book and closed notes exam. No electronic aids are allowed, either.
  • You should have 7 sheets total (the cover sheet, plus numbered pages 1-13). The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave. The back of the second-to-last page contains some class and function declarations you have seen before; you can use this sheet as reference while taking the exam.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can.

Problem Points Score Grader

Total 90

  1. [Classes in C++ – 20 points].

Consider the following code, which contains a class representing an exam for a course of at most 20 students. The code below would appear in the header file for this class, which we’ll assume is named examdata.h.

#ifndef EXAMDATA_225_H #define EXAMDATA_225_H

#include "string.h" // we will assume the string.h file here // is the same one you’ve seen on the MPs

class ExamData { public:

// Assigns the internal String to be equal to the // parameter String, and sets this collection of // exam scores to be of size zero ExamData(String theName);

// If there are less than 20 exam scores so far, // add the parameter score in the next available cell, // (thus incrementing the number of scores by 1) and // return true. Otherwise, do not change the member variables // at all, and return false. bool addValue(double examScore);

private:

String examName; int numScoresSoFar; double theValues[20]; };

#endif

On the next page, write the examdata.cpp file for this class. The comments on the two functions above indicate what needs to be done by those two functions. You do not need to have any comments in your answer.

  1. [Pointers and References – 15 points].

Consider the following class:

class Circle { public:

// initializes member variables to parameter values Circle(double theRadius, double theX, double theY);

// calculates and returns area of circle double area();

// moves the center of the circle by the first parameter’s // value in the positive X direction and by the second // parameter’s value in the positive Y direction. void translate(int xMove, int yMove);

private:

double radius; // radius of the circle double xCenter; // x-coordinate of the center of the circle double yCenter; // y-coordinate of the center of the circle };

(a) Consider the following code: Circle* cPtr; Given the variable cPtr above, write additional code that will (1) create a local Circle object with radius 3.1, x-coordinate 5.6, and y-coordinate 2.3, (2) assign the pointer above to point to that Circle object, and (3) using the pointer, obtain the area of the circle and print it out.

(b) Imagine you have the following function:

double Foo(Circle const & theParam) { Circle c2(theParam); double theArea = theParam.area(); return (theArea + c2.area()); }

The above function will not compile with the given Circle class. Explain what change you would need to make to the Circle class so that the above code and the Circle class would both compile correctly.

(The Big Three, continued)

  1. [Generic Programming – 20 points].

(a) You want to write a class whose instances are function objects. The class should be named XCharsInRange, and the operator() for the class has a return value of type bool and has three parameters. The first two parameters are of type int, and the third parameter is of type String. You can assume the first parameter is less than or equal to the second parameter. The function will return true if, in the parameter String, the total number of times the ’X’ and ’x’ characters appear, is between the two integers inclusive, return true; otherwise, return false. For example, if the first two arguments to the function were 5 and 7 , then the function returns true if the ’X’ and ’x’ characters appear between 5 and 7 times, inclusive. in the parameter String, total. If the String had two ’X’ characters and five ’x’ characters, for example, then the total is seven and so you would return true. (It is okay to write the definition for this class right into the class declaration itself, i.e. you don’t need to divide things up into a .h and .cpp, though you can if you want to.)

  1. [Inheritance – 15 points].

Consider the following class:

class Window { public:

// initializes member variables to be equal to parameter values Window(String theTitle, int theWidth, int theHeight);

virtual ~Window() { }

// prints out // This window has title T, width W, and height H // where T, W, and H are the values of the member variables virtual void print();

String getTitle(); // returns title of window int getWidth(); // returns width of window int getHeight(); // returns height of window

private:

String title; int width, height; };

You want to write a class DialogWindow that is a derived class of Window, and whose speci- fication is as follows:

  • There is one additional member variable, a String to hold a message to the user
  • There is a constructor that has four parameters, which hold the title, the width, the length, and the message string of the window, respectively. The constructor should initialize the object’s member variables to be equal to the parameter values.
  • There is a method print() which has no parameters, returns nothing, and which prints: This window tells the user D while having title T, width W, and height H where D is the dialog message stored in one of this class’s member variables, and T, W, and H are the title, width, and height stored in the other member variables.

(Inheritance, continued)

(scratch paper, page 1)

(scratch paper, page 2)