CP264 Spring 2018 A3: Functions Arrays
Assignment Files:
You need to download six files: “main.c”, “matrix.h”, “matrix.c”, “test.h” , “test.c”
and “test.txt”.
Create an eclipse C Project with the title: “cp264_A3_studentID” and import to it the above six files.
Submission Instructions:
In one zip folder called: “cp264_A3_studentID.zip”, submit your modified version of the following
four files: “matrix.c”, “matrix.h”, “test.c” and “test.h”. You don’t need to include your
main function, because you are not supposed to change it from the first place (except changing the
testing module number).
Assignment Overview:
The objective is to build a matrix classifier that would, for a given matrix, provide the different
classifications of that matrix.
In the following Wikipedia article, you will find a very long list of matrix types. However, in this
assignment, we will only focus on the following five types: square matrix, unique matrix, uniform.
matrix, symmetric matrix and identity matrix.
In order to classify matrices, we need a matrix representation using the C language. Most of this is
presented in the files: “matrix.c” and “matrix.h”. You will be mainly adding functions to these
two file.
Since there are too many functions in this assignment, it is very easy to get lost in the debugging
process. Therefore, a testing library has been created for you at the “test.c” and “test.h” files.
You may use these files for unit testing of your functions as you write them. Also, you may be asked
to add few testing functions, as necessary. The expected output of the full testing is available at the
file “test.txt”. This file could be used to compare your results.
The Matrix Library:
There are several available implementations for matrices in the C language. However, the one
developed for this assignment is designed to focus only on functions and C arrays.
A matrix is simply a double array with some restrictions. Arrays are normally passed to functions
through pointers, a topic which we did not cover yet. Therefore, we will not be using pointers in this
assignment.
CP264 Spring 2018 A3: Functions Arrays
Qutaiba Albluwi © 2018
A global matrix of size 10x10, called the masterMatrix, is declared at the top of the matrix library.
All of the defined functions access and manipulate this global matrix.
Note that the masterMatrix is fixed in size, i.e. 10x10. At later stages of course, we will learn how
to create and manage dynamic arrays, i.e. of dynamic array size. One way to overcome the size
restriction is to use a delimiter to control the start and end of the matrix. The delimiter we will use
in this assignment is -1, which indicates that a specific matrix element is empty, i.e. not used. For
instance, if we need to create a matrix of size 2x2, then all elements other than (0,0), (0,1), (1,0),
(1,1) should be equal to -1. This trick allows to create matrices of sizes less than or equal to 10x10,
but not larger.
The following functions are defined and fully implemented. You are not supposed to change the
declaration or the implementation of these functions:
# Function signature Description
1 void initializeMatrix()
Initializes the masterMatrix to be an empty matrix. An empty
matrix is defined here as a matrix which all elements are equal to
-1.
2 void reset() Does exactly like intializeMatrix() – just another name
3 void randomMatrix (int r, int c)
Initializes a matrix of size rxc , by initializing elements in the
matrix to random numbers in the range of [0-100]. The matrix is
actually a sub-matrix of masterMatrix, that starts at position
[0][0] and has r number of rows and c number of columns. All
elements outside the sub-matrix are set to -1.
4 void printMatrix (int r, int c) Prints the sub-matrix rxc from masterMatrix.
5 int isEmpty() Returns 1 if the masterMatrix is empty (i.e. all elements equal to -1) and 0 otherwise.
6 int getElement(int r, int c) Returns the masterMatrix element at the position [r][c] and returns errorCode (-99) when failure
7
int setElement
(int r, int c,
int value)
Sets the masterMatrix element at the position [r][c] to
value. Returns 1 if successfully set, and 0 if the set operation
failed.
8 int getRowLength (int r)
Returns the length of the rth row, which is defined as the number
of consecutive non-negative integers starting at element [r][0].
Returns -1 if operation fails. If successfully executed, the return
value should be between 0 (no elements) and 10 (full row).
9
int
getColumnLength
(int c)
Returns the length of the cth column, which is defined as the
number of consecutive non-negative integers starting at element
[0][c]. Returns -1 if operation fails. If successfully executed, the
return value should be between 0 (no elements) and 10 (full
column).
10 Void copyMatrix (int b[10][10]) Copies the contents of the matrix b into the masterMatrix.
CP264 Spring 2018 A3: Functions Arrays
Qutaiba Albluwi © 2018
11 int isEqualTo (int b[10][10]) Check if given matrix is equal to current contents of matsterMatrix. Return 1 if true, and 0 otherwise
12 int isVector()
Returns 1 if the contents of masterMatrix represent a vector. A
vector is either a row or column vector that starts at [0][0], at
which all of its elements are non-negative integers and all other
elements equal to -1.
In order to better understand the functionalities of these functions, read the comments provided at
the top of each function. It is also advisable that, in the main.c file, you execute the command
testMatrixFunction(num) for each of the functions. You only need to use the function number
in the above table in place of the variable num.
For instance to test the function isVector, you can execute the command:
testMatrixFunction(12);
To run a test for all of the functions at the same time, simply run the command:
testAll();
Task 1: Is it a Matrix?
Before a matrix is classified, we need to verify that the current contents of masterMatrix actually
represent a valid matrix.
You would need to develop a function called isMatrix() that returns 1 (True) if masterMatrix
is a valid matrix and 0 (False) otherwise.
The return value is determined through the following:
1- An empty matrix True (Hint: use isEmpty( ) function)
2- A valid vector True (Hint: use isVector() function)
3- If the following three conditions apply True:
3.1 The matrix starts at element [0][0] (i.e. != -1)
3.2 All elements within the matrix are non-negative integers
3.3 All elements outside the matrix are -1
4- Otherwise False
To test your function run the command testMatrixFunction(13) in the main function, and
compare your results with those in “test.txt”.
Task 2: Square Matrix
In this step, you need to develop the function isSquareMatrix() to test if the current contents
of the matrix represent a valid square matrix. A square matrix is a matrix which the number of rows
equal to the number of columns.
The empty matrix is a valid square matrix of size 0x0. If the matrix has only one element, then it is
both a vector and a square matrix of size 1x1. The largest possible square matrix is 10x10.
CP264 Spring 2018 A3: Functions Arrays
Qutaiba Albluwi © 2018
The function should return True (1) if contents of the masterMatrix represent a valid square
matrix and False (0) otherwise.
To test your function run the command testMatrixFunction(14) in the main function, and
compare your results with those in “test.txt”.
Task 3: Identity Matrix
In this step, you need to develop the function isIdentityMatrix() to test if the current contents
of the masterMatrix represent a valid identity matrix. An identity matrix is a square matrix which
all diagonal elements equal to 1, and all other elements equal to 0.
The empty matrix is NOT an identity matrix. A matrix with one element, i.e. size 1x1, is considered
an identity matrix if the value of the element is 1. Here are more examples:
The function should return True (1) if contents of the masterMatrix represent a valid identity
matrix and False (0) otherwise.
To test your function run the command testMatrixFunction(15) in the main function, and
compare your results with those in “test.txt”.
Task 4: Uniform. Matrix
In this step, you need to develop the function isUniformMatrix() to test if the current contents
of the masterMatrix represent a valid uniform. matrix. A uniform. matrix is defined here as a matrix
which all elements have equal value.
The empty matrix is not a uniform. matrix, as it has no elements. A matrix that contains one single
item is always uniform. Note that the uniform. matrix can be square or non-square.
The function should return True (1) if contents of the masterMatrix represent a valid uniform.
matrix and False (0) otherwise.
Create the function with the proper comments on top and and make the proper changes to the
header file. Also, in the testing files, uncomment the relevant functions to enable testing.
To test your function run the command testMatrixFunction(16) in the main function, and
compare your results with those in “test.txt”.
Task 5: Unique Matrix
In this step, you need to develop the function isUniqueMatrix() to test if the current contents
of the masterMatrix represent a valid unique matrix. A uniform. matrix is defined here as a matrix
which all elements have unique values, i.e. no two elements exist that have the same value. It is the
opposite of uniform. matrix.
CP264 Spring 2018 A3: Functions Arrays
Qutaiba Albluwi © 2018
The empty matrix is not a unique matrix, as it has no elements. A matrix that contains one single
item is always unique. Note that a unique matrix can be square or non-square.
Hint: you might need to create another temporary one dimension array to gradually add elements
from the matrix and use linear search to test if an element pre-exist.
The function should return True (1) if contents of the masterMatrix represent a valid unique
matrix and False (0) otherwise.
Create the function with the proper comments on top and and make the proper changes to the
header file. Also, in the testing files, uncomment the relevant functions to enable testing.
To test your function run the command testMatrixFunction(17) in the main function, and
compare your results with those in “test.txt”.
Task 6: Symmetric Matrix
In this step, you need to develop the function isSymmetricMatrix() to test if the current
contents of the masterMatrix represent a valid symmetric matrix. A symmetric matrix is a matrix
that equal its transpose.
A transpose of a matrix is constructed by turning rows into columns and vice versa. For instance:
In a symmetric matrix, the diagonal elements would always be unique, while other elements need
to reflect symmetry. In that regard, only a square matrix could be a symmetric matrix. An example
of a symmetric matrix is:
[
1 7 9
7 3 4
9 4 5
]
Hint: you might need to find the transpose of the matrix and then use isEqualTo function to compare
it to masterMatrix.
The function should return True (1) if contents of the masterMatrix represent a valid symmetric
matrix and False (0) otherwise.
Create the function with the proper comments on top and and make the proper changes to the
header file. Also, in the testing files, uncomment the relevant functions to enable testing.
To test your function run the command testMatrixFunction(18) in the main function, and
compare your results with those in “test.txt”.
Task 7: Classifier
Now that we have all the functions ready, we need to create a function called:
matrixClassifer() that would inspects the contents of masterMatrix and prints all the
applicable matrix classifications.
CP264 Spring 2018 A3: Functions Arrays
Qutaiba Albluwi © 2018
For instance, if masterMatrix is empty then the following should be produced:
Valid Matrix
Empty Matrix
Vector
Square Matrix
Symmetric Matrix
Another example:
For a full list of tests, see “test.txt” file.
Task 8: Testing Module
Unlike the previous modules, you might have noticed that test.c and test.h do not have a
testing module for matrixClassifier. You would need to create one. Your created module
should produce similar results to those shown in the test.txt file.
You would need to do the following:
1- Create a function called: test_matrixClassifer( ) in test.c file.
2- Edit test.h to reflect the presence of the above function
3- Edit testMatrixFunction in test.c
4- Edit testAll in test.c
You can verify your code by running both testMatrixFunction and testAll in the main.c file.
You should get outputs similar to those shown in test.txt.
This assignment might look long, but the actual coding is neither long nor complex
The challenge is to understand the code you have in hand and apply the proper
edits in the proper places.
Good luck