The overall goal of this assignment is to implement a C++ application that makes use of pointers to access data contained in arrays. Throughout this assignment you will refresh your knowledge of control structures, functions, one and two dimensional arrays, and pointer arithmetic. As a bonus, you will be implementing the insertion sort algorithm to sort the data in the given array.
The functional goal is to sort the 2D array given to you. You will use insertion sort to perform sorting column or row wise. see image.
In the end, your goal is to sort the array and use pointers to traverse through the given data. The learning objectives of this session include:
To achieve the learning objectives, students are expected to complete the following:
The following requirements are an overview of what is expected of you in this assignment. More details on how to implement the solution can be found in the Technical Requirements section.
The following snippet is pseudocode for the insertion sort algorithm:
for i <- to length(A)-1
j <- i
while j > 0 and A[j-1] > A[j].
swap A[j] and A[j-1]
j = j <- 1
end while
end for
Which behaves as presented below:
1 2 3 5 6 7 8
1. Download the starter file set. It will contain the following files:
2. The provided main.cpp file is the test file. You are free to test your own program using your own main.cpp file but bear in mind that you will be tested against the provided one.
3. The provided functions.h file contains all of the function prototypes of the functions that are going to be tested. You are free to add any others as you wish. Your main task is to write the C++ implementation code for each of these functions.
4. Use your favorite IDE or editor to modify your files. At first, the program won't compile because the functions' implementation are missing.
5. In functions.h , write an empty implementation for each of the functions specified.
6. The program will now compile. If you try running the program it will tell you that it has failed all the tests. Your goal is to make the program pass all the tests by completing the implementation of the methods given to you. You can implement helper functions if needed. See the following section for more information.
This section will serve as a guideline of what is necessary for the program to pass all the tests. We perform unit testing using the Catch framework. Catch is a header-only framework which means you just need to drop the header file containing the framework into your project. This is the reason behind the catch.hpp file.
You can also see that some code is provided in the main.cpp file. This code tests the functions in your functions.h file. If your code passes all the tests it is ready for submission and will, most likely (cheating is heavily penalized), receive full credit.
The only thing that you need to do is to modify the functions.h files to get the provided functions working. Don't change the function signature as the testing program relies on this to grade your submission.
Let's start explaining what each of the functions must do.
const unsigned int rows = 4;
const unsigned int columns = 4;
char first[rows] [columns] = {
{'z', 'c', 'a', 'f'},
{'a', 'q', 'w', 'e'},
{'y', 'a', 'b', 'z'},
{'n', 'e', 'i', 'o'}
};
// Looks at position (0,0) for 'z'
getcharacter (first, 0, 0);
const unsigned int rows = 4; const unsigned int columns = 4;
char first[rows][columns] = {
{'z', 'c', 'a', 'f'},
{'a', 'q', 'w', 'e'},
{'y', 'a', 'b', 'z'},
{'n', 'e', 'i', 'o'}
}
// Looks at the address of the character in position (0,1).
// address should be something like ox7fff5fbfe281 char* address = getcharacterAddress(first, 0, 1);
// Note: to display the address value, you would need to cast
// the address variable to void*, e.g., see below.
cout << static_cast void*>(address);
const unsigned int rows = 4;
const unsigned int columns = 4;
char first[rows][columns] = {
{'z', 'c', 'a', 'f'},
{'a', 'q', 'w', 'e'},
{'y', 'a', 'b', 'z'},
{'n', 'e', 'i', 'o'}
};
const unsigned int size = 5;
const unsigned int rowNumber = 3;
char row[size];
getRow(first, rowNumber, row);
const unsigned int rows = 4;
const unsigned int columns = 4;
char first[rows][columns] = {
{'z', 'c', 'a', 'f'},
{'a', 'a', 'w', 'e'},
{'y', 'a', 'b', 'z'},
{'n', 'e', 'i', 'o'}
};
const unsigned int size = 5;
const unsigned int columnNumber = 3;
char column[size];
getColumn(first, columnNumber, column);
char test[4] = {'z', 'c', 'a', 'f'};
insertionSort(test, 4);
const unsigned int rows = 4;
const unsigned int columns = 4;
char first[rows][columns] = {
{'z', 'c', 'a', 'f'},
{'a', 'q', 'w', 'e'},
{'y', 'a', 'b', 'z'},
{'n', 'e', 'i', 'o'}
};
sortRow(first, 0);
const unsigned int rows = 4;
const unsigned int columns = 4;
char first[rows][columns] = {
{'z', 'c', 'a', 'f'},
{'a', 'q', 'w', 'e'},
{'y', 'a', 'b', 'z'},
{'n', 'e', 'i', 'o'}
};
sortColumn(first, 0);