Sei sulla pagina 1di 7

Arrays

7.1. INTRODUCTION
An array is a set of similar data types that share a common name. For
example, we can define an array name rollno to represent a set of roll numbers of
a group of students. A particular value is indicated by writing a number, called
index number or subscript within brackets after the array name. For example
rollno[20 represents the roll number of 20
th
student. !hile the complete set of
values is referred to as an array, the individual values are called elements. Arrays
can be of any variable type. "onsider the following program of finding the
greatest among three numbers num#, num2 and num$ %
/* Finding greatest among three numbers */
in!"ude #stdio.h$
in!"ude #!onio.h$
%oid main&'
(
int num1) num*) num+) great ,
!"rs!r&' ,
-rint.&/0nter three numbers 1 /' ,
s!an.&/2d 2d 2d/) 3num1) 3num*) 3num+' ,
i.&num1 $ num* 33 num1 $ num+'
great 4 num1 ,
e"se i.&num* $ num1 33 num* $ num+'
great 4 num* ,
e"se
great 4 num+ ,
-rint.&/5nThe greatest numbers is 1 2d/) great' ,
get!h&' ,
6
RUN 1 1
0nter three numbers 1 *7 +7 17
The greatest numbers is 1 +7
Finding out the greatest among $ numbers may not be a cumbersome task.
&magine the task of finding the greatest among #0 numbers. 'his could be done
by setting up a series of if statements to compare each of the numbers.
7.2 Arrays
'he same problem could be solved if we introduce arrays in the place of
individual numbers i.e., by declaring a homogeneous group of number values
(integer, in this case). 'he program for finding greatest among #0 numbers are as
shown below. !e define a variable called num[#0 which represents an entire set
of #0 numbers. *ach element of the set can then be referenced by means of a
number called an index number or subscript.
/* Finding greatest among ten numbers */
in!"ude #stdio.h$
in!"ude #!onio.h$
%oid main&'
(
int num8179) i) great ,
!"rs!r&' ,
-rint.&/0nter ten numbers 1 5n5n/' ,
.or&i 4 7 , i # 17 , i::'
s!an.&/2d/) 3num8i9' ,
great 4 num879 ,
.or&i 4 7 , i # 17 , i::'
i.&num8i9 $ great'
great 4 num8i9 ,
-rint.&/5nThe greatest numbers is 1 2d/) great' ,
get!h&' ,
6
RUN 1 1
0nter ten numbers 1
17 +7 *7 ;7 <7 =7 77 177 >7 ?7
The greatest numbers is 1 177
Arrays whose elements are specified by one subscript are called single
dimensional arrays. "orresponding, arrays whose elements are specified by two
and three subscripts are called double dimensional and multi dimensional arrays
respectively.
7.*. @INAB0 DIC0N@IONAB ARRAD@
A list of items can be given a variable name using only one subscript and such
a variable is called a single subscripted variable or single dimensional array. For
example, if we want to represent a set of + numbers, say #0, 20, $0, ,0 and +0
by an array variable num, then we can declare the variable num as follows %
int num[+ -
Arrays 7.3
'he subscript of the array starts with 0 and the computer reserves + storage
locations as shown below %
num[0
num[#
num[2
num[$
num[,
'he value to the array elements can be assigned as follows %
num[0 . #0
num[# . 20
num[2 . $0
num[$ . ,0
num[, . +0
'his would cause the array num to store the values as shown below %
#0 num[0
20 num[#
$0 num[2
,0 num[$
+0 num[,
'hese elements may be used in programs /ust like any other " variable. For
example, the following are valid statements %
a . num[0 0 #0 -
num[, . numn[, 0 +0 -
7.+. D0CBARATION OF @INAB0 DIC0N@IONAB ARRAD@
1ike any other variable, arrays must be declared before they are used. 'he
syntax is %
data_type variable_name [size] ;
!here data2type specifies the type of element that will be contained in the
array, such as int, float or char. 'he si3e indicates the maximum number of
elements that can be stored inside the array. For example %
7.4 Arrays
float basicpay[40 -
5eclares the basicpay to be an array containing 40 real elements. Any
subscripts 0 to +6 are valid. 7imilarly %
int empno[40 -
5eclares the empno as an array to contain a maximum of 40 elements.
'he " language treats character strings simply as arrays of characters. 'he
si3e in a character string represents the maximum number of characters that the
string can hold. For example %
char mesg[8 -
5eclares the mesg as a character array (string) variable that can hold a
maximum of 8 characters. 7uppose we read the following string constant into the
string variable mesg %
9!*1":;*<
*ach of the string is treated as an element of the array mesg and is stored in
the memory as follows %
=!> mesg[0
=*> mesg[#
=1> mesg[2
="> mesg[$
=:> mesg[,
=;> mesg[+
=*> mesg[4
=?0> mesg[@
!hen the compiler sees a character string, it terminates it with an additional
null character. 'hus, the element mesg[@ holds the null character =?0> at the end.
!hen declaring character arrays allow one extra element space for the null
terminator.
'he following are the important points to be noted while suing arrays %
#. An array name should be a valid " variable name.
2. 7ubscripts must always be a positive integer or expression that gives a
positive integer.
Arrays 7.5
7.;. DOUEB0 DIC0N@IONAB ARRAD
A list of items can be given a variable name using two subscripts and such a
variable is called a double dimensional array. For example, if we want to represent
a matrix of 6 numbers as shown below %
#0 $0 +0
20 ,0 40
@0 80 60
Ay an array variable mat, then we can declare the variable mat as follows %
int mat[$[$ -
'he subscript of the array starts with [0[0 and the computer reserves 6
storage locations as shown below %
Colum
n [0]
Colum
n [1]
Column
[2]
Row
[0]
[0[0 [0[# [0[2
Row
[1]
[#[0 [#[# [#[2
Row
[2]
[2[0 [2[# [2[2
'he value to the array elements can be assigned as follows %
mat[0[0 . #0
mat[0[# . $0
mat[0[2 . +0
mat[#[0 . 20
mat[#[# . ,0
mat[#[2 . 40
mat[2[0 . @0
mat[2[# . 80
mat[2[2 . 60
7.6 Arrays
'his would cause the array mat to store the values as shown below %
Colum
n [0]
Colum
n [1]
Column
[2]
Row
[0]
#0
[0[0
$0
[0[#
+0
[0[2
Row
[1]
20
[#[0
,0
[#[#
40
[#[2
Row
[2]
@0
[2[0
60
[2[#
80
[2[2
'hese elements may be used in programs /ust like any other " variable. For
example, the following are valid statements %
diagsum . mat[0[0 0 mat[#[# 0 mat[2[2 -
mat[2[2 . mat[2[2 0 #0 -
7.<. D0CBARATION OF DOUEB0 DIC0N@IONAB ARRAD
1ike any other variable, arrays must be declared before they are used. 'he
syntax is %
data_type variable_name [row_size][col_size] ;
!here data2type specifies the type of element that will be contained in the
array, such as int, float or char. 'he row2si3e indicates the maximum number of
rows and the col2si3e indicates the maximum number of columns, and the
product of row2si3e x col2si3e represents the maximum number of elements that
can be stored inside the array. For example %
int mata[$[$ -
5eclares the mata to be an array containing 6 ($ x $) integer elements. Any
subscript [0[0 to [2[2 are valid. 7imilarly %
'he following are the important points to be noted while suing arrays %
#. An array name should be a valid " variable name.
2. 7ubscripts must always be a positive integer or expression that gives a
positive integer.
Arrays 7.7
$. &f there are more than one subscript, each must be specified within
separate sBuare brackets.

Potrebbero piacerti anche