1 d arrays in c++

25
1D ARRAYS 1D ARRAYS POONAM WALIA POONAM WALIA KV SHALIMAR BAGH KV SHALIMAR BAGH

Upload: poonamrwalia

Post on 30-Nov-2014

8.619 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 1 D Arrays in C++

1D ARRAYS1D ARRAYS

POONAM WALIAPOONAM WALIAKV SHALIMAR BAGH KV SHALIMAR BAGH

Page 2: 1 D Arrays in C++

WHAT IS AN ARRAYWHAT IS AN ARRAY

An array is a derived data type ( derived from fundamental data An array is a derived data type ( derived from fundamental data type )type )

It is a collection of variables of the same type that are referenced It is a collection of variables of the same type that are referenced by a common name. If the name of an array of 10 elements is by a common name. If the name of an array of 10 elements is ARY, then its elements will be referenced as :ARY, then its elements will be referenced as :

ARY[0], ARY[1], ARY[2], ………ARY[9]ARY[0], ARY[1], ARY[2], ………ARY[9]

Consist of contiguous memory locations.Consist of contiguous memory locations.

Lowest address corresponds to first elementLowest address corresponds to first element

Page 3: 1 D Arrays in C++

Highest address corresponds to the last element.Highest address corresponds to the last element.

Can have data items of type like: int, char, float and Can have data items of type like: int, char, float and also user-defined types like : structures, objects.also user-defined types like : structures, objects.

When upper bound and lower bound of an array is When upper bound and lower bound of an array is given, its size is calculated as :given, its size is calculated as :

Array size (length) = UB – LB +1 Array size (length) = UB – LB +1

Ex : -7, -6, -5, ….0, 1, 2, 3, 4….15Ex : -7, -6, -5, ….0, 1, 2, 3, 4….15

UB = 15 UB = 15 LB = -7LB = -7

=15-(-7) +1=15-(-7) +1=15+7+1=23=15+7+1=23

Page 4: 1 D Arrays in C++

NEEDNEED FORFOR AN ARRAYAN ARRAY

To store large number of variables of same type under a single To store large number of variables of same type under a single variable.variable.

Easy understanding of the program.Easy understanding of the program.

E.g.E.g.To store Marks of 50 students.To store Marks of 50 students.Record of sales of 100 salesman.Record of sales of 100 salesman.

Page 5: 1 D Arrays in C++

TYPES OF ARRAYSTYPES OF ARRAYS

One – dimensional arraysOne – dimensional arrays : Comprised of finite homogeneous : Comprised of finite homogeneous elements.elements.

Multi-dimensional arraysMulti-dimensional arrays : Comprised of elements, each of : Comprised of elements, each of which is itself an array.which is itself an array.

Page 6: 1 D Arrays in C++

One – Dimensional ArrayOne – Dimensional Array

Single Dimensional Array: Element specified by single Single Dimensional Array: Element specified by single subscriptsubscript

Syntax:Syntax:type array_name [ size ]type array_name [ size ]

Base type of array

Name of array

No. of elements that can be stored:

Can be a integer value without the sign

Page 7: 1 D Arrays in C++

Ex : MARKS [50]

..

..

..

0

1

2

3

49

MARKS [1]

MARKS [3]

Page 8: 1 D Arrays in C++

ARRAYARRAY INITIALIZATIONINITIALIZATION

int list [ 5 ] ; int list [ 5 ] ; // declaration// declaration

int list [ 5 ] = { 10, 20, 30, 40, 50 } ; int list [ 5 ] = { 10, 20, 30, 40, 50 } ; // declaration & initialization// declaration & initialization

Can skip the size of an array in array initializationCan skip the size of an array in array initialization

Elements of an array can be added or removed without Elements of an array can be added or removed without changing array dimensions.changing array dimensions.E.g.E.g.

float price [ ] = { 50.5, 63.97, 84.6, 779.8 };float price [ ] = { 50.5, 63.97, 84.6, 779.8 };

Page 9: 1 D Arrays in C++

Implementation of 1Dimensional Implementation of 1Dimensional Array in Memory Array in Memory

Starting address of the first element is calledStarting address of the first element is called Base address Base address

Address of element with subscript I = Base Address + ES ( I –L)Address of element with subscript I = Base Address + ES ( I –L)

ES = size of an array elementES = size of an array element

L = Lower bound of arrayL = Lower bound of array

Page 10: 1 D Arrays in C++

