Arrays
Arrays are defined as the collection of similar types of data items stored at contiguous
memory locations. It is one of the simplest data structures where each data element can be
randomly accessed by using its index number.
Representation of an array
As per the above illustration, there are some of the following important points -
o Index starts with 0.
o The array's length is 10, which means we can store 10 elements.
o Each element in the array can be accessed via its index.
Matrix Addition Algorithm using 1D Array:
1. Input: Two matrices (each represented as a 1D array) of dimensions m x n.
2. Initialize an empty array result of size m x n.
3. Iterate through each element of both matrices using a loop:
Add corresponding elements of the matrices and store the result in the
result array.
4. Display the result array as a matrix.
Matrix Multiplication Algorithm using 1D Array:
1. Input: Two matrices, matrix1 of dimensions m x n and matrix2 of dimensions n x
p.
2. Initialize an empty array result of size m x p with all elements as 0.
3. Use nested loops to iterate through rows of matrix1 and columns of matrix2:
For each element in the resulting matrix, calculate the sum of the products of
corresponding elements from matrix1 and matrix2.
Update the element in the result array.
4. Display the result array as a matrix.
Pseudocode for Matrix Addition:
function matrixAddition(matrix1, matrix2, rows, cols):
result = new array of size rows * cols
for i from 0 to rows * cols - 1:
result[i] = matrix1[i] + matrix2[i]
return result
Pseudocode for Matrix Multiplication:
function matrixMultiplication(matrix1, matrix2, rows1, cols1, cols2):
result = new array of size rows1 * cols2, initialized with 0
for i from 0 to rows1 - 1:
for j from 0 to cols2 - 1:
for k from 0 to cols1 - 1:
result[i * cols2 + j] += matrix1[i * cols1 + k] * matrix2[k * cols2 + j]
return result
2D Array
Matrix Addition Algorithm using 2D Array:
1. Input: Two matrices (matrix1 and matrix2) of dimensions m x n.
2. Initialize an empty 2D array result of size m x n.
3. Iterate through each element of both matrices using nested loops:
Add corresponding elements of the matrices and store the result in the
result matrix.
4. Display the result matrix.
Matrix Multiplication Algorithm using 2D Array:
1. Input: Two matrices, matrix1 of dimensions m x n and matrix2 of dimensions n x
p.
2. Initialize an empty 2D array result of size m x p with all elements as 0.
3. Use nested loops to perform matrix multiplication:
Iterate through rows of matrix1 and columns of matrix2.
For each element in the resulting matrix, calculate the sum of the products of
corresponding elements from matrix1 and matrix2.
Update the element in the result matrix.
4. Display the result matrix.
Pseudocode for Matrix Addition:
function matrixAddition(matrix1, matrix2, rows, cols):
result = new 2D array of size rows x cols
for i from 0 to rows - 1:
for j from 0 to cols - 1:
result[i][j] = matrix1[i][j] + matrix2[i][j]
return result
Pseudocode for Matrix Multiplication:
function matrixMultiplication(matrix1, matrix2, rows1, cols1, cols2):
result = new 2D array of size rows1 x cols2, initialized with 0
for i from 0 to rows1 - 1:
for j from 0 to cols2 - 1:
for k from 0 to cols1 - 1:
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result