Array is a collection of variables of the same type referenced under one common name and they include data of homogeneous nature.
Syntax: type array name[size];
Example: int marks[size];
Types of Array:
Single dimensional Array:The 1D array is the type of array which is similar to the base type array.
SYNTAX: type array name[size];
Example: int marks[50];
Program:
#include<iostream.h>
#include<conio.h>
int main()
{
const int size=5;
float marks[size];
for(int i=0;i<size;i++)
{
cout<<"Enter marks of student"<<(i+1)<<"\n";
cin>>marks[i];
}
cout<<"\n";
for(int i=0;i<size;i++)
cout<<"Marks["<<i<<"]"<<marks[i]<<"\n";
getch();
return 0;
}
2.Two Dimensional Array:The 2D array is the type of array in which is defined in the form of rows and columns.
SYNTAX: type arrayname[rows][columns];
Example: int a[10][20];
Program:
#include<iostream.h>
#include<conio.h>
int main()
{
int sales[4][5];
int i,j;
long total;
for(i=0;i<4;i++)
{
total=0;
cout<<"Enter sales for salesman"<<(i+1)<<"\n";
for(j=0;j<5;j++)
{
cout<<"Month"<<(j+1)<<"";
cin>>sales[i][j];
total+=sales[i][j];
}
cout<<"\n";
cout<<"Total sales of salesman"<<(i+1)<<"="<<total<<"\n";
}
getch();
return 0;
}
ARRAY Intialization: The array intialiazation is done in two ways unsized and size intialiazation
Sized: type array name [size]={value list};
Example: int dayofmonth[12]={30,29,30,31,30,29,30,31,30,30,31,29};
Unsized : type array name[ ]={value list};
Example: int days[ ]={1,2,3,4,5,6,7};
Comentarios