1.

Define an array. Also write an algorithm for searching an element in the array using any one method that you are familiar with.

Answer»

An array is a collection of elements with same data type. The index of first element is 0 (zero) and the index of last element is size -1. There are 2 methods linear search and binary search.

Searching: It is the process of finding the position of the given element. 

1. Linear search: 

In this method each element of the array is compared with the element to be searched starting from the first element. If it finds the position of the element in the array is returned.

2. Binary search: 

It uses a technique called divide and conquer method. It can be performed only on sorted arrays. First we check the element with the middle element. There are 3 possibilities. The first possibility is the searched element is the middle element then search can be finished. 

The second possibility is the element is less than the middle value so the upper bound is the middle element. The third possibility is the element is greater than the middle value so the lower bound is the middle element. Repeat this process.8.3 Two dimensional (2D) arrays.

Some occasions we have to store 6 different marks of 50 students. For this we use 2D arrays. An array with two subscripts is used, eg. int mark[r][c]; Here r is the row and c is the column.



Discussion

No Comment Found

Related InterviewSolutions