02_Arrays
02_Arrays
TO
ARRAYS
Arrays
A collection of same type elements under the
same variable identifier(variable name)
referenced by index number.
IN SHORT, ARRAY IS
–Structures of related data items
–Static entity – same size throughout program
Types of Arrays
1.SingleDimensional
2.Multidimensional
Syntax of Array Declaration (Single
Dimensional)
Data_type array_name[size];
Where:
data_type – valid data type
array_name – any user-defined identifier
size – number of elements in the array
Example: int num[5];
To refer to an array element:
Element/s
–Item/s of the array
–The variables of the array
Specify
–Array name
–Position number/index number/subscript
Index Number
–The ‘ID’ of the array element
–Also called Position number or Subscript
–Starts from 0 to size-1
–Ex: if size = 5, index numbers are 0 – 4
Refer to an element in the Ar
arrayname[index number]
–First element at position 0 (must always be put in
mind)
c[ 0 ], c[ 1 ]...c[ n – 1 ]
Example
Consider an integer array num with size 5:
int num[5];
It will have five integer elements from num[0] to num[4].
elements value(int)
First Element → num[0] Subscript/Index number
num[1] – the position number
num[2] contained within square
brackets.
num[3] - must be an integer or
an integer expression.
Last Element → num[4]
Array Manipulation
You can use loops to manipulate each element of the array.
Note: When you declare an array, each elements are NOT initialized to
zero, but is instead given a trash value by your compiler.
int num[5];
}
num[0] 342332
num[4] 123
Initialization of Arrays
We can initialize the elements in the array in the same way
as the ordinary variables when they are declared.
Note: The values in the list care separated by commas, for example
the statement
Initialization of Arrays
int number[3]={0,0,0};
This will declare the array size as an array of size 3 and will
assign zero to each element.
Initialization of Arrays
What you should remember:
1. If the number of values in the list is less than the number of
elements, then only that many elements are initialized. The
remaining elements will be set to zero automatically.
Ex: int num[5] = {1};
}
num[0] 1
num[1] 0 num[0] is initialized to 1, while
the rest are initialized to 0.
num[2] 0
num[3] 0
num[4] 0
Initialization of Arrays
Therefore: int num[3] = {0, 0, 0}; can also be written as:
int num[3] = {0}; //this initialize all elements to 0.
num[0] 0
num[1] 0
num[2] 0
Initialization of Array Elements
More examples:
int num[5] = {1,2}; int num[5] = {1, 1, 2, 2};
num[0] 1 num[0] 1
num[1] 2 num[1] 1
num[2] 0 num[2] 2
num[3] 0 num[3] 2
num[4] 0 num[4] 0
Initialization of Arrays
What you should remember:
2. If too many elements are initializers:
Example:
int num[3] = {0, 0, 0, 0};
Will declare the array to contain four elements (size = 4) with initial
values 1. this approach works fine as long as
Initialization of Array Elements: Using loops
int n [ 10 ] , i ; //declare an array n with size 10.
for (i = 0; i < 10; i ++) //index i starts with 0 and ends with 9.
{
n [ i ] = 0 ; //n[0] = 0; n[1] = 0; n[2]=0; and so on…
}
c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
Snippet above declared two integer arrays arr1 of size 100 and
arr2 of size 27.
Example
This will cause an error because n does not have a value yet. Meaning it
can be any random value set by the compiler. Including hexadecimal
trash values.
Sorting Arrays
Sorting data – placing the data into a particular order such as ascending
or descending.
Example:
Write a program that sorts the values in the elements of the 5-
element array num into ascending order.
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [0] with the rest of the elements in the array:
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [1] with the rest of the elements in the array except index [0]:
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [2] with the rest of the elements in the array except index [0] & [1]:
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [3] with last element at index [4]: