21

Arrays

  • Upload
    uos

  • View
    35

  • Download
    0

Embed Size (px)

Citation preview

GROUP MEMBERS:

HASEEB ILYAS

018

MUHAM

MAD A

WAIS

0

12

MUZAM

MIL

HUSSAIN

005

TOPIC

Arrays

WHAT IS ARRAY?

An array is a group of consecutive memory locations with same name and data type.

The objects in an array are called elements of array. The total number of elements in an array is called length.

The element of an array is accessed by its subscript value or index value.

ADVANTAGES OF ARRAYS

Arrays are used to process a large amount of data of same type.

Arrays are used to reduce the size of program.

Arrays can store many values easily and quickly.

The searches process can be applied on array easily.

TYPES OF ARRAYS

1. One-dimensional arrays2. Multi-dimensional arrays

ONE D-ARRAY

One dimensional array is also called a list or linear array. It consist of only one column or one row.

For example the temperature of each hour of a day is stored in an array. The name of array is “temp” and its elements are temp[0],temp[1],………….temp[23].

DECLARATION OF 1-D ARRAY

Defining the name of array, its types and total numbers of elements of an array is called declaring of the array.

Syntax data type array_name [n];“n” represents the total number of elements of array. e.g Int xyz[5];

1-D ARRAY INITIALIZATION The process of assigning a values to array element at the

time of array declaration is called initialization of array. Syntax data type identifier[n]={list of values}; e.g int mark[4]={70,56,84,64,23}; int arr[2]={‘p’,’a’,’k’} Initializing values may be constant or character

type.

SEARCHING IN AN ARRAY

The process of finding the required data in array. Searching become more important when length of array is very large.

There are two techniques for searching:1. Sequential search2. Binary search

SEQUENTIAL SEARCH It is also called linear or serial search. It

follow the following steps to search value:

1. Visit the first element of array and compare its with required value.

2. If the value of array matches with the desired value then search is complete.

3. If value of array does not match then move to next element and repeat the same process.

BINARY SEARCH1. It is a quicker method to search value in array. But it can

only search for sorted array. Following steps are used:2. It locate the middle element of array and compare it with

desired value.3. If they are equal the search is complete.4. If they are not equal, it reduce the search half an array.5. If the required value is less than the middle value then it

search for first middle half otherwise second half.6. If required number is not found then loop is complete

without successful search.

SORTING ARRAYS The process of arranging the values in a

particular order. An array can be sorted in two ways:

a) Ascending order e.g 23, 34,56,78 b) Descending order e.g 78,56,34,23

TECHNIQUE OF SORTING ARRAYThere are two technique of sorting of array

1. Selection sort2. Bubble sort

SELECTION SORTIt is a technique that sort an array. It selects an

element in array and move it to its proper position.

It works as follow:.

1. Find the minimum value in the list.2. Swap it with value at first position.3. Sorts the remainder of the list excluding the first

value.

BUBBLE SORT It is also known as exchange sort. It repeatedly

visits the array and compare two values at a time.it works as follow:

1) It compare the adjacent element. If the first is greater than second, swaps them.

2) Repeat this for each pair of adjacent element, starting with the first two and ending with last two.

3) Keep repeating for one fewer element each time until there is no pairs to compare.

TWO D-ARRAY

It is considered as table that consist of rows and columns. Each element in an array is referred with the help of two indexes. One indicates row and second column.

DECLARING 2-D ARRAYSyntax: data_type identifier[row][column]; e.g: int arr[4][3];

0,0 0.1 0,21,0 1,1 1,22,0 2,1 2,23,0 3,1 3,2

2-D INITILIZATIONIt can also be initialized at the time of declaration.

It can be performed by assigning the initial values in braces separated by commas.

Some important points:1) The elements of each rows are enclosed within the

braces and separated by commas.2) All rows are enclosed within braces.3) For number arrays, if all elements are not specified , the

un-specified element are initialized by zero.

2 D ARRAYfor example:

Int arr[4][3]={{12,5,22}, {95,3,41}, {77,6,53} {84,59,62}}

12 5 2295 3 4177 6 53

84 59 62