Dana Vrajitoru
C243/A594 Data Structures
C243/A594 Homework 1
Due date: Wednesday, September 4, 2019.
Description. In this homework we will work on a small
program that makes use of a class called MyArray that will
implement a safe array. The goal of the class is for its objects to
work like normal arrays, but where access operations are safe. Thus, a
specific error message will appear when we try to access an index out
of bounds.
Ex. 1. a. Inside your home directory in your Linux account
(the default directory when you log on), create a folder for this
class with the command
mkdir c243
if you have not done this already. Change your working directory
to this folder using the command
cd c243
Repeat this steps to create a directory called hw1 or
whatever you want to name it and change your working directory to it.
b. Download the following files into the folder you have
created for this homework:
Makefile
MyArray.h
MyArray.cc
main.cc
If you work from a terminal in your Linux account, you can copy
them directly doing something like
cp /home/dvrajito/public_html/teach/c243/p1/* ./
which will copy all the files from that directory into yours.
From a Linux system other than our labs, you can also copy them
with a command like
wget http://www.cs.iusb.edu/~danav/teach/c243/p1/Makefile
and then a similar one for which of the other files.
These files are also available on Canvas, in Files, Homework 1.
c. Compile the program with the command
make
Test the program with the command
array
or if it doesn't work,
./array
Note that when you run the program, you should see an error message
about illegal access to an element of the array. This is done on
purpose to test the feature.
Ex. 2. a. Add some functions to the class MyArray
with the given prototypes to do the following operations:
- Input the elements of the array from the console. This function
should ask for the size of the array first, and call an appropriate
function to initialize the array with the size entered by the
user. For this, you will have to figure out which class method you
need to call. Then it must ask the user to input all the elements of
the array one by one and read them from the console.
void input();
- Initialize the elements of the array with random values between 0
and a given maximum limit provided as a parameter. The size of the
array will be provided as the first parameter. The function must
reallocate the array (unless the given size is equal to the current
size), and then assign to each element a random value between 0 and
the limit-1. You can use the function rand() and the
percentage (% limit) operator for this purpose.
void randomize(int theSize, int limit=100);
Note that the default value for the parameter should only appear in
the prototype of the function and not in the implementation.
- Compare the elements of the array to another one and decide if
they are equal or not (2 functions). The operator== must be
explicitly implemented with a for loop comparing the elements
of the arrays one by one. The operator!= should simply return
the opposite of the first one by calling it (one line of code).
bool operator==(MyArray &data);
bool operator!=(MyArray &data);
Note. If the arrays don't have the same size, then they are
not equal no matter what the elements are. This is a simple test since
we know the size of the arrays explicitly and it should be done before
the for loop.
- Compute the scalar product of two arrays, by multiplying the
elements at corresponding positions to each other, and then summing all
of them up. If one array is larger than the other, compute the product
of the common part (based on the smaller size). For example,
{2, 1, 6, 3} * {5, 1, 9, 8} = 2*5 + 1*1 + 6*9 + 3*8 = 89
while
{3, 1, 5, 2, 7, 2} * {7, 2, 4} = 3*7 + 1*2 + 5*4 = 43
and
{4, 6} * {1, 3, 2, 6} = 4*1 + 6*3 = 22
Prototype:
int scalarProduct(MyArray &data);
This function should only return the value and not output it.
b. Modify the main function to test the three
functions and at least one of the operators. For this, in
the main do the following:
- declare two array objects;
- ask the user for a size and randomize the first array with this
size and a value limit of 10 by calling the appropriate class method;
output this array in the main;
- call the function that inputs the array from the user to
initialize the second array;
- compare the arrays using one of the operators you defined and
output an appropriate message;
- compute the scalar product between the arrays and output the
result.
Remove or comment out any part of the original main function
that is not relevant to your program.
Upload to Canvas, under Assignments, Homework 1: all the
source files (.cc and .h). If you modify the Makefile, you have to
upload it too.
Note. If you worked remotely on one of our lab computers and
want to submit the files from your local computer,
see How
To Transfer Files to get those files to your local computer. It's
always a good idea to create home backup files of your homework files
anyway.