CP264 Fall 2018 A2: Functions & Arrays
Qutaiba Albluwi ? 2018
A2: C Functions & Arrays
Due Date: Thursday, September 27th, 2018 at 11:55 pm.
Instructions:
1- Download the file: “cp264_fall2018_A2_template.zip”
2- Extract the above archived file, and you should find the following six files:
1) “main.c”
2) “matrix.h”
3) “matrix.c”
4) “test.h”
5) “test.c”
6) “text.txt”
3- Create a C project in eclipse with the following suggested name: “cp264_ A2”
4- Copy (or import) the above files into the project
5- Open the “matrix.c” file and edit the top comment with your credentials.
6- You should not edit any file except “matrix.c” or the “main.c” function in order to make
the proper testing call.
7- Put your solutions in the designated areas.
8- You are not allowed to import other libraries other than the ones already included
9- You are not allowed to create functions other than the ones already created.
10- Testing functions are provided for you. You need to produce outputs that EXACTLY match
the given output.
Submission Instructions:
Submit ONLY one .c file, which is your modified version of “matrix.c”. Do not change the name
of the file, but make sure that you edit the comment on top with your name and ID.
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.h” and “matrix.c”. All of your coding will be placed in the
“matrix.c” 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. The expected output of
the full testing is available at the file “test.txt”. This file could be used to compare your results. CP264 Fall 2018 A2: Functions & Arrays
Qutaiba Albluwi ? 2018
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 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.
A global matrix of size 10x10, called the masterMatrix, is declared at the top of the matrix library
header file. All of the functions defined in the matrix library make an access to 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 those at positions [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 submatrix
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). CP264 Fall 2018 A2: Functions & Arrays
Qutaiba Albluwi ? 2018
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.
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.
13 int isMatrix( )
Returns 1 if the contents of masterMatrix represent a matrix. All
vectors are considered a valid matrix. The empty matrix is also a valid
matrix. The first element should start at [0][0], all rows should have the
same length, and all colums should have the same length.
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: 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 SIZExSIZE.
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 to the following (also available at “test.txt” file):
------------------------------
Testing isSquareMatrix
------------------------------