辅导CP264留学生、解析C项目/C语言、辅导C语言编程、辅导C语言程序

- 首页 >> C/C++编程

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

------------------------------

Case 1: Empty Matrix:

create Empty Matrix

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSquareMatrix = 1

Case 2: Matrix of size 1x1:

set first element to 9

9 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSquareMatrix = 1

Case 3: Matrix of size 1x2:

set second element to 9

9 9 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSquareMatrix = 0

Case 4: Matrix of size 2x2:

use global matrix (i2)

1 0 - - - - - - - -

0 1 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSquareMatrix = 1

Case 5: Invalid Matrix:

set Last element to 9

1 0 - - - - - - - -

0 1 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - 9

isSquareMatrix = 0

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

Case 6: Matrix of size 3x3:

Use global matrix (s3)

1 7 3 - - - - - - -

7 4 8 - - - - - - -

3 8 6 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSquareMatrix = 1

Case 7: Matrix of size 4x6:

create random Matrix of size 4x6

11 47 4 53 100 85 - - - -

40 34 13 54 94 86 - - - -

39 8 86 61 13 76 - - - -

32 45 99 83 83 95 - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSquareMatrix = 0

Case 8: Matrix of size 10x10:

create random Matrix of size 10x10

11 47 4 53 100 85 40 34 13 54

94 86 39 8 86 61 13 76 32 45

99 83 83 95 37 39 70 18 46 17

39 10 42 57 9 60 33 74 86 2

61 96 34 2 86 98 41 79 50 42

20 85 24 26 61 73 84 53 95 75

96 4 40 100 8 73 88 95 98 39

5 21 76 22 37 37 44 59 66 33

80 15 63 41 68 37 50 6 70 59

3 75 94 15 6 70 44 78 71 5

isSquareMatrix = 1

Task 2: 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.

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

To test your function run the command testMatrixFunction(15) in the main function, and

compare your results to the following (also available at “test.txt” file):

------------------------------

Testing isIdentityMatrix

------------------------------

Case 1: Empty Matrix:

create Empty Matrix

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIdentityMatrix = 0

Case 2: I1:

setFirst element to 1

1 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIdentityMatrix = 1

Case 3: invalid I1:

setFirst element to 0

0 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIdentityMatrix = 0

Case 4: I2:

use global matrix (i2)

1 0 - - - - - - - -

0 1 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIdentityMatrix = 1

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

Case 5: I3:

use global matrix (i3)

1 0 0 - - - - - - -

0 1 0 - - - - - - -

0 0 1 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIdentityMatrix = 1

Case 6: invalid I3:

Modify last identity matrix

1 0 0 - - - - - - -

0 1 0 - - - - - - -

0 0 2 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIdentityMatrix = 0

Task 3: 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.

To test your function run the command testMatrixFunction(16) in the main function, and

compare your results to the following (also available at “test.txt” file):

------------------------------

Testing isUniformMatrix

------------------------------

Case 1: Empty Matrix:

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniformMatrix = 0

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

Case 2: Single Element Matrix:

5 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniformMatrix = 1

Case 3: Uniform Vector:

5 5 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniformMatrix = 1

Case 4: Invalid Matrix:

5 5 - - - - - - - -

5 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniformMatrix = 0

Case 5: Uniform Square Matrix 2x2

5 5 - - - - - - - -

5 5 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniformMatrix = 1

Case 6: Uniform non-square Matrix 2x3

5 5 5 - - - - - - -

5 5 5 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

isUniformMatrix = 1

Case 7: Non uniform 2x3 matrix

5 5 8 - - - - - - -

5 5 5 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniformMatrix = 0

Task 4: 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.

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: There is a function provided to you, called find which performs a linear search on an array to

check if an element is found in it. You might use the function in your implementation, but it is not

necessary.

The function should return True (1) if contents of the masterMatrix represent a valid unique matrix

and False (0) otherwise.

To test your function run the command testMatrixFunction(17) in the main function, and

compare your results to the following (also available at “test.txt” file):

------------------------------

Testing isUniqueMatrix

------------------------------

Case 1: Empty Matrix:

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniqueMatrix = 0

Case 2: Single Element Matrix:

8 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniqueMatrix = 1

Case 3: Unique Vector:

8 6 4 2 - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniqueMatrix = 1

Case 4: Non-unique Vector:

8 6 4 2 8 - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniqueMatrix = 0

Case 5: Invalid Matrix:

8 6 4 2 8 - - - - -

5 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isUniqueMatrix = 0

--------------------

Case 6: Full Unique Matrix:

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50

51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70

71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90

91 92 93 94 95 96 97 98 99 100

isUniqueMatrix = 1

Case 7: Full Non-Unique Matrix:

1 2 3 4 5 6 7 8 9 10

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

11 12 13 14 15 16 17 18 19 100

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50

51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70

71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90

