Arrays


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];

Example:
int a[5]={5,10,2,1,4};
Explanation:

Example program single dimensional array:

#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();
}

Output:


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];

Example:
int a[3][3];
Explanation:


Example program multidimensional array:

#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();
}

Output:

Explanation:

Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me