(ARRAY)
Array
An array is a collection of similar data items stored at contiguous
memory locations.
It is simply a group of data types.
An array is a derived data type.
An array is used to represent a list of numbers , or a list of
names.
Elements can be accessed randomly using indices of an array.
They can be used to store collection of primitive data types such
as int, float, double, char, etc. of any particular type.
Examples
List of employee in an organization.
List of product and their cost.
List of customer.
Declaration of an Array
Syntax:
datatype array_name[size];
For example: int arr[6];
Here, we declared an array, arr, of integer type. And its
size is 6. Meaning, it can hold 6 integer values.
It's important to note that the size and
type of an array cannot be changed once
it is declared.
Types of arrays
Arrays are classified into two types:-
1. Single Dimensional Array / One Dimensional Array
2. Multi Dimensional Array
One-dimensional arrays(1-D)
A variable which represent the list of items using only one
index (subscript) is called one-dimensional array.
Single dimensional arrays are used to store a row of values.
In single dimensional array, data is stored in linear form.
Declaration Syntax:
datatype arrayName [ size ] ;
For Example , if we want to represent a set of five numbers
say(35,40,20,57,19), by an array variable number, then number is
declared as follows:
int number [5] ;
Continue..
and the computer store these numbers as shown below :
number [0]
number [1]
number [2]
number [3]
number [4]
The values can be assigned to the array as follows :
number [0] = 35;
number [1] = 40;
number [2] = 20;
number [3] = 57;
number [4] = 19;
Syntax of 1-D array
The general form of array declaration is :
Accessing elements in 1-D
Array
Total length of array= last index-first index+1
Array elements are accessed by using an integer
index. Array index starts with 0 and goes till size of
array minus 1.
Name of the array is also a pointer to the first
element of array.
Suppose you declared an array mark as above. The
first element is mark[0], the second element
is mark[1] and so on.
Initialization of an 1-D
Array
An array can be stored in following stages :
1. At compile time
2. At run time
1. Compile time initialization :
In compile time initialization, the array is initialized
when they are declared.(to fix size and elements)
The general form of initialization of array is :
type array-name[size] = { list of
values };
The list of values are separated by commas.
It is possible to initialize an array during
declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
You can also initialize an array like this.
int mark[] = {19, 10, 8, 17, 9};
Here, we haven't specified the size. However, the
compiler knows its size is 5 as we are initializing it
with 5 elements.
No If the number of elements is lesser than the size
te of an array then the rest of the locations are
automatically filled by 0.
2. Run-Time Initialization:
In run time initialization, the array is explicitly initialize at run
time.
This concept generally used for initializing large arrays.
Example:
for(i=0; i < 100; i++)
{
if( i < 50)
sum[i]
=0.0;
else
sum[i] =
1.0;
}
Here first 50 elements of the array sum are initialized to 0
and the remaining 50 elements are initialized to 1 at runtime.
Change Value of Array elements
int mark[5] = {19, 10, 8, 17, 9}
// make the value of the third element to -1
Þ mark[2] = -1;
// make the value of the fifth element to 0
Þ mark[4] = 0;
Þ int mark[5]={19,10,-1,17,0}
Access elements out of its bound!
Suppose you declared an array of 3 elements. Let's say,
int arr[3];
if int arr[3]= {2,5,3,6} //this is wrong
PROGRAM : Array declaration and initialization
#include<stdio.h>
int main()
{
int i=0;
int ar[5]={20,30,40,50,60};
//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++)
{
printf(“ar[%d]=%d \n", i, ar[i] ); //print array
}
return 0;
}
PROGRAM: Array Input/Output
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 elements: ");
// taking input and storing it in an array
for(int i = 0; i < 5;i++)
{
scanf("%d", &values[i]);
}
printf("Displaying elements: "); // printing elements of an array
for(int i = 0; i < 5; i++)
{
printf(“values[%d]=%d\n", i, values[i]);
}
return 0;
PROGRAM: Array Input/Output at run time
// Program to take number of elements from the user and store them in an array
#include <stdio.h>
int main()
{
int n, values[n];
printf("Enter no. of elements: ");
scanf("%d", &n);
// taking input and storing it in an array
printf("enter elements");
for(int i = 0; i < n; i++)
{
scanf("%d", &values[i]);
}
printf("Displaying elements: \n");
// printing elements of an array
for(int i = 0; i < n; i++)
{
printf("values[%d]=%d\n", i,values[i]);
}
return 0;
// Program to find the average of n numbers using arrays
#include <stdio.h>
int main() {
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i < n; i++)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum =sum+marks[i];
}
average = sum / n;
printf("Average = %d", average);
return 0;
2. Multi Dimensional Array
An array created with more than one dimension (size) is
called as multi dimensional array. Multi dimensional array
can be of two dimensional array or three dimensional
array or four dimensional array or more.
Most popular and commonly used multi dimensional array
is two dimensional array.
The 2-D arrays are used to store data in the form of table.
We also use 2-D arrays to create mathematical matrices.
Declaration of Multi- Dimensional Array
Syntax –
dataType arrayName[size1][size2]
….[sizeN],
here N is the number of dimensions.
e.g. Three dimensional array:
int survey[10][20][30];
Two-dimensional Arrays
A variable which represent the list of items using two
index (subscript) is called two-dimensional array.
The basic form of declaring a two-dimensional array :
Syntax:
data_type array_name[row_size][column_size];
e.g. int x[10][20];
Elements in two-dimensional arrays are commonly
referred by x[i][j] where i is the row number and ‘j’ is
the column number.
A two – dimensional array can be seen as a table with ‘x’
rows and ‘y’ columns where the row number ranges from 0
to (x-1) and column number ranges from 0 to (y-1).
int table[x][y];
Initialization of 2-D Array
There are two ways in which a Two-Dimensional array can be
initialized:
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array have 3 rows and 4 columns. The elements in the
braces from left to right are stored in the table also from left to
right. The elements will be filled in the array in the order, first 4
elements from the left in first row, next 4 elements in second row
and so on.
Better Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
This type of initialization make use of nested braces. Each set of
inner braces represents one row. In the above example there are
total three rows so there are three sets of inner braces.
Accessing Elements of Two-
Dimensional Arrays
Elements in Two-Dimensional arrays are accessed
using the row indexes and column indexes.
Example:
int x[2][1];
Note: In arrays if size of array is N. Its index will be
from 0 to N-1. Therefore, for row index 2 row
number is 2+1 = 3
Size of multidimensional arrays
Size of multidimensional arrays
Total number of elements that can be stored in a
multidimensional array can be calculated by multiplying the
size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200
elements.
Similarly,
array int x[5][10][20] can store total (5*10*20) = 1000
elements.
2D ARRAY PROGRAM
#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2
columns*/
OUTPUT-
int a[5][2] = { {0,0}, {1,2}, {2,4},
{3,6},{4,8}}; a[0][0]: 0
int i, j; a[0][1]: 0
/* output each array element's value a[1][0]: 1
*/
a[1][1]: 2
for ( i = 0; i < 5; i++ )
a[2][0]: 2
{
a[2][1]: 4
for ( j = 0; j < 2; j++ )
a[3][0]: 3
{
a[3][1]: 6
printf("a[%d][%d] = %d\n", i,j,
a[i][j] ); a[4][0]: 4
} a[4][1]: 8
}
return 0;