MODULE 3
Arrays
INTRODUCTION
Arrays: Array is a sequential collection of similar data items.
Pictorial representation of an array of 5 integers
10 20 30 40 50
A[0] A[1] A[2] A[3] A[4]
An array is a collection of similar data items.
All the elements of the array share a common name .
Each element in the array can be accessed by the subscript(or index) and array name.
The arrays are classified as:
1. Single dimensional array
2. Multidimensional array.
Single Dimensional Array.
A single dimensional array is a linear list of related data items of same data type.
In memory, all the data items are stored in contiguous memory locations.
Declaration of one-dimensional array(Single dimensional array)
Syntax:
datatype array_name[size];
datatype can be int,float,char,double.
array_name is the name of the array and it should be an valid identifier.
Size is the total number of elements in array.
For example:
int a[5];
The above statement allocates 5*2=10 Bytes of memory for the array a.
a[0] a[1] a[2] a[3] a[4]
float b[5];
, The above statement allocatests 5*4=20 Bytes of memory for the array b.
Each element in the array is identified using integer number called as index.
If n is the size of array, the array index starts from 0 and ends at n-1.
Storing Values in Arrays
Declaration of arrays only allocates memory space for array. But array elements are not initialized
and hence values has to be stored.
Therefore to store the values in array, there are 3 methods
1. Initialization
2. Assigning Values
3. Input values from keyboard through scanf()
Initialization of one-dimensional array
Assigning the required values to an array elements before processing is called initialization.
data type array_name[expression]={v1,v2,v3…,vn};
Where
datatype can be char,int,float,double
array name is the valid identifier
size is the number of elements in array
v1,v2,v3…......vn are values to be assigned.
Arrays can be initialized at declaration time.
Example:
int a[5]={2,4,34,3,4};
2 4 34 3 4
a[0] a[1] a[2] a[3] a[4]
The various ways of initializing arrays are as follows:
1. Initializing all elements of array(Complete array initialization)
2. Partial array initialization
, 3. Initialization without size
4. String initialization
1. Initializing all elements of array:
Arrays can be initialized at the time of declaration when their initial values are known in advance.
In this type of array initialization, initialize all the elements of specified memory size.
Example:
int a[5]={10,20,30,40,50};
10 20 30 40 50
2. Partial array initialization
If the number of values to be initialized is less than the size of array then it is called as partial
array initialization.
In such a case elements are initialized in the order from 0th element.
The remaining elements will be initialized to zero automatically by the compiler.
Example:
int a[5]={10,20};
10 20 0 0 0
3. Initialization without size
In the declaration the array size will be set to the total number of initial values specified.
The compiler will set the size based on the number of initial values.
Example:
int a[ ]={10,20,30,40,50};
In the above example the size of an array is set to 5
4. String Initialization
Sequence of characters enclosed within double quotes is called as string.
The string always ends with NULL character(\0)
char s[5]=”SVIT”; We can observe that string length is 4,but size
is 5 because to store NULL character we need
one more location.
So pictorial representation of an array s is as follows:
, S V I T \0
S[0] S[1] S[2] S[3] S[4]
3.1.2 Assigning values to arrays
Using assignment operators, we can assign values to individual elements of arrays.
For example:
int a[3];
a[0]=10;
a[1]=20;
a[2]=30;
10 20 30
a[0] a[1] a[2]
Reading and writing single dimensional arrays.
To read array elements from keyboard we can use scanf() function as follows:
To read 0th element: scanf(“%d”,&a[0]);
To read 1st element: scanf(“%d”,&a[1]);
To read 2nd element: scanf(“%d”,&a[2]);
……
…….
To read n element : scanf(“%d”,&a[n-1]);
th
In general
To read ith element:
scanf(“%d”,&a[i]); where i=0; i<n; i++
To print array elements we can use printf() function as follows:
To print 0th element: printf(“%d”,a[0]);
To print 1st element: printf(“%d”,a[1]);
To print 2nd element :printf(“%d”,a[2]);
……..
……..
To nth element : printf(“%d”,&a[n-1]);
In general
To read ith element:
printf(“%d”,a[i]); where i=0; i<n;
Arrays
INTRODUCTION
Arrays: Array is a sequential collection of similar data items.
Pictorial representation of an array of 5 integers
10 20 30 40 50
A[0] A[1] A[2] A[3] A[4]
An array is a collection of similar data items.
All the elements of the array share a common name .
Each element in the array can be accessed by the subscript(or index) and array name.
The arrays are classified as:
1. Single dimensional array
2. Multidimensional array.
Single Dimensional Array.
A single dimensional array is a linear list of related data items of same data type.
In memory, all the data items are stored in contiguous memory locations.
Declaration of one-dimensional array(Single dimensional array)
Syntax:
datatype array_name[size];
datatype can be int,float,char,double.
array_name is the name of the array and it should be an valid identifier.
Size is the total number of elements in array.
For example:
int a[5];
The above statement allocates 5*2=10 Bytes of memory for the array a.
a[0] a[1] a[2] a[3] a[4]
float b[5];
, The above statement allocatests 5*4=20 Bytes of memory for the array b.
Each element in the array is identified using integer number called as index.
If n is the size of array, the array index starts from 0 and ends at n-1.
Storing Values in Arrays
Declaration of arrays only allocates memory space for array. But array elements are not initialized
and hence values has to be stored.
Therefore to store the values in array, there are 3 methods
1. Initialization
2. Assigning Values
3. Input values from keyboard through scanf()
Initialization of one-dimensional array
Assigning the required values to an array elements before processing is called initialization.
data type array_name[expression]={v1,v2,v3…,vn};
Where
datatype can be char,int,float,double
array name is the valid identifier
size is the number of elements in array
v1,v2,v3…......vn are values to be assigned.
Arrays can be initialized at declaration time.
Example:
int a[5]={2,4,34,3,4};
2 4 34 3 4
a[0] a[1] a[2] a[3] a[4]
The various ways of initializing arrays are as follows:
1. Initializing all elements of array(Complete array initialization)
2. Partial array initialization
, 3. Initialization without size
4. String initialization
1. Initializing all elements of array:
Arrays can be initialized at the time of declaration when their initial values are known in advance.
In this type of array initialization, initialize all the elements of specified memory size.
Example:
int a[5]={10,20,30,40,50};
10 20 30 40 50
2. Partial array initialization
If the number of values to be initialized is less than the size of array then it is called as partial
array initialization.
In such a case elements are initialized in the order from 0th element.
The remaining elements will be initialized to zero automatically by the compiler.
Example:
int a[5]={10,20};
10 20 0 0 0
3. Initialization without size
In the declaration the array size will be set to the total number of initial values specified.
The compiler will set the size based on the number of initial values.
Example:
int a[ ]={10,20,30,40,50};
In the above example the size of an array is set to 5
4. String Initialization
Sequence of characters enclosed within double quotes is called as string.
The string always ends with NULL character(\0)
char s[5]=”SVIT”; We can observe that string length is 4,but size
is 5 because to store NULL character we need
one more location.
So pictorial representation of an array s is as follows:
, S V I T \0
S[0] S[1] S[2] S[3] S[4]
3.1.2 Assigning values to arrays
Using assignment operators, we can assign values to individual elements of arrays.
For example:
int a[3];
a[0]=10;
a[1]=20;
a[2]=30;
10 20 30
a[0] a[1] a[2]
Reading and writing single dimensional arrays.
To read array elements from keyboard we can use scanf() function as follows:
To read 0th element: scanf(“%d”,&a[0]);
To read 1st element: scanf(“%d”,&a[1]);
To read 2nd element: scanf(“%d”,&a[2]);
……
…….
To read n element : scanf(“%d”,&a[n-1]);
th
In general
To read ith element:
scanf(“%d”,&a[i]); where i=0; i<n; i++
To print array elements we can use printf() function as follows:
To print 0th element: printf(“%d”,a[0]);
To print 1st element: printf(“%d”,a[1]);
To print 2nd element :printf(“%d”,a[2]);
……..
……..
To nth element : printf(“%d”,&a[n-1]);
In general
To read ith element:
printf(“%d”,a[i]); where i=0; i<n;