2. ∗ Introduction to Arrays
∗ One-Dimensional Arrays
∗ Declaration of One-Dimensional Arrays
∗ Initialization of One-Dimensional Arrays
Arrays
3. ∗ Basic data types are
constrained by the fact that only one value can be stored.
eg; int x=2;
∗ If you need a large of volume data to be handled , array is
essential.
∗ An array is a fixed-size sequenced collection of elements of
the same data type.
∗ Types of arrays:
1- one-dimensional array
2- two-dimensional array
3- multidimensional array
Introduction to Arrays
4. ∗ General form of declaration :
type variable-name[size];
e.g.
int x[10];
Note:
∗ – type can be any type (int, float, char, …)
∗ – array name is an identifier
∗
Declaration of
One-Dimensional Arrays
6. The value to the array elements can be assigned as follows
x[0] = 95 ;
x[1] = 93 ;
x[2] = 94 ;
x[3] = 96 ;
x[4] = 97 ;
x[5] = 92 ;
x[6] = 91 ;
x[7] = 100 ;
x[8] = 99 ;
x[9] = 98 ;
7. A list of items can be given one variable name using only one
subscript such variable is called One-dimentional array.
int x[10];
8. ∗ Elements in an array must be initialized after it is declared,
otherwise they will contain “garbage”.
∗ An array can be initialized with two ways:
∗ 1- At compile time (when writing the code)
∗ 2- At run time ( excution time )
Initialization of One-
Dimensional Arrays
9. ∗ Compile Time Initialization
∗ General form:
type array-name[size] = {list of values};
e.g.
int x[10]={6,5,9,3,2,3,1,8,6,4};
int number[] = {9,3,2};
float mark[5] = {2.3,3.04,4.5,56.0,6};
char name[] = {‘J’,’A’,’C’,’K’}; or “JACK”
int number[5] = {1,2}; // remain 0
char name[10] = {‘J’,’A’,’C’,’K’} ; // remain null
int array[3] = {1,2,3,4} ; // illegal
Initialization of One-Dimensional
Arrays