CSC 309 Object-Oriented Programming in C++
Homework #2 - Revised version
Due: Sept 21 (Thu) [Loop], 22 (Fri) [DL] (1 week)
Write a program to do the following. Note that this assignment is
intended to let you practice pointers and arrays through general but typical
routines applied to arrays rather than tackling a specific task.
Specifics:
Your program should do the following steps, in this order. The program
must also have 6 functions. They are explained in detail below. Note
that you are free to use/add any variables besides the ones specifically
mentioned below.
- Step 0:
- Declare three integer arrays of size 5, 4 and 9. Name them
ar1, ar2 and ar3, respectively.
- Ask the user for the first value and increment for ar1. This
first value should be assigned into ar1[0].
- Do the same for ar2.
- Step 1:
- Fill the rest of ar1 (index 1 through the end) by calling the
function fillArray.
- Print the elements in ar1 to the terminal by calling the function
printArray.
- Step 2:
- Do the same for ar2 -- Fill the rest of ar2 and print the content.
- Step 3:
- Merge ar1 and ar2 into ar3 by calling the function mergeArrays.
- Print the elements in ar3 (by calling printArray).
- Step 4:
- Shuffle the elements in ar3 by calling the function shuffle.
- Print the elements in ar3 (by calling printArray).
- Step 5:
- Find out the index of the minimum element in ar3 by calling the
function indexOfMinimum.
- Print that index to the terminal.
- Step 6:
- Finally, sort ar3 by calling the function sortArray.
- Print the elements in ar3 (by calling printArray).
Specifics of the six functions are given below.
You must write the function 2, 4 and 6 (printArray, shuffle, sortArray) using
the pointer (*) notation ONLY -- YOU MAY NOT use the array
index ([]) notation, NOWHERE in the functions. The remaining three
functions (1, 3 and 5) can use the [] notation.
- void fillArray(int ar[], int size, int inc);
This function has three parameters: an int array 'ar', its size 'size' and
the increment value 'inc'. The function assumes the 0th element ar[0] is
already filled with some value, and fills the remaining slots, from ar[1]
on, with the value of the preceding element plus 'inc'. For instance, when
ar[0] was filled with 5, ar[1] should be 8, ar[2] shoud be 11 and so on.
- void printArray(int* ar, int size);
This function prints the elements in the array pointed by 'ar' to the
terminal. Print a space between elements for readability.
You may use the pointer (*) notation ONLY in this
function.
- void mergeArrays(int a1[], int size1, int a2[], int size2, int a3[]);
This function has 5 parameters: two arrays (a1 and a2) and their sizes
(size1 and size2), and the third array (a3). The function merges a1
and a2 into a3 -- all elements in a1, then all elements in a2. Thus,
a3 essentially becomes the concatenation of a1 and a2. You may assume
that a3 is allocated with enough space to hold the concatenation.
- void shuffle(int* ar, int size);
This function "shuffles" the elements in the array pointed by 'ar' (and
whose length is 'size'). To do so, you randomly choose two indices and
swap the elements at those indices -- and you do this procedure 'size'
number of times.
To obtain an index, you can use the C++ library function "rand" defined in <cstdlib>
(thus you need to #include this library). You should also apply % by
size because rand returns an integer in the range 0 to RAND_MAX (the largest
value of integer) which is way too larger than the size of the array.int idx = rand() % size; // gives an integer between 0 and size-1 inclusive
Do not worry if two indices end up the same; you don't have to do
anything particular. You may use the pointer (*)
notation ONLY in this function.
- int indexOfMinimum(int ar[], int size);
This function returns the index of the minimum element in the array.
- void sortArray(int* ar, int size);
This function sorts the elements in the array pointed by 'ar'.
You may use the pointer (*) notation ONLY in this
function.
You can write any sorting routine, but here is a BIG HINT.
Following is a simplified Selection Sort algorithm, which works correctly,
written in the [] notation. If you like, you can modify it to use the
* notation (and change any other places where appropriate).void simpleSort(int a[], int length)
{
for (int i = 0; i < length-1; i++) {
for (int j = i+1; j < length; j++) {
if (a[i] > a[j]) {
int t;
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
A sample output:
Enter the first value and increment for ar1: 5 3
Enter the first value and increment for ar2: 2 4
(1) ar1:
5 8 11 14 17
(2) ar2:
2 6 10 14
(3) ar3:
5 8 11 14 17 2 6 10 14
(4) ar3 after shuffle:
14 2 8 5 10 6 11 17 14
(5) Index of the minimum element is: 1
(6) ar3 after sort:
2 5 6 8 10 11 14 14 17
Submission
- Submit the source file (with .cpp extension; you can name the file as
you like) through Course OnLine
(COL).
- Make sure YOU ADD YOUR NAME and COMMENTS in the file.