Array:
An array is a collection of similar data items, that
are stored under a common name.
It is denoted by index or subscript enclosed in square
brackets, i.e. [].
Array value is starting with 0.
Types
of arrays:
- Single dimensional arrays
- Multidimensional arrays
1.
Single dimensional arrays:
The collection of items can be stored under a one
variable name using only one subscript, such a variable is called ‘one dimensional
array or single dimensional array’.
Array declaration of syntax:
Data_type array_variable [Size of the array];
Data_type array_variable [Size of the array];
Example program single dimensional array:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3];
clrscr();
printf("Enter two data for addition\n");
scanf("%d %d",&a[0],&a[1]);
a[2]=a[0]+a[1];
printf("Result is : %d",a[2]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3];
clrscr();
printf("Enter two data for addition\n");
scanf("%d %d",&a[0],&a[1]);
a[2]=a[0]+a[1];
printf("Result is : %d",a[2]);
getch();
}
2.
Multidimensional arrays:
Multidimensional arrays are used in a situation where
a table of value needs to be stored in an array.
Array declaration of syntax:
Data_type array_variable [row size][column size];
Data_type array_variable [row size][column size];
Example program multidimensional array:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int std[2][1];
int i;
clrscr();
for (i=0;i<=2;i++)
{
printf("\n
Enter %d student roll number and mark: ",i);
scanf("%d
%d",&std[i][0],&std[i][1]);
}
for (i=0;i<=2;i++)
{
printf("\n %d Student roll number %d and mark %d ",i,std[i][0],std[i][1]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int std[2][1];
int i;
clrscr();
for (i=0;i<=2;i++)
{
printf("\n
Enter %d student roll number and mark: ",i);
scanf("%d
%d",&std[i][0],&std[i][1]);
}
for (i=0;i<=2;i++)
{
printf("\n %d Student roll number %d and mark %d ",i,std[i][0],std[i][1]);
}
getch();
}
Explanation:
No comments:
Post a Comment