ARRAYS
18 26 90 47 50 86 13 22 64 75
VAL
NAME OF THE ARRAY
Collection of similar type of data
(ALL ARE INTEGER VALUES)
Single variable contains
Multiple values
VAL
10 65 78
98 55 62
71 28 45
TYPES OF ARRAY
• 1-D array
• 2-D array
18 26 90 47 50 86 13 22 64 75
0
1
2
0 1 2
COLUMNS
R
O
W
S
MEMORY REPRESENTATION
One- Dimensional Array
18 26 90 47 50 86 13 22 64 75
4
0
2
0
4
0
2
2
4
0
2
4
4
0
2
6
4
0
2
8
4
0
3
0
4
0
3
2
4
0
3
4
4
0
3
6
4
0
3
8
4
0
4
0
Memory Address
0 1 2 3 4 5 6 7 8 9
Subscript/ Index
BYTES REQUIRED
Bytes=(size of data type)* (no_of_elements)
=2*10=20 bytes
MEMORY REPRESENTATION
1
0
0
0
1
0
0
2
1
0
0
4
1
0
0
6
1
0
0
8
1
0
0
10
1
0
0
12
1
0
0
14
1
0
0
16
1
0
0
18
BYTES REQUIRED
Bytes=(size of data type)* (no_of_rows)*(no_of_columns)
=2*3*3=18 bytes
Two- Dimensional Array
ARRAY DECLARATION
One-Dimensional Array
int arr[5]; contains 5 numbers
char name[15]; 15 characters (including 0)
float salary[10]; 10 floating point (decimal) values
Two-Dimensional Array
int arr[5][5]; contains 25(5x5) numbers
char names[15][10]; 15 names with 10 characters each
float salary[10][5]; contains 50(10x5) floating point (decimal) values
ARRAY INITIALIZATION
INTEGERS
1. int a[5]={1,2,3,4,5};
2. int a[ ]={1,2,3,4,5};
3. int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
4. int marks[ ][2]={ 10, 15,
20, 12,
14, 16};
ARRAY INITIALIZATION
CHARACTER ARRAY/ STRINGS
1. char str[6] = “Radha”;
2. char str[ ] = “Radha”;
3. char str[ ]={ ‘r’, ’a’ ,’d’ ,’h’ ,’a’ , ’0’};
4. char days[3][15]={“Monday”, “Tuesday”, “Wednesday”};
PROGRAMS
• Enter 10 numbers and display them
• Bubble Sort
• String program(2-d array)
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i;
cout<<“n ENTER 10 NUMBERS”;
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<“nNUMBERS ENTERED BY YOU ARE”;
for(i=0;i<10;i++)
{
cout<<“n”<<a[i];
}
getch();
}
Initial value of i=0 ???
Loop
???
BACK
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,j,size,temp;
cout<<"n ENTER THE SIZE OF THE ARRAY";
cin>>size;
for(i=0;i<size;i++)
{
cin>>a[i];
}
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<size;i++)
{
cout<<"n"<<a[i];
}
getch();
}
If array is
i=0j<size-i-1(size=8) j<8-0-1=7 j<7
j= 0 No change as a[0] > a[0+1] 10 > 46 (false)
j=1 Swapping as a[1]>a[1+1] 46 >12 (true)
10 46 12 5 30 2 24 17
10 46 12 5 30 2 24 17
10 46 12 5 30 2 24 17
j=2 Swapping as a[2]>a[2+1] 46 > 5 (true)
j=3 Swapping as a[3]>a[3+1] 46 > 30 (true)
J=4 Swapping as a[4]>a[4+1] 46>24(true)
10 12 46 5 30 2 24 17
10 12 5 46 30 2 24 17
10 12 5 30 46 2 24 17
J=5 Swapping as a[5]>a[5+1] 46 > 24 (true)
J=6 Swapping as a[6]>a[6+1] 46 > 30 (true)
ARRAY AFTER Ist PASS
10 12 5 30 2 46 24 17
10 12 5 30 2 24 46 17
10 12 5 30 2 24 17 46
i=1 j<size-i-1(size=8) j<8-1-1=6 j<6
j=0 No change as a[0] > a[0+1] 10 > 12 (false)
j=1 Swapping as a[1]>a[1+1] 12 >5 (true)
j=2 No change as a[2]>a[2+1] 12>30(false)
10 12 5 30 2 24 17 46
10 12 5 30 2 24 17 46
10 5 12 30 2 24 17 46
j=3 Swapping as a[3]>a[3+1] 30 > 2 (true)
j=4 Swapping as a[4]>a[4+1] 30 > 24 (true)
J=5 Swapping as a[5]>a[5+1] 30>17(true)
10 5 12 30 2 24 17 46
10 5 12 2 30 24 17 46
10 5 12 2 24 30 17 46
ARRAY AFTER IInd PASS
i=2 j<size-i-1(size=8) j<8-2-1=6 j<5
j=0 Swapping as a[0] > a[0+1] 10 > 5 (true)
j=1 No change as a[1]>a[1+1] 10 >12 (false)
10 5 12 2 24 17 30 46
10 5 12 2 24 17 30 46
5 10 12 2 24 17 30 46
PERFORM THE DRY RUN ON YOUR OWN TO GET SORTED LIST
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char name[10][15];
int I;
cout<<“nENTER 10 NAMES”;
for(i=0;i<10;i++)
{
gets(name[i]);
}
cout<<“nNAMES ENTERED BY YOU ARE”;
for(i=0;i<10;i++)
{
cout<<“n”<<name[i];
}
getch();
}

Arrays