The Summation Of The Diagonal Elements

The Summation Of The Diagonal Elements
  • 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 A-2:
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

No comments:

Post a Comment


About Me

Dhaka, Dhaka, Bangladesh