Presentation Transcript
Arrays :Arrays Designed By: Harendra Singh Dhaila,
Jaycees Public School,
Rudrapur, Uttrakhand
Definition :Definition Array is a collection of variables of same data type with a common name.
Types of Arrays :Types of Arrays One Dimensional Arrays Two Dimensional Arrays Multi Dimensional Arrays
3D Array, 4D Arrays etc.
Declaration of Array :Declaration of Array One Dimensional Arrays
int A[10];
float B[20];
char C[20]; Two Dimensional Arrays
int X[5][5];
char Y[5][25];
Initialization of the Array :Initialization of the Array int A[5]= {1,2,3,4,5};
int ARR[ ] = {1,2,3,4,5,6};
char B[20]=“Rudrapur”;
float C[5]={2.4,3.5,1.5};
int D[2][3]={{1,2},
{3,4},
{5,6}};
Accessing Array Elements :Accessing Array Elements In C++ first element of the array is always at zero position.
Sample Program on 1-D Array :Sample Program on 1-D Array #include
void main()
{
int A[10],i;
for(i=0;iA[i];
}
cout<<“\n Array Contents\n”;
for(i=0;i<10;i++)
{
cout<
Sample Program on 2-D Array :Sample Program on 2-D Array #include
void main()
{
int A[3][4],i,j;
for(i=0;iA[i];
}
cout<<“\n Array Contents\n”;
for(i=0;i<10;i++)
{ for(j=0j<4;j++)
cout<
Address Calculation in 2D Arrays :Address Calculation in 2D Arrays Row Major
Column Major 0
1
2
3
4 0 1 2 3 4 5 Base Address : 1000 Address of Location X Address of Location Y Size of Each Element : 2 Bytes In Row Major : 1032
In Column Major : 1016 In Row Major : 1018
In Column Major : 1032
Address Calculation in Row Major :Address Calculation in Row Major A[i][j]= B + [i*n + j]
Where
B=Base address of the array
n = Total column of the array
i = Row number of desired row
j = Column number of desired row
Address in Column Major :Address in Column Major A[i][j]= B + [i + j*m]
Where
B=Base address of the array
m = Total rows of the array
i = Row number of desired row
j = Column number of desired row
Sorting :Sorting Arranging number in order (either ascending or descending) is called sorting. TYPES OF SORTING
Selection Sort
Bubble Sort
Insertion Sort