
The Summation Of The Diagonal Elements
The Summation Of The Diagonal Elements
Algorithm 2.9: Algorithm to find out summation of diagonal elements
1. Input: B[1 . . . n, 1...n], sum = 0;
//a two dimensional array
2. Find each diagonal element and add them
for (i = 1; i ≤ n; i = i + 1)
{for (j = i; j ≤ n; j = j + 1)
if (i = j  i + j = n + 1), sum = sum + B[i, j]
}
4. when n is odd
if (n %2= 1), sum = sum  B [ n+1/2, n+1/2]
3. Output: Print sum as the result of summation of diagonal elements.
[Note: Diagonal elements are those elements whose indexes are equal (i.e., i = j) or their summation results n +1 (i.e., i + j = n +1). Since the middle is added twice so it has been subtracted when the value of n is odd]
Problem as assignment
Problem A2:
There are 40 students in class. They have written 4 class tests of a course. Write an algorithm to find out the average mark of the best 3 class tests for each student.
Write program for your for your algorithm.
This is the end
of
Array
 We shall start from the first number of the array and check it whether it is a diagonal element or not. If it is a diagonal element, it will be added to the sum, which is a variable initially set zero to store the result of summation of all diagonal elements.
 We shall advance to the next element and one by one we shall go through the whole list.
 If the value of n is an odd number, then the middle number of each of the two diagonals will be common to both diagonals.
 So, this number will be added twice. So, this number will be subtracted from final summation
Algorithm 2.9: Algorithm to find out summation of diagonal elements
1. Input: B[1 . . . n, 1...n], sum = 0;
//a two dimensional array
2. Find each diagonal element and add them
for (i = 1; i ≤ n; i = i + 1)
{for (j = i; j ≤ n; j = j + 1)
if (i = j  i + j = n + 1), sum = sum + B[i, j]
}
4. when n is odd
if (n %2= 1), sum = sum  B [ n+1/2, n+1/2]
3. Output: Print sum as the result of summation of diagonal elements.
[Note: Diagonal elements are those elements whose indexes are equal (i.e., i = j) or their summation results n +1 (i.e., i + j = n +1). Since the middle is added twice so it has been subtracted when the value of n is odd]
Problem as assignment
Problem A2:
There are 40 students in class. They have written 4 class tests of a course. Write an algorithm to find out the average mark of the best 3 class tests for each student.
Write program for your for your algorithm.
This is the end
of
Array
The Summation Of Boundary Elements
Algorithm 2.8: Algorithm to find the summation of boundary elements
1. Input: A[1…m, 1…n], sum = 0;
//a twodimensional array and a variable
2. Find each boundary element
for (i = 1; i ≤ m; i = i + 1)
for (j = 1; j ≤ n; j = j + 1)
if (i = 1  j = 1  i = m  j = n),
sum = sum + A[i, j];
[Boundary elements are those elements whose index i = 1 or j = 1, and those whose index i = m or j = n) and add it with sum (previous result)]
3. Output: Print sum as the result of summation of boundary elements
Back
Subscribe to:
Posts (Atom)