91 92 93 94 95 96 97 98 99 100

isUniqueMatrix = 0

Task 5: 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 equals 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.

Note that an empty matrix is a VALID symmetric matrix

The function should return True (1) if contents of the masterMatrix represent a valid symmetric

matrix and False (0) otherwise.

To test your function run the command testMatrixFunction(18) in the main function, and

compare your results to the following (also available at “test.txt” file):

------------------------------

Testing isSymmetricMatrix

------------------------------

Case 1: Empty Matrix:

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

- - - - - - - - - -

isSymmetricMatrix = 1

Case 2: Single Element matrix:

8 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSymmetricMatrix = 1

Case 3: Identity Matrix 3x3:

1 0 0 - - - - - - -

0 1 0 - - - - - - -

0 0 1 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSymmetricMatrix = 1

Case 4: valid symmetric matrix 3x3:

1 7 3 - - - - - - -

7 4 8 - - - - - - -

3 8 6 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSymmetricMatrix = 1

Case 5: invalid symmetric matrix 3x3:

1 7 10 - - - - - - -

7 4 8 - - - - - - -

3 8 6 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSymmetricMatrix = 0

Case 6: Invalid Matrix:

1 7 10 - - - - - - -

7 4 8 - - - - - - -

3 8 7 - - 10 - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSymmetricMatrix = 0

Case 7: a vector:

1 2 3 4 5 6 7 8 - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isSymmetricMatrix = 0

Task 6: Binary Matrix

In this step, you need to develop the function isBinaryMatrix() to test if the current contents of

the masterMatrix represent a valid binary matrix. A binary matrix is defined as a matrix in which

all of its elements are either 1 or 0. Note that an empty matrix is not a binary matrix.

The function should return True (1) if contents of the masterMatrix represent a valid binary matrix

and False (0) otherwise.

To test your function run the command testMatrixFunction(19) in the main function, and

compare your results to the following (also available at “test.txt” file):

------------------------------

Testing isBinaryMatrix

------------------------------

Case 1: Empty Matrix:

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 0

Case 2: Valid Single Element Matrix:

1 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 1

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

Case 3: invalid Single Element Matrix:

4 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 0

Case 4: Valid binary vector:

1 - - - - - - - - -

0 - - - - - - - - -

0 - - - - - - - - -

1 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 1

Case 5: Invalid Matrix:

1 - - - - - - - - -

0 - - - - - - - - -

0 - - - - - - - - -

1 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

1 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 0

Case 6: I3

1 0 0 - - - - - - -

0 1 0 - - - - - - -

0 0 1 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 1

Case 7: Non-square binary matrix 5x4

0 1 0 1 - - - - - -

0 1 0 1 - - - - - -

0 1 0 1 - - - - - -

0 1 0 1 - - - - - -

0 1 0 1 - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

isBinaryMatrix = 1

Case 8: invalid binary matrix 5x4

0 1 0 1 - - - - - -

0 1 0 1 - - - - - -

0 1 4 1 - - - - - -

0 1 0 1 - - - - - -

0 1 0 1 - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isBinaryMatrix = 0

Task 7: Incremental Matrix

In this step, you need to develop the function isIncrementalMatrix() to test if the current

contents of the masterMatrix represent a valid incremental matrix. An incremental matrix is a

matrix in which every element is greater than or equal to the element before it. Elements are

ordered from left to right and then top to bottom. For example, each of the following is a valid

incremental matrix:

[3 4 5] [

1 5 7

8 8 10] [

1 1 2

2 3 3

4 4 5

]

The function should return True (1) if contents of the masterMatrix represent a valid incremental

matrix and False (0) otherwise. Note that the empty matrix is an Invalid incremental matrix.

To test your function run the command testMatrixFunction(20) in the main function, and

compare your results to the following (also available at “test.txt” file):

------------------------------

Testing isIncrementalMatrix

------------------------------

Case 1: Empty Matrix:

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIncrementalMatrix = 0

Case 2: single element Matrix:

7 - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018

- - - - - - - - - -

- - - - - - - - - -

isIncrementalMatrix = 1

Case 3: Valid incremental Vector:

7 9 10 10 - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIncrementalMatrix = 1

Case 3: Valid 2x3 incremental matrix:

1 5 7 - - - - - - -

8 8 10 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIncrementalMatrix = 1

Case 4: invalid 2x3 incremental matrix:

1 5 7 - - - - - - -

6 8 10 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIncrementalMatrix = 0

Case 5: invalid matrix:

1 5 7 - - - - - - -

8 8 10 - - - - - - -

10 11 - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

isIncrementalMatrix = 0

Case 6: valid 3x3 incremental matrix:

1 5 7 - - - - - - -

8 8 10 - - - - - - -

10 11 11 - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

- - - - - - - - - -

CP264 Fall 2018 A2: Functions & Arrays

Qutaiba Albluwi ? 2018


站长地图