Array
Array is the collection of similar type of data elements
Suppose we want to store the roll no’s of student then there
are two ways
1. Declare 25 different variables for storing 25 roll no’s
2. Use a single variable which can store 25 values (roll no’s)
The first way is very inconvenient or difficult because it
require a lot of variables
If we want to store marks, total and percentage of the
student, we have to declare a large number of variables.
Handling a large no of variables makes the program very
difficult
The second method of is easy to use this facility is
provided by arrays
So using array you can store more values of same variable.
The general format of declaring an array is
data type array name [dim…][dim..][dim..];
where data type is the type of
which you want to create an array .
Array is the name of that array dim
1, dim1,dim 2… are the dimension of the array.
Dimension means the capacity of the
array i.e. specifies the no of elements of array can store.
The dimension must be
including the square brackets.
Example
int rollno[25];
char name[20][30];
The first and third example declares one dimensional array.
Where the second example a two dimensional array.
- The first example tells the compiler that an int array call rollno, is declared and it can store 25 int values .
- The second array which can store at the most 20*30=600.
Accessing the individual element of an array :-
Just storing no of values is an array is not sufficient but
you should access that array.
For this you have to use subscript .Subscript means the
position of an element in a array.
The counting of array starts from zero and not from the one
.
So the nth element will have the subscript (n-1)
e.g.
In the array rollno you can store 25 int values .So int
values .
So rollno[0] will give 1 st values
Subscript element
rollno[0] 1st
rollno[11] 12th
rollno[25] 26th
Memory allocation for array
The array element are stored in contiguous memory locations
.
So all the elements from an array
Eg int rollno[10];
char x[5];
float y[5];
0
|
1
|
2
|
3
|
4
|
x
0
|
1
|
2
|
3
|
4
|
y
Types of array :-
1.
One dimensional array
2.
Multidimensional
array
In one dimensional array we specify only one dimension
Eg int rollno[10];
If we specify more than one dimension it is known as
multidimensional array.
In multidimensional array we will study double dimensional
array.
So , in double dimensional array we specify two dimensions
Eg int a[10][10];
char x[30][50];
Bound checking
When we try to access any element out of range or boundaries
of array
We expect that the compiler should give an error .
But compiler doesn’t give an error .
Instead of it you will get unexpected result i.e. suppose we
have the array
int rollno[10];
and we want to access the element rollno[10],rollno[11]…
then no error is given by compiler but you will have wrong result .
so the care should be taken by the programmer that array
overflow doesn’t occur.
Initialization
of one dimensional array
Initialization means providing some initial value to a
variable .
Generally when we don’t initialize a variable it contains
garbage value .
Like simple variable initialization.
We can initialization of
one dimensional array at the time of declaration .
For this we have to use opening and closing curly brackets.
Eg char x[ ] ={ ‘a’,’b’,’v’,’p’};
int y[] = {
10,15,20,25,30};
If we are initializing array at the time of declaration
.there will be no need to specify fix dimensions .
In the initialization the values are separated by comma while
initializing a above array the values must be enclosed within single quotes.
The following code initializes the values in the array
sequentially and then prints them out:
#include <stdio.h>
int main()
{
int a[5];
int i;
for (i=0; i<5;
i++)
a[i] = i;
for (i=0; i<5;
i++)
printf("a[%d] = %d\n", i, a[i]);
}
Two dimensional
array
In double dimensional array we have to specify two
dimensions ,
Then the total no of elements that array can hold or store
is equal to the multiplication of those two dimensions .
The general format of declaration of double dimension array
is
data type
array name [dim1][dim2]
rows columns
where , data type is the type of array you want to create .
array name
is the name of array
dim1 &
dim2 are the two dimensions.
The double dimensional array is accessed in terms of rows
and columns i.e. tabular format.
This is just for the simplicity of user whereas, the
compiler will store all those elements serially.
int a[][]={ { 10,20,30},{40,50,60},{70,80,90};
e.g. char
name[10][20];
float
x[5][10];
int a[2][3];
The first statement declares a double dimension char array name
which can have at the most 20 columns ; i.e. name can store 10 strings and
every string can have at the most 20 characters.
Double dimensional
array initialization :-
The double dimensional array can be initialized like one
dimensional array by specifying all the values .
e.g. int a[2][3]={ 10,20,30,40,50,60};
10
|
20
|
30
|
40
|
50
|
60
|
2001
2003 2005
2007 2008 2009
Array values
A[0][0]
10
A[0][1] 20
A[0][2] 30
A[1][0] 40
A[1][1] 50
A[1][2] 60
When we declare n dimension
0 1 2
10
|
20
|
30
|
40
|
50
|
60
|
The compiler allocates a continuous memory for all the
elements in array as shown in fig
for accessing the individual element of double dimension
array we have to specify its respective row number as well as column number in
separate square brackets. The counting
of row as well as column status with zero, so in above example a[0] [0] will be
the first element in array whereas, a[1] [2] is the last element.
The double
dimensional array can be said that it is a one dimensional array in which every
element is again a one dimensional array using this e can initialize, the
double dimensional array as
int a[2]
[3] = {{10, 20, 30}
{40,
50, 60}}
/ * Double dimensional array demo */
#
include <stdio.h>
#
include<conio.h>
main(
)
{
int a[3] [3]; , /* Array declaration
int I, j ,
clscr c;
/* Storing
the elements in array*/
for (i=0’ i<3; i++)
{
Printf (“\n Enter
a number : “);
scanf (“%d”, & a[i] [j])
}
/*
Printing the elements */
for (i=0;
i<3; i++)
{
printf (“\n”);
for (j=0; j<3; j++)
{
Printf (“%d”,
a[i][j]);
}
}
}
No comments:
Post a Comment