L30 Array Dynamic1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Discussion of 2D array exercises

Dynamic arrays

Dynamic Arrays
An Introduction

Department of Computer Science


University of Pretoria

28 May 2024

COS132 Dynamic Arrays 1 / 13


Discussion of 2D array exercises
Dynamic arrays

Write a matrix addition function,


void addMatrices(float a[][4], float b[][4], float result[][4], int
rows, int cols);.
Matrix addition is defined as follows: For two matrices A and B of the same
dimensions m × n:
Cij = Aij + Bij , 0 ≤ i < m, 0 ≤ j < n

where C is the resulting matrix.


What do we know?
Both matrices are m × n
The resulting matrix is also m × n
Consider the following example:

     
1 0 0 0 1 0 1 0 1+1 0+0 0+1 0+0
0 1 1 0 + 0 1 0 1 = 0 + 0 1+1 1+0 0 + 1
0 0 0 1 1 0 1 0 1+1 0+0 0+1 1+0

 
2 0 1 0
= 0 2 1 1
2 0 1 1

COS132 Dynamic Arrays 2 / 13


Discussion of 2D array exercises
Dynamic arrays

Now write the function:


void addMatrices(float a[][4], float b[][4], float result[][4], int
rows, int cols);
Require 2 loops, one to iterate per row (i) and one per column (j)
i runs from 0 to rows - 1
j runs from 0 to cols - 1
result[i][j] = a[i][j] + b[i][j]

COS132 Dynamic Arrays 3 / 13


Discussion of 2D array exercises
Dynamic arrays

Write void addMatrices(float a[][4], float b[][4], float result[][4], int


rows, int cols);
Require 2 loops, one to iterate per row (i) and one per column (j)
i runs from 0 to rows - 1
j runs from 0 to cols - 1
result[i][j] = a[i][j] + b[i][j]

void addMatrices ( f l o a t a [ ] [ 4 ] , f l o a t b [ ] [ 4 ] , f l o a t result [][4] ,


i n t rows , i n t c o l s ) {
f o r ( i n t i = 0 ; i < r o w s ; ++i ) {
f o r ( i n t j = 0 ; j < c o l s ; ++j ) {
result [ i ][ j ] = a[ i ][ j ] + b[ i ][ j ];
}
}
}

COS132 Dynamic Arrays 4 / 13


Discussion of 2D array exercises
Dynamic arrays

Our matrices may have less than 4 rows, but must have 4 columns. This is
due to the requirement that the second array index must be a constant (4)
when sending a static array as a parameter to the functions we write.
How can we generalise this to make provision for multiple size matrices and
especially when we need to multiply matrices?
Matrix multiplication is defined as:
For matrices A of dimension m × n and B of dimension n × p:

n−1
X
Cij = Aik × Bkj , 0 ≤ i < m, 0 ≤ j < p
k=0

where C is the resulting matrix of dimension m × p.


In this case, if n = 4 and p = 4 due to the parameters we have been passing,
then matrix B must be 4 × 4
So far, we have known the size of the array at compile-time. We require
some way of creating arrays more dynamically.
We want to define the number of rows and columns an array has at run-time.
This means we need to make use of heap memory and create the array using
new. It will mean that we need to take responsibility to delete the memory.

COS132 Dynamic Arrays 5 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

To create a 1D dynamic array, we require a pointer to the first element of the


array. For example: int *arr;
We can then reserve the contiguous memory on the heap for the array based
on the size we require, arr = new int[5];
We take responsibility to delete the array, delete [] arr;

COS132 Dynamic Arrays 6 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;

i n t main ( ) {
int ∗ arr ;
int size ;

c o u t << ”How many e l e m e n t s would you l i k e t o s t o r e ? ” ;


c i n >> s i z e ;
a r r = new i n t [ s i z e ] ;

c o u t << ” P l e a s e e n t e r y o u r ” << s i z e << ” v a l u e s ” << e n d l ;


f o r ( i n t i = 0 ; i < s i z e ; i ++) {
c o u t << i + 1 << ” : ” ; c i n >> a r r [ i ] ;
}

c o u t << ”The v a l u e s e n t e r e d a r e : ” ;
f o r ( i n t i = 0 ; i < s i z e ; i ++) { c o u t << a r r [ i ] << ” ” ; }
c o u t << e n d l ;

delete [ ] arr ;
return 0;
}

COS132 Dynamic Arrays 7 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

2D dynamic arrays become a little more complex.


We typically place a 1D array on the heap representing the rows.
Each element in the the 1D array is a pointer to another 1D array
Each row of a 2D dynamic array does not need to have the same number of
“columns”

These structures become difficult to manage. It is necessary to keep track of


how many “columns” are linked to each “row”

COS132 Dynamic Arrays 8 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

Now create a 2 × 3 matrix using 2D dynamic arrays


We make use of double pointers to define the initial variable on the stack:
float **arr;
int rows = 2, cols = 3;
Create the array representing the 2 rows:
arr = new float*[rows];
Create 2 arrays with 3 elements and link them to the rows:
for (int i = 0; i < rows; i++) {
arr[i] = new float[cols];
}

COS132 Dynamic Arrays 9 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

Read values into the arrays


cout << "Type in the matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "[" << i << "][" << j << "] ";
cin >> arr[i][j];
}
}
Delete the matrix
for (int i = 0; i < rows; i++)
delete [] arr[i];
delete [] arr;
arr = NULL;

COS132 Dynamic Arrays 10 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

Write the code to add two matrices


Previously the function was defined as:
void addMatrices(float a[][4], float b[][4], float result[][4],
int rows, int cols);
We can now define the function as follows:
float** addMatrices(float **a, float **b, int rows, int cols);

COS132 Dynamic Arrays 11 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

To help manage the matrices, write functions to allocate the memory on the
heap and free the memory from the heap representing the matrices. You can
add these functions to your matrix library.
float** allocateMatrix(int rows, int cols) {
float** matrix = new float*[rows];
for (int i = 0; i < rows; ++i)
matrix[i] = new float[cols];
return matrix;
}

void deallocateMatrix(float** matrix, int rows) {


for (int i = 0; i < rows; ++i)
delete[] matrix[i];
delete[] matrix;
matrix = NULL;
}
A printMatrix function is always handy ;-)
void printMatrix(float** matrix, int rows, int cols);

COS132 Dynamic Arrays 12 / 13


Discussion of 2D array exercises 1D dynamic arrays
Dynamic arrays 2D dynamic arrays

Write the addMatrices function


float** addMatrices(float **a, float **b, int rows, int cols) {
float** matrix = allocateMatrix(rows, cols);

for (int i = 0; i < rows; ++i) {


for (int j = 0; j < cols; ++j) {
matrix[i][j] = a[i][j] + b[i][j];
}
}

return matrix;
}
What about the multiplyMatrices function? This stands over again for
homework.
Remember the definition is given by: For matrices A of dimension m × n and
B of dimension n × p:

n−1
X
Cij = Aik × Bkj , 0 ≤ i < m, 0 ≤ j < p
k=0

where C is the resulting matrix of dimension m × p.

COS132 Dynamic Arrays 13 / 13

You might also like