ICE-2111
Md. Zihadur Rahman
1
Array
2
Introduction
• An array is a sequence of objects all of which have the
same type.
• The objects are called the elements of the array and are
numbered consecutively 0, 1, 2, 3, ... . These numbers are
called index values or subscripts of the array.
3
PROCESSING ARRAYS
• Using Direct Access on Arrays:
4
• Printing a Reverse in Order:
PROCESSING ARRAYS
5
• Initializing an Array
• The first line declares a to be the array of 3 elements. The second line
uses the sizeof() function to compute the actual number of elements in
the array.
• The value of sizeof(float) is 4 because on this machine a float value
occupies 4 bytes in memory. The value of sizeof(a) is 12 because the
complete array occupies 12 bytes in memory. Therefore, the value of
size is computed to be 12/4 = 3.
6
float a[7] = { 55.5,66.6, 77.7 };
• This array is declared to have 7 elements of type float;
then its initializer list initializes the first 3 elements with
the given values and the remaining 4 elements with the
value 0.
PROCESSING ARRAYS
7
• Initializing an Array with Trailing Zeros:
8
• Allowing an Array Index to Exceed its Bounds
The last three values printed are garbage values.
9
• Passing an Array to a Function that Returns its Sum
10
• Explanation:
• When an array is passed to a function, as in the call
sum(a,size) in previous example, the value of array name
a is actually the memory address of the first element
(a[0]) in the array.
• The function uses that address value to access and
possibly modify the contents of the array. So passing an
array to a function is similar to passing a variable by
reference: the function can change the values of the
array’s elements.
11
• Printing the Memory Location of an Array
• This program prints the value of the address stored in an
array name.
• Its value is the memory address of the first byte of the
first element a[0] in the array.
12
THE LINEAR SEARCH ALGORITHM
13
THE BUBBLE SORT ALGORITHM
14
THE BINARY SEARCH ALGORITHM
15
• Here is a trace of the call index (44 , a ,7).
16
• Determining whether an Array is Sorted:
17
MULTIDIMENSIONAL ARRAYS
• Reading and Printing a Two-Dimensional Array
18
Thank you
19

4. Array.pptx

  • 1.
  • 2.
  • 3.
    Introduction • An arrayis a sequence of objects all of which have the same type. • The objects are called the elements of the array and are numbered consecutively 0, 1, 2, 3, ... . These numbers are called index values or subscripts of the array. 3
  • 4.
    PROCESSING ARRAYS • UsingDirect Access on Arrays: 4
  • 5.
    • Printing aReverse in Order: PROCESSING ARRAYS 5
  • 6.
    • Initializing anArray • The first line declares a to be the array of 3 elements. The second line uses the sizeof() function to compute the actual number of elements in the array. • The value of sizeof(float) is 4 because on this machine a float value occupies 4 bytes in memory. The value of sizeof(a) is 12 because the complete array occupies 12 bytes in memory. Therefore, the value of size is computed to be 12/4 = 3. 6
  • 7.
    float a[7] ={ 55.5,66.6, 77.7 }; • This array is declared to have 7 elements of type float; then its initializer list initializes the first 3 elements with the given values and the remaining 4 elements with the value 0. PROCESSING ARRAYS 7
  • 8.
    • Initializing anArray with Trailing Zeros: 8
  • 9.
    • Allowing anArray Index to Exceed its Bounds The last three values printed are garbage values. 9
  • 10.
    • Passing anArray to a Function that Returns its Sum 10
  • 11.
    • Explanation: • Whenan array is passed to a function, as in the call sum(a,size) in previous example, the value of array name a is actually the memory address of the first element (a[0]) in the array. • The function uses that address value to access and possibly modify the contents of the array. So passing an array to a function is similar to passing a variable by reference: the function can change the values of the array’s elements. 11
  • 12.
    • Printing theMemory Location of an Array • This program prints the value of the address stored in an array name. • Its value is the memory address of the first byte of the first element a[0] in the array. 12
  • 13.
    THE LINEAR SEARCHALGORITHM 13
  • 14.
    THE BUBBLE SORTALGORITHM 14
  • 15.
    THE BINARY SEARCHALGORITHM 15
  • 16.
    • Here isa trace of the call index (44 , a ,7). 16
  • 17.
    • Determining whetheran Array is Sorted: 17
  • 18.
    MULTIDIMENSIONAL ARRAYS • Readingand Printing a Two-Dimensional Array 18
  • 19.