[go: up one dir, main page]

0% found this document useful (0 votes)
44 views4 pages

1 - 1D & 2D Array

The document explains arrays as collections of similar data items stored in contiguous memory locations, highlighting their index-based access. It provides algorithms and pseudocode for matrix addition and multiplication using both 1D and 2D arrays. Key points include the initialization of result arrays and the iterative processes for performing operations on matrices.

Uploaded by

nitugp.1216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

1 - 1D & 2D Array

The document explains arrays as collections of similar data items stored in contiguous memory locations, highlighting their index-based access. It provides algorithms and pseudocode for matrix addition and multiplication using both 1D and 2D arrays. Key points include the initialization of result arrays and the iterative processes for performing operations on matrices.

Uploaded by

nitugp.1216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like