Saint Louis University |
Computer Science A220/P126
|
Dept. of Math & Computer Science |
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.
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.
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.
When declaring an object, such as an int, one might declare it locally, as follows:
int x;in this case, the variable x now denotes the object itself. The system will have allocated memory for storing one integer (though admittedly we have not actually initialized that memory to anything yet). The point is that the memory exists and is ready to be used. Also, whenever this variable declaration goes out of scope, the memory presumably will be taken back automatically by the system. Alternatively, we can have such data allocated dynamically as follows:
int *px;What is important to note is that the first of the two statements declares a variable px which is a pointer to potential int data. That statement alone does not cause the system to set aside any memory for storing such an int (though it does cause the system to set aside memory for storing the pointer itself!) The second of the two statements uses the new keyword to instruct the system to dynamically allocate memory to hold a single int data piece (and it sets the pointer px to be the memory address of the dynamically allocated space).
px = new int;
In the context of arrays, there is again a distinction between declaring arrays A and B as follow:
double A[n];In this example, A was declared locally and B was declared dynamically. However in the context of arrays, both variables A and B are of identical type, namely a pointer to the first element of the array.
double *B;
B = new double[n];
We often use the assignment operator (=) to make an assignment of data. For primitive data types, such as an int, we may say
int x,y;The first line declares both variables (locally). The second line sets the value of the variable denoted as x. The third line set the value of the left-hand-side expression to equal the current value of the right-hand-side expression. Note that there are still two distinct integers represented in memory and that a future change to one of the variables does not have any coordinated effect on the other.
x = 5;
y = x;
We need to be a bit more careful in understanding the use of the assignment operator for arrays and other types of objects.
For most data types, including objects from defined classes, there is generally a default way to construct the data. For example, when you declare a string object as
string firstname;the string will be constructed yet set equal to the empty string by default (for primitive data types, there might not even be a default setting).
Alternatively, when constructing a new object, it is often possible to specify that it should be constructed so as to look identical to some already existing structure. This is done using the syntax of a "copy constructor" and is discussed in Chapter 1.5.2.
Once we understand the distinction between an object and a pointer to an object, we need to carefully understand options when passing such data as a parameter.
In writing the most reusable, generic code, we will make great use of templated classes. We will explain more down the road, but for now, the review in Chapter 1.5.5 of the text should be sufficient for this assignment.
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.
Warmup.cpp
We expect that your entire code will lie in this single file,
but if this is not the case, please submit any additional such
files, together with compilation instructions.
Readme File
For every assignment, you will be asked to
submit such a text file. In general, this provides a way for
you to add any further comments you wish to make to the grader.
For many assignments, there will also be additional explicit
requirements for the contents of this file.
For this assignment, we wish to have you discuss your chosen design for the print and reversal subroutines, and how this interface was effected by the need to cope with both locally and dynamically declared data. You are also free to add any additional comments about other aspects of your program if you wish.
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.
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.