Ex : What will be the adresses of element Marks[-1] and Ex : What will be the adresses of element Marks[-1] and marks[2] of the following array with element size as 2 bytes marks[2] of the following array with element size as 2 bytes

and base address as 1000.and base address as 1000.

Marks

-3

-2

-1

0

1

2

3

1000

Page 11: 1 D Arrays in C++

SOL : SOL :

Address of Marks [I] = Base Address + ES ( I – L) Address of Marks [I] = Base Address + ES ( I – L)

L = -3L = -3

ES = 2ES = 2

Base Address Base Address = 1000= 1000

Address of Marks[-1]= 1000 + 2(-1-(-3))Address of Marks[-1]= 1000 + 2(-1-(-3))

= 1000+ 2(2)= 1004= 1000+ 2(2)= 1004

Address of Marks[2]= 1000 + 2(2-(-3))Address of Marks[2]= 1000 + 2(2-(-3))

= 1000+ 2(5)= 1010= 1000+ 2(5)= 1010

Page 12: 1 D Arrays in C++

Searching Searching

Linear Search or Sequential search Linear Search or Sequential search

Binary Search Binary Search

Basic Operation on 1 Dimensional Basic Operation on 1 Dimensional Arrays Arrays

Page 13: 1 D Arrays in C++

A A Sequential SearchSequential Search can be used to determine if a can be used to determine if a specific value (the specific value (the search keysearch key) is in an array.) is in an array.

Approach is to start with the first element and compare Approach is to start with the first element and compare each element to the search key:each element to the search key: If found, return the index of the element that contains If found, return the index of the element that contains

the search key.the search key. If not found, return -1.If not found, return -1.

Because -1 is not a valid index, this is a good return Because -1 is not a valid index, this is a good return value to indicate that the search key was not found value to indicate that the search key was not found in the array.in the array.

Linear Search or Sequential SearchLinear Search or Sequential Search

Page 14: 1 D Arrays in C++

Algorithm to perform a Sequential Algorithm to perform a Sequential SearchSearch

Step 1 : Set ctr = LStep 1 : Set ctr = L Step 2 : Repeat steps 3 through 4 until ctr> UStep 2 : Repeat steps 3 through 4 until ctr> U Step 3 : If AR[str] == ITEM then Step 3 : If AR[str] == ITEM then

{ print “ Search Successful”{ print “ Search Successful”

print ctr, “is the location of”, ITEMprint ctr, “is the location of”, ITEM

breakbreak

}} Step 4 : Ctr= ctr+1Step 4 : Ctr= ctr+1 Step 5 : If ctr > U then Step 5 : If ctr > U then

print “ Search Unsuccessful”print “ Search Unsuccessful” Step 6 : ENDStep 6 : END

Page 15: 1 D Arrays in C++

Binary Search Binary Search A A Binary SearchBinary Search is like the "Guess a Number" game. is like the "Guess a Number" game. To guess a number between 1 and 100, we start with 50 To guess a number between 1 and 100, we start with 50

(halfway between the beginning number and the end (halfway between the beginning number and the end number). number).

If we learn that the number is greater than 50, we If we learn that the number is greater than 50, we immediately know the number is not 1 - 49. immediately know the number is not 1 - 49.

If we learn that the number is less than 50, we immediately If we learn that the number is less than 50, we immediately know the number is not 51 - 100. know the number is not 51 - 100.

We keep guessing the number that is in the middle of the We keep guessing the number that is in the middle of the remaining numbers (eliminating half the remaining remaining numbers (eliminating half the remaining numbers) until we find the number.numbers) until we find the number.

Page 16: 1 D Arrays in C++

Algorithm to perform Binary SearchAlgorithm to perform Binary Search Step 1 : Set beg = L, last = UStep 1 : Set beg = L, last = U Step 2 : Repeat steps 3 through 6 until beg > lastStep 2 : Repeat steps 3 through 6 until beg > last Step 3 : mid = INT((beg+last)/2)Step 3 : mid = INT((beg+last)/2) Step 4 : If AR[mid] == ITEM then Step 4 : If AR[mid] == ITEM then

{ print “ Search Successful”{ print “ Search Successful”

print ITEM, “found at “,midprint ITEM, “found at “,mid

breakbreak

}} Step 5 : If AR[mid] < ITEM then Step 5 : If AR[mid] < ITEM then

beg = mid +1beg = mid +1 Step 6 : If AR[mid] > ITEM then Step 6 : If AR[mid] > ITEM then

