|
|||||||||||||||
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 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
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
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
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 two-dimensional 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
Find Out the summation Of the boundary elements Of the array
Find Out the summation Of the boundary elements Of the array
Problem 2.8:
Given a two-dimensional array, find out the summation of the boundary elements of the array. Here no element should be added twice.
Solution:
>>First, we have to identify the boundary elements.
The summation Of the boundary elements Of the array
Problem 2.8:
Given a two-dimensional array, find out the summation of the boundary elements of the array. Here no element should be added twice.
Solution:
>>First, we have to identify the boundary elements.
- In a two dimensional array, elements of first column and the last column and the first row and last row are the boundary elements as shown in the Figure-2.8.
- When i is 1, the row is the first row and when i is m (where m represents the number of rows in the array), the row is the last row.
- Similarly, when j = 1, the column is the first column and the index of the last column is j = n. So, a number in a two-dimensional array is a boundary element if i = 1, i = m, j = 1 or j = n
The summation Of the boundary elements Of the array
- We shall start from the first number of the array.
- If it is a boundary element, the number will be added to the sum (which is a variable to store the result and initially it is set zero).
- We shall check every number whether it is a boundary element or not, if the number is a boundary element it will be added to sum.
- Otherwise, we shall proceed with the next number of the list (array) and continue the process to the end of the list.
Location of an element
Location of an element of a two-dimensional array
Row-major Order:
Row-major Order:
- If Loc (A[i, j]) denotes the location in the memory of the element A[i][j] or Aij, then in row-major order –
Loc (A[i, j]) = Base (A) + (n (i - 1) + (j - 1)) * w;
- Here Base (A) is starting or base address of the array A, n is the number of columns and w is the width of each cell, i.e, number bytes per cell.
Column-major Order:
In column-major order,
Example:
Base address, Base (A) = 100, Size of the array = 5 × 6. If the type of array is integer then find Loc (A[4, 3]).
Solution:
(2 bytes for each integer cell in C/C++)
If the array is stored in row-major order:
Loc (A[4, 3]) = Base (A) + (n (i - 1) + (j - 1))* 2
= 100 + (6 × 3 + 2)* 2
= 100 + 40
= 140
If the array is stored in memory in column-major order:
Loc (A[4, 3]) = Base (A) + m (j - 1) + (i - 1)* 2
= 100 + (5 × 2 + 3)* 2
= 100 + 26
=126
Loc (A[i, j]) = Base (A) + (m (j - 1) + (i - 1)) * w;
Here Base (A) is starting or base address of the array A, m is the number of rows and w is the cell widthExample:
Base address, Base (A) = 100, Size of the array = 5 × 6. If the type of array is integer then find Loc (A[4, 3]).
Solution:
(2 bytes for each integer cell in C/C++)
If the array is stored in row-major order:
Loc (A[4, 3]) = Base (A) + (n (i - 1) + (j - 1))* 2
= 100 + (6 × 3 + 2)* 2
= 100 + 40
= 140
If the array is stored in memory in column-major order:
Loc (A[4, 3]) = Base (A) + m (j - 1) + (i - 1)* 2
= 100 + (5 × 2 + 3)* 2
= 100 + 26
=126
Two dimensional array representation in memory
- The elements of a two dimensional array are stored in computer’s memory row by row or column by column.
- If the array is stored as row by row, it is called row-major order.
- If the array is stored as column by column, it is called column-major order.
- Suppose there is a two-dimensional array of size 5 × 6. That means, there are 5 rows and 6 columns in the array.
- In row-major order, elements of a two dimensional array are ordered as –
- A11, A12, A13, A14, A15, A16, A21, A22, A23, A24, A25, A26, A31, ............, A46, A51, A52, .......,A56.
- and in column-major order, elements are ordered as –
- A11, A21, A31, A41, A51, A12, A22, A32, A42, A52, A13, ............, A55, A16, A26, .......,A56.
Time complexity
This complexity is related to execution time of the algorithm.
It depends on the number of element (item) comparisons and number of element movement (movement of data from one place to another).
However, the complexity of the most of the algorithms described here related to the number of element comparisons.
That means, the complexity of the algorithm is computed with respect to the total number of element (item) comparisons needed for the algorithm
Preview <<<<<<
It depends on the number of element (item) comparisons and number of element movement (movement of data from one place to another).
However, the complexity of the most of the algorithms described here related to the number of element comparisons.
That means, the complexity of the algorithm is computed with respect to the total number of element (item) comparisons needed for the algorithm
Preview <<<<<<
Importance of data structure
- Computer science as well as computer engineering deals with two jargons which are software and hardware.
- Without software, hardware (electrical, mechanical, electronic parts of computer that we see and touch) is useless.
- So, study of software is very important in computer science, and software consists of programs which use different types of data.
- In a program we not only use elementary data items but also use different types of organized data. That means we use data structure in a program. As we know we write programs to solve problems. That is, to solve problems we have to use data structures. The different data structures give us different types of facilities.
- If we need to store data in such a way that we have to retrieve data directly irrespective of their storage location. We can get this facility using one type of data structure such as array gives us such facility.
program
Program
- Sequence of instructions of any programming language that can be followed to perform a particular task.
- For a particular problem, at first we may write an algorithm then the algorithm may be converted into a program.
- In a program usually we use a large amount of data. Most of the cases these data are not elementary items, where exists structural relationship between elementary data items.
That means the programs uses data structure(s).
Algorithm
Set or sequence of instructions (steps) that can be followed to perform a task (problem).
Do not strictly follow grammar of any particular programming language.
However its language may be near to a programming language.
preview <<
Do not strictly follow grammar of any particular programming language.
However its language may be near to a programming language.
q Each and every algorithm can be divided into three sections.
v First section is input section, where we show which data elements are to be given.
v The second section is very important one, which is operational or processing section. Here we have to do all necessary operations, such as computation, taking decision, calling other procedure (algorithm) etc.
v The third section is output, where we display the result found from the second section
preview <<
BackgRound2
Example of Data Structures:
Array, Linked List, Stack, Queue, Tree, Graph, Hash
Table etc.
Types of elementary data item:
Character, Integer, Floating point numbers etc.
Expressions of elementary data in C/C++
Elementary data item - Expression in C/C++
Character - char
Integer - int
Floating point number - float
preview <<
Array, Linked List, Stack, Queue, Tree, Graph, Hash
Table etc.
Types of elementary data item:
Character, Integer, Floating point numbers etc.
Expressions of elementary data in C/C++
Elementary data item - Expression in C/C++
Character - char
Integer - int
Floating point number - float
preview <<
BackgRound
Elementary items constitute a unit and that unit may be considered as a structure.
.>> structure may be treated as a frame or proforma where we organize some elementary items in different ways.
Data structure is a structure or unit where we organize elementary data items in different ways.
>>That means, a data structure is a means of structural relationships of elementary data items for storing and retrieving data in computer’s memory.
Usually elementary data items are the elements of a data structure.
However, a data structure may be an element of another data structure. That means a data structure may contain another data structure
.>> structure may be treated as a frame or proforma where we organize some elementary items in different ways.
Data structure is a structure or unit where we organize elementary data items in different ways.
>>That means, a data structure is a means of structural relationships of elementary data items for storing and retrieving data in computer’s memory.
Usually elementary data items are the elements of a data structure.
However, a data structure may be an element of another data structure. That means a data structure may contain another data structure
Subscribe to:
Posts (Atom)
Followers
About Me
- ..
- Dhaka, Dhaka, Bangladesh
- ..