Arrays

Cards (25)

  • An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table.
  • Arrays are data type that is used to represent a large number of homogenous values.
  • Loop is a powerful set of programming constructs that makes array processing easy.
  • We can use loops to read and write the elements in an array. We can use loops to add, subtract, multiply, and divide the elements. We can also use loops for more complex processing such as calculating averages.
  • An array must be declared and defined before it can be used. Declaration and definition tell the compiler the name of the array, the type of each element, and the size or number of elements in the array. The size of the array is a constant and must have a value at compilation time.
  • The General Form – One-dimensional array
    storage class data-type array[expression];

    where storage class refers to storage class of the array, data-type is the data type, array is the array name and expression is positive-valued integer expression that indicates the number of array elements. Storage class is optional.
  • C uses an index to access individual elements in an array. The index must be an integral value or an expression that evaluates to an integral value. The simplest form for accessing an element is a numeric constant.
  • The address of any element in the array using the following simple formula:
    element address = array address + (sizeof(element) * index
  • Initialization of all elements in an array can be done at the time of declaration and definition, just as with variables. For each element in the array we provide a value. The only difference is that the values must be enclosed in braces and, if there are more than one, separated by commas.
  • The Initialization general form is:
    Storage class data-type arrayname[expression]={value1, value2,..value n};
  • The most appropriate loop is the for because the number of element are fixed and known.
  • Multidimensional arrays are defined in much the same manner as one-dimensional.
    A two-dimensional array requires two pairs of square brackets.
  • In general term, a multidimensional array definition can be written as:

    storage-class data-type arrayname[expression1][expression2];
  • On the other hand, the number of values within each part of braces cannot exceed the defined row size
  • Searching - This is the process of finding a certain data item from a given list of values.
  • The searching process is considered successful if the specified data item is successfully found during the searching process in C++ otherwise, it is not successful.
    The search operation comes to an end or is terminated when the specified item is found.
  • Sequential search - is also known as linear search or serial.
  • The Sequential search method is a very simple and straightforward technique to search a specified data item in an unordered list
  • The sequential search is slow and is used for an only a small list of data. This method is not recommended for a large amount of data because some more efficient method is available for large and complex search.
  • Binary Search – It is a type of search algorithm that works on sorted arrays. The binary search algorithm divides the array into two halves at every step until the target element is found.
  • Binary search- is a searching algorithm for finding an element's position in a sorted array.
  • Binary search -In this approach, the element is always searched in the middle of a portion of an array.
  • Binary Search can be implemented only on a sorted list of items . If the elements are not sorted already, we need to sort them first.
  • Binary Search Algorithm can be implemented in two ways:
    1. Iterative Method
    2. Recursive Method
  • The recursive method follows the divide and conquer approach .