Introduction to Programming
Lecture 5 : Arrays (Single-Dimension)
Askar Khaimuldin
Senior-lecturer
[email protected]
Content
• Preface
• Declaration and initialization
• sizeof operator
• The array name
• Conjunction of loops and arrays
• Null-Terminated Strings (char array)
• Sorting (Bubble sort)
Preface
• Array is a data structure of related data items
• Static entity (same size throughout program)
• A set of variables of the same type
• Each element is referred to through a common name
• Specific element is accessed using index
An ounce of practice is worth more than tons of preaching
Declaration and initialization
• The general form for a single-dimension array
declaration
• Arrays must be explicitly declared providing a
constant size
• *Constants are initialized in declaration and
never changed
• An element is accessed by indexing the array
name
• N-element array “c”
• c[ 0 ], c[ 1 ] … c[ n - 1 ]
• Nth element is at position n-1
• Starts from 0 and ends with n-1 (anything else is out
of range)
Declaration and initialization
• Array elements like other variables
• Assignment, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
• Can perform operations inside subscript
• c[ 5 – 2 ] same as c[3]
• By default, local variables store “garbage”
values, whereas global variables are initialized to
zero
• One can initialize an array during its declaration
by providing a list of initializers
• In case an initializer list is empty all elements are
initialized to 0
sizeof operator
• An operator sizeof is used to retrieve an actual size of any type in bytes.
Example:
• sizeof(short) // this would output “2”
• If one creates an array “int a[5]”, the “sizeof(a)” will be 20 (assuming int
is 4 byte)
• It is also possible to do the same using “sizeof(int[5])”
The array name
• The name of the array denotes the address of
the 0’s element
• The set of data is stored in contiguous memory
locations in index order
• Example table of memory addresses reserved
for each element
Element [0] Element [1] Element [2] Element [3] … Element [99]
0x0097F5B0 0x0097F5B4 0x0097F5B8 0x0097F5BC 0x0097F73C
• Since int is 4 byte, the memory address is
increasing by 4 for every next element
Conjunction of loops and arrays
• Since an access to a particular element is gained
using an index, one can use a control variable as
index of an array
In this example the variable “i” is used
to iterate through the array
The first loop is started from 2 in order
not to get out of range in [i-2] and [i-1]
A code is filling the array with N
Fibonacci numbers
Null-Terminated Strings (char array)
• Using a character array can be simplified using
C-style string
• The C-style string is usual char array that ends
with null terminator (i.e. ‘\0’, 0, null)
word[0] word[1] word[2] word[3] word[4] word[5]
‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’
• One can use input and output streams to work
with entire character array
• Null terminator stops processing further data
Null-Terminated Strings (char array)
• The C++ has a library for C-style string <cstring> which is helpful for
doing standard manipulations (copy, compare, search, etc.)
• The null terminator is placed at the end of any string by default in
several cases:
• using input stream (cin >> charArrayName;)
• using double quotes (char charArrayName[] = “Hello”;)
• using initializer list (char charArrayName[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’})
• In this case other elements are initialized to 0 (which is ‘\0’ or null in ASCII)
• One can place ‘\0’ anywhere inside an array
Sorting (Bubble sort)
• Bubble Sort is the simplest sorting
algorithm
• Several passes through the array
• Successive pairs of elements are
compared
• Repeatedly swaps the adjacent
elements if they are in wrong
order
• At each i`th iteration of the outer
loop the maximum (can be
minimum) element is moved to
the position of n-i-1 Input:
Output:
Bubble sort (Example)
• Go left to right, and exchange elements as necessary
• One pass for each element
• Original: 3 4 2 7 6
• Pass 1: 3 2 4 6 7 (elements exchanged)
• Pass 2: 2 3 4 6 7
• Pass 3: 2 3 4 6 7 (no changes needed)
• Pass 4: 2 3 4 6 7
• Pass 5: 2 3 4 6 7
• Small elements "bubble" to the top (like 2 in this example)
Swap
• Swapping variables
• int x = 3, y = 4;
• y = x;
• x = y;
• What happened?
• Both x and y are 3!
• Need a temporary variable
• Solution
• int x = 3, y = 4, temp = 0;
• temp = x; // temp gets 3
• x = y; // x gets 4
• y = temp; // y gets 3
Literature
• Herbert Schildt. 2003. The Complete Reference C++, 4th
edition (Chapter 4)
Good luck