Course Home | Homework | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science A220/P126
Data Structures and Object-Oriented Programming

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Programming Assignment 01

Arrays and Vectors

Due: Tuesday, 25 January 2005, 8pm

Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.

Collaboration Policy

For this assignment, you must work individually in regard to the design and implementation of your project.

Please make sure you adhere to the policies on academic integrity in this regard.


Contents:


Overview

We apologize, in advance, as this assignment is very contrived and not that interesting. It is meant primarily as a good way to knock off any rust in your programming abilities and to prepare for future assignments. This also gives us a good starting point for you to ask questions, if we are using techniques which are unfamiliar.

There are several particular aspects of the language which we wish to reinforce in this assignment.


Requirements

You are to write a program Warmup.cpp which has a main routine with the following outward behavior. We begin with a high-level description of the task. To begin, we will have you read a series of double-precision numbers from the user and to load this data simultaneously into an array A and a vector V, both of which should be declared "locally."w After loading the data, you should echo both data sets back to the user, to demonstrate that they have been read properly.

At this point, we wish to explore various syntactic ways to "clone" these two original structures.

In the second phase of the program, we wish to have you create a new array B which is a clone of A. You may explore various techniques to accomplish such a task involving arrays. We want you to create two new vectors, W and X, both of which will be clones of the original vector V. In particular, you are required to use the assignment operator to copy vector V to vector W as a whole. You are required to use the copy constructor when initially creating vector X, so that it is originally made to be a clone of vector V.

Once you have created the clones B, W, and X, we wish to have you reverse the contents of each of these new structures (that is, the first entry of B should become the last entry of B, and vice versa). Once you have performed the reversals, we wish to have you print out these new structures and also print out the original structures again (to verify that the order of the contents in the original structure was left unchanged by your modifications to the cloned structures).

In a third phase of the program, we wish to have you create one new array, C, and two new vectors, Y and Z, and to again clone the original data to these new structures, then reverse the contents of the new structures, then print the contents of both the new and original structures. The difference between the second and third phase is that you must declare C, Y and Z "dynamically."

This ends the execution of your program. However we have one additional set of requirements regarding your technique. You will notice that there are some repetitive tasks, namely in printing and in reversing the contents of the array and vector structures. You are required to accomplish these tasks by means of a separate set of print and reversal subroutines designed for these purposes. You must use the same such subroutines, whether or not you are dealing with the locally declared structures or the dynamically declared structures. However, you are allowed to have a separate slate of routines for processing arrays and a separate slate of routines for processing vectors, since these are distinct data types (see the Extra Credit challenge for a way to avoid this distinction!) You are responsible for designing the interface of these routines and the appropriate parameter lists so that you can successfully accomplish the desired tasks.

Please make sure that your program strictly adheres to all of the above specifications.

To get you going in the right direction, we will provide you with a partially completed program Warmup.cpp to get you going.


Files to Submit


Grading Standards

The assignment is worth 10 points. Eight points will be based upon our evaluation of your source code in explicitly following the specifications for this assignment. The remaining two points will be devoted to our evaluation of the discussion in your readme file.


Extra Credit (2 points)

This is a particularly challenging extra credit problem, but one which seems worthwhile for those who really wish to understand good object-oriented programming principles.

In the official assignment, we allowed you to have one routine to print the contents of an array, and a second routine to print the contents of a vector. Of course if you look at these two routines, they are probably almost identical to each other, except for the explicit typing of the structure. Similarly, you were allowed two distinct reversal routines, one for arrays and one for vectors, but again it is quite likely that the body of those routines are nearly identical. As a general rule, if you ever see such nearly duplicated blocks of code, chances are there is a better way to do things!

In particular, arrays and vectors even share a common syntax for accessing elements at a given index, namely as A[index] for an array A and V[index] for a vector V. Because of this common syntax, it is actually possible to write a single print routine which is capable of dealing with either an array or a vector. Likewise, it is possible to write a single reversal routine which is capable of dealing with either an array or a vector. The extra credit challenge is to submit a second version of your code WarmupExtra.cpp which solves the assignment using such unified routines. (the reason that we are having you submit a separate program is because we don't wish to have a mistake in an extra credit attempt adversely affect the score your receive on the original assignment).

To accomplish such unified routines, you will need to make use of what are known as Function Templates, to allow the precise type of your parameter(s) to vary accordingly. These are described in Chapter 2.3.1 of the text. Conceptually, this challenge is straightforward, however because of the idiosyncrasies of both locally and dynamically declared arrays and vectors, it actually is challenging to implement correctly. You will need to carefully consider the needed parameters and the style of parameter passing to use.


Michael Goldwasser
CS A220/P126, Spring 2005
Last modified: Saturday, 22 January 2005
Course Home | Homework | Programming | Schedule & Lecture Notes | Submit