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

1 D Arrays in C++

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