Monday, September 5, 2016
Two Dimensional array algorithms
This algorithm traverses two Dimensional array:
Description: Here A is a two – dimensional array with M rows and N columns. This algorithm traverses array A and applies the operation PROCESS to each element of the array.
1. Repeat For I = 1 to M
2. Repeat For J = 1 to N
3. Apply PROCESS to A[I][J]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
4. Exit
|
Explanation: The first for loop iterates from 1 to M i.e. to the total number of rows and the second for loop iterates from 1 to N i.e. to the total number of columns. In step 3, it applies the operation PROCESS to the elements of the array A.
This algorithm transposes the array:
Description: Here A is a two – dimensional array with M rows and N columns. This algorithm transposes the array.
1. Repeat For I = 1 to M
2. Repeat For J = 1 to N
3. Set B[ J ][ I ] = A[ I ][ J ]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
4. Exit
|
Explanation: The first for loop iterates from 1 to M i.e. total number of rows and second for loop iterates from 1 to N i.e. total number of columns. In step 3, the element at location A[ I ][ J ] is assigned to B [ J ][ I ] by the statement B [ J ][I] = A [ I ][ J ].
This algorithm adds two arrays:
Description: Here A is a two – dimensional array with M rows and N columns and B is a two –dimensional array with X rows and Y columns. This algorithm adds these two arrays.
1. If ( M != X ) or ( N != Y ) Then
2. Print: Addition is not possible.
3. Exit
[ End of If ]
4. Repeat For I = 1 to M
5. Repeat For J = 1 to N
6. Set C[ I ][ J ] = A[ I ][ J ] + B[ I ][ J ]
[ End of Step 5 For Loop ]
[ End of Step 6 For Loop ]
7. Exit
|
Explanation: First, we have to check whether the rows of array A are equal to the rows of array B or the columns of array A are equal to the columns of array B. if they are not equal, then addition is not possible and the algorithm exits. But if they are equal, then first for loop iterates to the total number of rows i.e. M and the second for loop iterates to the total number of columns i.e. N. In step 6, the element A[ I ][ J ] is added to the element B[ I ][ J ] and is stored in C[ I ][ J ] by the statement:
C [ I ] [ J ] = A [ I ] [ J ] + B [ I ][ J ]
This algorithm multiplies two arrays:
Description: Here A is a two – dimensional array with M rows and N columns and B is a two –dimensional array with X rows and Y columns. This algorithm multiplies these two arrays.
1. If (M != Y) or (N != X) Then
2. Print “Multiplication is not possible”
3. Else
4. Repeat For I = 1 to N
5. Repeat For J = 1 to X
6. Set C[ I ][ J ] = 0
7. Repeat For K = 1 to Y
8. Set C [ I ][ J ] = C [ I ][ J ] + A[ I ][ K ] * B[ K ][ J ]
[End of Step 7 For Loop]
[End of Step 5 For Loop]
[End of Step 4 For Loop]
[End of If]
9. Exit
|
Explanation: First we check whether the rows of A are equal to columns of B or the columns of A are equal to rows of B. If they are not equal, then multiplication is not possible. But, if they are equal, the first for loop iterates to total number of columns of A i.e. N and the second for loop iterates to the total number of rows of B i.e. X. In step 6, all the elements of C are set to zero. Then the third for loop iterates to total number of columns of B i.e. Y. In step 8, the element A[ I ] [ K ] is multiplied with B[ K ][ J ] and added to C[ I ][ J ] and the result is assigned to C[ I ] [ J ] by the statement:
C[I][J] = C[I][J] + A[I][K] * B[K][J]
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment