C - ARRAYS
ARRAYS:
It is collection of similar data type data.
There are two types of array –
1. Single Dimensional Array
2. Multi Dimensional Array
Values are arranged in the form of columns.
SYNTAX-
Data type array name [size];
int a [5];
In this data type
is integer and name of array is num and can store 3 elements of integer type.
Array work on the basis of index value. Its
index value always starts with 0. So, when we work on array then we take one
less than total size of array.
EXAMPLE-
/*
Example of Single Dimensional Array */
main()
{
int
num[3],i,sum=0;
for
(i=0;i<3;i++)
{
printf
(“Enter number “);
scanf(“%d”,
&num [i]);
}
for
(i=0;i<3;i++)
{
sum+=
num[i];
}
printf(“sum
is “, sum);
getch();
}
Output-
Enter
Number 2
Enter
Number 43
Enter
Number 13
Sum
is 58
2. Multi Dimensional Array –
In multi dimensional array data is stored in
rows and columns.
SYNTAX-
Data type array name [rows] [columns];
int num [3] [3];
In that array number of rows re 3 and columns
are 3.
EXAMPLE-
/* Example of multi Dimensional Array */
main()
{
int
num[3][2],i,j;
for
(i=0;i<3;i++)
{
for
(j=0;j<2;j++)
{
printf
(“Enter number “);
scanf(“%d”,
&num [ i ][ j ]);
}
}
for
(i=0;i<3;i++)
{
for
(j=0;j<2;j++)
{
printf
(“%d\t“,num [ i ][ j ] );
}
printf
(“\n”);
}
getch();
}
Output-
Enter
number 1
Enter
number 2
Enter
Number 3
Enter
number 4
Enter
number 5
Enter
number 6
1 2
3 4
5 6
Comments
Post a Comment