To find out largest element



Problem 2.1:
Given a list of elements, write an algorithm to store the list of elements (numbers) in an array and find out the largest element of the list.
Algorithm 2.1: Algorithm to search the largest element of a list
1. Input: x[1 . . . n];
2. for (i = 1; i ≤ n; i = i + 1)
store data to x[i];
3. large = x[1];
4. for (i = 2; i ≤ n; i = i + 1)
if (x[i] > large), large = x[i]; // if any element larger
then the previous_upgrade large
5. Output: the largest number is, large


Find Out A Particular (Specific) Element


Problem 2.2
Given a linear array with data, find out a particular (specific) element of x from the array. We do not know the index (cell) number where the element has been stored.

Algorithm 2.2: Algorithm to search a particular element from a list
1. Input: a[1 . . . n], x;
//A set of data in array a, and variable x i.e., the target element
2. found = 0
3. for (i = 1; i ≤ n; i = i + 1)
{
if (a[i] = = x);
location = i, found = 1, break;
}
4. Output: if (found = = 1), print “FOUND” message and location.
else print “NOT FOUND” message

Find out the summations of odd numbers and even numbers separately


Problem 2.3:
Given a list of integers stored in a linear array. Find out the summations of odd numbers and even numbers separately.
Solution:
  • Given a list of integer, we have to find out the odd numbers and then we shall add those odd numbers.
  • Similarly, we shall find out the even numbers in the list and adding those numbers we shall get the summation of even .
  • To store the result, we require two variables; sum__even and sum__odd.
  • Initially, values of these variables will be zero (0) and every time we find an even number we shall add it to the sum__even and every time we find an odd number, we shall add it to the sum__odd.
  • If a number is divisible by 2 it is even, otherwise odd.
  • We have to start from the first number of the list.
  • If it is even, it will be added with the sum_even and if it is odd, it will be added with the sum_odd.
  • Similarly, we shall access whole list one by one and we shall add them with either sum_even (if a number is even) or sum_odd (if a number is odd).


preview <<


No comments:

Post a Comment

Followers

About Me

Dhaka, Dhaka, Bangladesh
..