Class Notes (1,100,000)
CA (650,000)
Western (60,000)
ENGSCI (100)
Lecture 6

Engineering Science 1036A/B Lecture Notes - Lecture 6: Array Data Structure, Null Character, Code Segment


Department
Engineering Science
Course Code
ENGSCI 1036A/B
Professor
Quazi Rahman
Lecture
6

This preview shows pages 1-3. to view the full 13 pages of the document.
Arrays
What is an Array?
An array is a set of consecutive memory locations used to store
same types of data
Each items in an array is called an element
The number of elements in an array is called the dimension or
the size of the array
An array shares a common identifier and data type:
double myArray[10]; [dimesion]
Array Declaration:
Syntax:
dataType arrayIdentifier [arraysize];
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Array Declaration Cont. :
C++ requires that the array size must be a constant expression.
This is called static array declaration. For example, the following
is illegal:
Individual elements of an array are specified using offsets
referred to as index or subscripts
The index of the first element of an array always has a subscript
value zero, e.g myArray [0] holds the first element of the array
myArray[10] (as an example)
The index of the last element of the array always has a subscript
value of [dimension – 1]. For the array declared “int
myArray[10]” the last element goes to myArray[9]
Array Arithmetics:
Each element in the array is represented using the following
syntax, known as an indexed variable: arrayName[index];
After an array is created, an indexed variable can be used in the
same way as a regular variable. Index can be an integer
expression
For example: the following code segment adds the values of
myArray[0] and myArray[1] and assign the new value to
myArray[2]
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Array Example:
What if we need to read 100 different integers in display them in
reverse order?
Why is i < 100 and i = 99?
Because since the size of the array is 100, the index of the last
element is, element 100, is written as myArray [99] or
myArray[size-1] because the first element, element #1 is given
by myArray [0].
Boundaries:
C++ does not check array boundary’s
So, accessing array elements using subscripts outside the
declared boundary does not cause syntax errors
E.g Array declaration double myArray[10]; sets the boundary
index from 0 to 9 and in this case, trying to access myArray[-1]
and myArray[10] will not cause any syntax error but it will most
likely result in a run time error or a logical error
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version