Upcoming SlideShare
×

# 1 D Arrays in C++

9,697 views

Published on

Published in: Education, Technology, Design
4 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
9,697
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
289
0
Likes
4
Embeds 0
No embeds

No notes for slide

### 1 D Arrays in C++

1. 1. 1D ARRAYS POONAM WALIA KV SHALIMAR BAGH
2. 2. WHAT IS AN ARRAY <ul><li>An array is a derived data type ( derived from fundamental data type ) </li></ul><ul><li>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 ARY, then its elements will be referenced as : </li></ul><ul><li>ARY[0], ARY[1], ARY[2], ………ARY[9] </li></ul><ul><li>Consist of contiguous memory locations. </li></ul><ul><li>Lowest address corresponds to first element </li></ul>
3. 3. <ul><li>Highest address corresponds to the last element. </li></ul><ul><li>Can have data items of type like: int, char, float and also user-defined types like : structures, objects. </li></ul><ul><li>When upper bound and lower bound of an array is given, its size is calculated as : </li></ul><ul><li>Array size (length) = UB – LB +1 </li></ul><ul><li>Ex : -7, -6, -5, ….0, 1, 2, 3, 4….15 </li></ul><ul><li>UB = 15 LB = -7 </li></ul><ul><li>=15-(-7) +1 </li></ul><ul><li>=15+7+1=23 </li></ul>
4. 4. NEED FOR AN ARRAY <ul><li>To store large number of variables of same type under a single variable. </li></ul><ul><li>Easy understanding of the program. </li></ul><ul><li>E.g. To store Marks of 50 students. Record of sales of 100 salesman. </li></ul>
5. 5. TYPES OF ARRAYS <ul><li>One – dimensional arrays : Comprised of finite homogeneous elements. </li></ul><ul><li>Multi-dimensional arrays : Comprised of elements, each of which is itself an array. </li></ul>
6. 6. One – Dimensional Array <ul><li>Single Dimensional Array: Element specified by single subscript </li></ul><ul><li>Syntax: type array_name [ size ] </li></ul>Base type of array Name of array No. of elements that can be stored: Can be a integer value without the sign
7. 7. Ex : MARKS [50] 0 1 2 3 49 MARKS [1] MARKS [3] . . .
8. 8. ARRAY INITIALIZATION <ul><li>int list [ 5 ] ; // declaration </li></ul><ul><li>int list [ 5 ] = { 10, 20, 30, 40, 50 } ; // declaration & initialization </li></ul><ul><li>Can skip the size of an array in array initialization </li></ul><ul><li>Elements of an array can be added or removed without changing array dimensions. E.g. </li></ul><ul><li>float price [ ] = { 50.5, 63.97, 84.6, 779.8 }; </li></ul>
9. 9. Implementation of 1Dimensional Array in Memory <ul><li>Starting address of the first element is called Base address </li></ul><ul><li>Address of element with subscript I = Base Address + ES ( I –L) </li></ul><ul><li>ES = size of an array element </li></ul><ul><li>L = Lower bound of array </li></ul>
10. 10. Ex : What will be the adresses of element Marks[-1] and marks[2] of the following array with element size as 2 bytes and base address as 1000. Marks -3 -2 -1 0 1 2 3 1000
11. 11. SOL : Address of Marks [I] = Base Address + ES ( I – L) L = -3 ES = 2 Base Address = 1000 Address of Marks[-1]= 1000 + 2(-1-(-3)) = 1000+ 2(2)= 1004 Address of Marks[2]= 1000 + 2(2-(-3)) = 1000+ 2(5)= 1010
12. 12. <ul><li>Searching </li></ul><ul><li>Linear Search or Sequential search </li></ul><ul><li>Binary Search </li></ul>Basic Operation on 1 Dimensional Arrays
13. 13. <ul><li>A Sequential Search can be used to determine if a specific value (the search key ) is in an array. </li></ul><ul><li>Approach is to start with the first element and compare each element to the search key: </li></ul><ul><ul><li>If found, return the index of the element that contains the search key. </li></ul></ul><ul><ul><li>If not found, return -1. </li></ul></ul><ul><ul><ul><li>Because -1 is not a valid index, this is a good return value to indicate that the search key was not found in the array. </li></ul></ul></ul>Linear Search or Sequential Search
14. 14. Algorithm to perform a Sequential Search <ul><li>Step 1 : Set ctr = L </li></ul><ul><li>Step 2 : Repeat steps 3 through 4 until ctr> U </li></ul><ul><li>Step 3 : If AR[str] == ITEM then </li></ul><ul><li>{ print “ Search Successful” </li></ul><ul><li> print ctr, “is the location of”, ITEM </li></ul><ul><li> break </li></ul><ul><li>} </li></ul><ul><li>Step 4 : Ctr= ctr+1 </li></ul><ul><li>Step 5 : If ctr > U then </li></ul><ul><li>print “ Search Unsuccessful” </li></ul><ul><li>Step 6 : END </li></ul>
15. 15. Binary Search <ul><li>A Binary Search is like the &quot;Guess a Number&quot; game. </li></ul><ul><li>To guess a number between 1 and 100, we start with 50 (halfway between the beginning number and the end number). </li></ul><ul><li>If we learn that the number is greater than 50, we immediately know the number is not 1 - 49. </li></ul><ul><li>If we learn that the number is less than 50, we immediately know the number is not 51 - 100. </li></ul><ul><li>We keep guessing the number that is in the middle of the remaining numbers (eliminating half the remaining numbers) until we find the number. </li></ul>
16. 16. Algorithm to perform Binary Search <ul><li>Step 1 : Set beg = L, last = U </li></ul><ul><li>Step 2 : Repeat steps 3 through 6 until beg > last </li></ul><ul><li>Step 3 : mid = INT((beg+last)/2) </li></ul><ul><li>Step 4 : If AR[mid] == ITEM then </li></ul><ul><li>{ print “ Search Successful” </li></ul><ul><li> print ITEM, “found at “,mid </li></ul><ul><li> break </li></ul><ul><li>} </li></ul><ul><li>Step 5 : If AR[mid] < ITEM then </li></ul><ul><ul><li>beg = mid +1 </li></ul></ul><ul><li>Step 6 : If AR[mid] > ITEM then </li></ul><ul><li>last = mid -1 </li></ul><ul><li>Step 7 :if beg != last </li></ul><ul><li>print “ Search Unsuccessful” </li></ul><ul><li>Step 8 : END </li></ul>
17. 17. Example of a Binary Search <ul><li>For example, we will search for the value 7 in this sorted array: </li></ul><ul><li>To begin, we find the index of the center element, which is 8, and we compare our search key (7) with the value 45. </li></ul>
18. 18. Binary Search Example (con't) <ul><li>Because 7 is less than 45, we eliminate all array elements higher than our current middle element and consider elements 0 through 7 the new subarray to search. </li></ul><ul><li>The index of the center element is now 3, so we compare 7 to the value 8. </li></ul>
19. 19. Binary Search Example (con't) <ul><li>Because 7 is less than 8, we eliminate all array elements higher than our current middle element (3) and make elements 0 through 2 the new subarray to search. </li></ul><ul><li>The index of the center element is now 1, so we compare 7 to the value 6. </li></ul>
20. 20. Binary Search: Finding the search key <ul><li>Because 7 is greater than 6, we eliminate array elements lower than our current middle element (1) and make element 2 the new subarray to search. </li></ul><ul><li>The value of element 2 matches the search key, so our search is successful and we return the index 2. </li></ul>
21. 21. Binary Search Example 2 <ul><li>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 middle element, which is 8. </li></ul><ul><li>We compare our search key (34) with the value 45. </li></ul>
22. 22. Binary Search Example 2 (con't) <ul><li>Because 34 is less than 45, we eliminate array elements higher than our current middle element and consider elements 0 through 7 the new subarray to search. </li></ul><ul><li>The index of the center element is now 3, so we compare 34 to the value 8. </li></ul>
23. 23. Binary Search Example 2 (con't) <ul><li>Because 34 is greater than 8, we eliminate array elements lower than our current middle element and consider elements 4 through 7 the new subarray to search. </li></ul><ul><li>The index of the center element is now 5, so we compare 34 to the value 15. </li></ul>
24. 24. Binary Search Example 2 (con't) <ul><li>Again, we eliminate array elements lower than our current middle element and make elements 6 and 7 the new subarray to search. </li></ul><ul><li>The index of the center element is now 6, so we compare 34 to the value 22. </li></ul>
25. 25. Binary Search 2: search key is not found <ul><li>Next, we eliminate array elements lower than our current middle element and make element 7 the new subarray to search. </li></ul><ul><li>We compare 34 to the value 36, and attempt to eliminate the higher subarray, which leaves an empty subarray. </li></ul><ul><li>We have determined that 32 is not in the array. We return -1 to indicate an unsuccessful search. </li></ul>