last = mid -1last = mid -1 Step 7 :if beg != lastStep 7 :if beg != last

print “ Search Unsuccessful”print “ Search Unsuccessful” Step 8 : ENDStep 8 : END

Page 17: 1 D Arrays in C++

Example of a Binary SearchExample of a Binary Search

For example, we will search for the value 7 in this sorted For example, we will search for the value 7 in this sorted array:array:

To begin, we find the index of the center element, which is 8, To begin, we find the index of the center element, which is 8, and we compare our search key (7) with the value 45.and we compare our search key (7) with the value 45.

Page 18: 1 D Arrays in C++

Binary Search Example (con't)Binary Search Example (con't)

Because 7 is less than 45, we eliminate all array elements Because 7 is less than 45, we eliminate all array elements higher than our current middle element and consider elements higher than our current middle element and consider elements 0 through 7 the new subarray to search.0 through 7 the new subarray to search.

The index of the center element is now 3, so we compare 7 to The index of the center element is now 3, so we compare 7 to the value 8.the value 8.

Page 19: 1 D Arrays in C++

Binary Search Example (con't)Binary Search Example (con't)

Because 7 is less than 8, we eliminate all array elements Because 7 is less than 8, we eliminate all array elements higher than our current middle element (3) and make elements higher than our current middle element (3) and make elements 0 through 2 the new subarray to search.0 through 2 the new subarray to search.

The index of the center element is now 1, so we compare 7 to The index of the center element is now 1, so we compare 7 to the value 6.the value 6.

Page 20: 1 D Arrays in C++

Binary Search: Finding the search keyBinary Search: Finding the search key

Because 7 is greater than 6, we eliminate array elements lower Because 7 is greater than 6, we eliminate array elements lower than our current middle element (1) and make element 2 the than our current middle element (1) and make element 2 the new subarray to search.new subarray to search.

The value of element 2 matches the search key, so our search The value of element 2 matches the search key, so our search is successful and we return the index 2.is successful and we return the index 2.

Page 21: 1 D Arrays in C++

Binary Search Example 2Binary Search Example 2

This time, we search for a value not found in the array, 34. This time, we search for a value not found in the array, 34. Again, we start with the entire array and find the index of the Again, we start with the entire array and find the index of the middle element, which is 8. middle element, which is 8.

We compare our search key (34) with the value 45.We compare our search key (34) with the value 45.

Page 22: 1 D Arrays in C++

Binary Search Example 2 (con't)Binary Search Example 2 (con't)

Because 34 is less than 45, we eliminate array elements higher Because 34 is less than 45, we eliminate array elements higher than our current middle element and consider elements 0 than our current middle element and consider elements 0 through 7 the new subarray to search.through 7 the new subarray to search.

The index of the center element is now 3, so we compare 34 to The index of the center element is now 3, so we compare 34 to the value 8.the value 8.

Page 23: 1 D Arrays in C++

Binary Search Example 2 (con't)Binary Search Example 2 (con't)

Because 34 is greater than 8, we eliminate array elements Because 34 is greater than 8, we eliminate array elements lower than our current middle element and consider elements lower than our current middle element and consider elements 4 through 7 the new subarray to search.4 through 7 the new subarray to search.

The index of the center element is now 5, so we compare 34 to The index of the center element is now 5, so we compare 34 to the value 15.the value 15.

Page 24: 1 D Arrays in C++

Binary Search Example 2 (con't)Binary Search Example 2 (con't)

Again, we eliminate array elements lower than our current Again, we eliminate array elements lower than our current middle element and make elements 6 and 7 the new subarray middle element and make elements 6 and 7 the new subarray to search.to search.

The index of the center element is now 6, so we compare 34 to The index of the center element is now 6, so we compare 34 to the value 22.the value 22.

Page 25: 1 D Arrays in C++

Binary Search 2: search key is not Binary Search 2: search key is not foundfound

Next, we eliminate array elements lower than our current Next, we eliminate array elements lower than our current middle element and make element 7 the new subarray to middle element and make element 7 the new subarray to search.search.

We compare 34 to the value 36, and attempt to eliminate the We compare 34 to the value 36, and attempt to eliminate the higher subarray, which leaves an empty subarray.higher subarray, which leaves an empty subarray.

We have determined that 32 is not in the array. We return -1 to We have determined that 32 is not in the array. We return -1 to indicate an unsuccessful search.indicate an unsuccessful search.