Array Definition and Sample Program With Notes

Array Definition and Sample Program With Notes


An Array is used to store multiple elements of the same type. A variable is used
to store only one value. For example, if we declare a variable x, as follows

int x;
x can store only one value of integer type.

If we wish to store many elements like sets, for eg: Set A = { 2, 4, 5, 7}

We have to take help of Arrays. An array can be used to store multiple
elements of the same type into a single Array Variable.



syntax:        Datatype    ArrayName[size];
eg:               int  A[10];
A is an Array which can store a maximum of 10 integers.

If we wish to store elements in a matrix (rows X columns) we have
to use a 2-Dimensional Array.

syntax:     Datatype   MatrixName[rowsize][col.size];
eg:            int  A[10][10];

Here A is a matrix which can store 10 x 10 (100) 
elements, arranged in 10 rows and each row can have 10 elements.

Array Program: 

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int marks[3];
cout<<"Enter marks";
for(int i=0;i<3;i++)
{
cin>>marks[i];
}
cout<<"the values are"<<endl;
for(int j=2;j>=0;j--)
{
cout<<marks[j]<<endl;
}
getch();
}

Comments