Arrays: An array is defined as collection of logically related data items of the same data type,
stored in contiguous memory locations, sharing a common name.
Each element of the array can be referred by using a value called index (or) subscript in
brackets after the array name. The index must be an integral value or an expression that evaluates
to an integral value.
Depending on the number of subscripts used, arrays can be categorized into one-
dimensional arrays, two-dimensional arrays and so on.
One-dimensional arrays: An array with only one subscript is called as one-dimensional array
(or) 1-d array. It is used to store a list of values, all of which share a common name.
Declaration of one-dimensional arrays: Arrays must be declared before they are used. Array
declaration tells the compiler the name of the array, type of each element, and the size (or)
number of elements in the array. The general form of declaring a one-dimensional array is
data_type array-name[size];
The type specifies any valid data type supported by C such as int, float or char. array-
name is any valid C identifier, and size indicates the maximum number of elements that can be
stored in an array.
Ex: 1.The statement int score[9]; declares score is an array containing maximum of 9 integer
elements.
2. The statement float height[50]; declares height is an array containing maximum of 50 real
elements.
Accessing elements in one-dimensional Arrays: Each element of the array can be referred by
using a value called index (or) subscript in brackets after the array name. The index must be an
integral value or an expression that evaluates to an integral value. The subscript value is indexed
from 0 to size-1.when the subscript value is 0, first element in the array is selected, when the
subscript value is 1, second element is selected and so on.
Storing values in one-dimensional arrays: Declaration and definition only reserve space for
the elements in the array. No values are stored. If we want to store values in the array, we must
initialize the elements, read values from the keyboard (or) assign values to each individual
element.
PSC VVIT, CSE Dept Page 1
,UNIT III Arrays and Strings
Initialization of one-dimensional arrays: We can initialize the elements of the one-dimensional
array when the array is declared. The general form is
data_type array-name[size]= {list of values};
The values in the list are separated by commas.
Ex: The statement int x[6]={10,20,30,40,50,60}; declares that x is an array containing 6 integer
elements whose values are given in the list which are as follows.
10 20 30 40 50 60
x[0] x[1] x[2] x[3] x[4] x[5]
Note: when the array is completely initialized, there is no need to specify the size of the array.
Ex: The statement int x[ ]={3,6,9,10,11,12}; declares that x is an array containing 6 integer
elements whose initial values are given in the list which are as follows. since the number of
values in the list for the array x is six, the size of x is automatically supplied as six.
3 6 9 10 11 12
x[0] x[1] x[2] x[3] x[4] x[5]
Note: If the number of elements in the list is less than the size of the array, then only that many
elements will be initialized. The remaining elements will be set to zero automatically.
Ex: The statement int x[6]={3,6,9,10}; will initialize the first four elements to 3,6,9 and 10 and
the remaining elements two elements are set to zero.
3 6 9 10 0 0
x[0] x[1] x[2] x[3] x[4] x[5]
Inputting Values from the Keyboard: We can store the values in to the array from the
keyboard. This can be done using a loop in which the index can be varied as shown below. For
example, the statement.
for(i=0;i<6;i++)
{
scanf(“%d”,&x[i]);
}
Stores 6 values in to the array integer array x[ ]. Here index i varies from 0 to 5.
PSC VVIT, CSE Dept Page 2
,UNIT III Arrays and Strings
Ex program:
/*program to read 10 values into one dimensional array using keyboard and printing them*/
#include<stdio.h>
int main( )
{
int i,x[10];
printf(“enter the elements of the array\n”);
for(i=0;i<10;i++)
scanf(“%d”,&x[i]);
printf(“the elements of the array are \n”);
for(i=0;i<10;i++)
printf(“%d\t”,x[i]);
return 0;
}
Assigning Values: Values to individual elements can be assigned using the assignment operator.
For example, simple assignment statement x[0]=100; assigns 100 to x[0].
Similarly the statement number[0]=35; assigns 35 to number[0].
Similarly we can write number[2]=number[0]+number[1];
Note: one array cannot be copied to another using assignment statement.
/*program to find maximum and minimum element from the list of elements*/
#include<stdio.h>
int main( )
{
int i,n,max,min,x[50];
printf(“enter the total number of elements of the array\n”)
scanf(“%d”,&n);
printf(“enter the elements of the array\n”);
for(i=0;i<n;i++)
scanf(“%d”,&x[i]);
printf(“the elements of the array are \n”);
for(i=0;i<n;i++)
printf(“%d\t”,x[i]);
max=min=x[0];
for(i=0;i<n;i++)
{
if(max<x[i])
max=x[i];
if(min>x[i])
min=x[i];
}
printf(“maximum element =%d\n”,max);
printf(“minimum element =%d\n”,min);
return 0;
}
PSC VVIT, CSE Dept Page 3
, UNIT III Arrays and Strings
Two Dimensional Arrays: An array with two subscripts is called as two dimensional array. A
two dimensional array can be thought of as a group of one or more one-dimensional arrays all of
which share a common name and are separable by subscript values.
Declaration of Two-dimensional arrays: Arrays must be declared before they are used. Array
declaration tells the compiler the name of the array, type of each element, and the size (or)
number of elements in the array. The general form of declaring a two-dimensional array is
data_type array-name[rowsize][colsize];
The type specifies any valid data type supported by C such as int, float or char, array-
name is any valid C identifier. rowsize indicates the number of rows and colsize indicates the
number of elements in each row.
Ex: 1.The statement int marks[3][3]; declares marks is a two dimensional array containing 3
rows and each row contains maximum of 3 elements.
Accessing elements in Two-dimensional Arrays: Each element of the two dimensional array
can be referred by using two indexes (or) subscripts in brackets after the array name. The first
index and second index must be an integral value or an expression that evaluates to an integral
value. The first subscript value is indexed from 0 to rowsize-1. The second subscript value is
indexed from 0 to colsize-1.
Memory gets allocated to store the array marks as follows.
Column numbers
0 1 2
0 Marks[0][0] Marks[0][1] Marks[0][2]
numbers
1 Marks[1][0] Marks[1][1] Marks[1][2]
Row
2 Marks[2][0] Marks[2][1] Marks[2][2]
Storing values in two-dimensional arrays: Declaration and definition only reserve space for
the elements in the array. No values are stored. If we want to store values in the array, we must
initialize the elements, read values from the keyboard (or) assign values to each individual
element.
Initialization of two-dimensional arrays: We can initialize the elements of the two-
dimensional array when the array is declared. The general form is
data_type array-name[rowsize][colsize]= {list of values};
The values in the list are separated by commas.
PSC VVIT, CSE Dept Page 4