Introduction to arrays


Definition: An array is a collection of similar data items that are stored under a common variable name and stored in contiguous memory locations.

Array is a Derived data type, which is collection of homogeneous elements of the same data type. A value in an array is identified by its subscript or
 index enclosed in square brackets with array name.

            Ordinary variables are capable of holding only one value at a time.  However, there are situations in which we would want to store more than one value
 at a time in a single variable in this type of situations we are using arrays.

Example program to find average of 10 marks without using arrays


Write a program to declare 10 marks and assign values and find out average of 10 marks.

#include<stdio.h>
#include<conio.h>
void main()
{
 int m1,m2,m3,m4,m5,m6,m7,m8,m9,m10;
float avg;
printf(“Enter 10 Marks: );
scanf(“%d%d%d%d%d%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5,&m6,&m7,&m8,&m9,&m10);
avg=(float)(m1+m2+m3+m4+m5+m6+m7+m8+m9+m10)/10;
printf(“Avg=%f”,avg);
}


Here we declared 10 variables to hold 10 values, but we require a solution where we want to store all these 10 values in a single variable name.
In this type of situations we are using an array with single name and stores all 10 marks.

Types of Arrays

  1. One-Dimensional Arrays or 1D
  2. Two-Dimensional Arrays or 2D
  3. Multi-Dimensional Arrays

No comments:

Post a Comment