Description
A matrix in algebra is an array of numbers arranged in rows and columns. In C++, it is
common to implement Matrices using two dimensional arrays. We will be focusing on
manipulating a fixed size 5×5 square matrix (5 rows and 5 columns).
In the following questions you will be asked to write multiple functions doing each a
specific task. In addition, you are required to provide one main function that calls all
the functions that you have created one by one and print the corresponding results
to the screen.
Question 1 (5 pts)
Can a function in C++ return an array? If yes please explain how, and if no please explain
why.
Question 2 (10 pts)
Write a function that will fill a 5×5 matrix by randomly generated numbers. You can use
rand() and srand() functions provided by
The signature of the function is:
void fillMatrix(int matrix[rows][cols]);
rows and cols are global constants defined as follow:
const int rows = 5;
const int cols = 5;
2
Question 3 (10 pts)
Write a function that given a 5×5 matrix as parameter, will print each row of the matrix to
the screen line by line.
The signature of the function is:
void PrintMatrix(int matrix[rows][cols]);
You can test your function by feeding it a randomly generated matrix from question 2.
Question 4 (15 pts)
In algebra, a transpose of a matrix is a matrix whose rows are the columns of the original.
Write a function that will take a 5×5 matrix as parameter and then transpose it in-place
which means without the use of an extra or a temporary matrix while transposing.
The signature of the function is:
void transposeMatrix(int matrix[rows][cols]);
You can test your function by feeding it a randomly generated matrix from question 2.
Question 5 (15 pts)
Write an iterative function that given two 5×5 matrices as parameters, will return a third
5×5 matrix holding the result of the product of the 2 matrices.
You can test your function by feeding it two randomly generated matrices from question 2.
Question 6 (15 pts)
Rewrite the same function from question 5 but using recursion.
3
Question 7 (15 pts)
Rewrite the same functions from question 2, question 3 and question 4 (fillMatrix,
printMatrix and transposeMatrix) using the pointer notation instead of the array notation.
Note that you should be using double pointers to allocate the needed 2-dimensional
arrays.
Question 8 (15 pts)
Is it possible to use a 1-dimensional array instead of 2-dimensional array to implement a
matrix? If yes rewrite the functions from question 2 and question 3 (fillMatrix and
printMatrix) using 1-dimensional array. If no, please explain why.
4