2. 2
Introduction to Array
One Dimensional Array
Two Dimensional Array
Array with Function
Array with Pointer
Outline
3. Introduction to Array
o Collection of similar data types is called Array.
o every array has a data type, name and size.
o Data type can be any valid data type like int, float, char.
o The rules of variable naming can be applied to array names.
o The size of array tells how many elements are there in the
array.
o The array occupy the memory depending upon their size
and have contiguous area of memory.
o It can be accessed by using the array index.
3
5. One Dimensional Array
Syntax of one Dimensional Array
Data type array name [ size of array ] semicolon
int average [5];
Initialization of One Dimensional Array
Data type array name[size of array]={element1, ..., elements};
float university [3] = {1.2, 2.4, 5.2} ;
Int university [ ] = {1, 2, 5, 9, 22, 14} ;
5
6. One Dimensional Array/program1
#include<iostream>
using namespace std;
main( )
{
//int x [ ]= {2, 3, 4, 5, 6};
int x [5]= {2, 3, 4, 5, 6};
//Note we can use array size more
than count of element, but not less
than count of array elements
cout<<x[0]<<endl;
cout<<x[1]<<endl;
cout<<x[2]<<endl;
cout<<x[3]<<endl;
cout<<x[4];
}
6
8. One Dimensional Array/program3
#include<iostream>
using namespace std;
main( ) {
char x[ ]={'2', '3', '4', '5', '6' };
/*char x [5];
x[0]='A';
x[1]='h';
x[2]='m';
x[3]='a';
x[4]='d'; */
for(int a=0; a<=4; a++)
{
cout<<x[a]<<endl;
}
}
//Note: we can't declare array
like int [];
8
9. One Dimensional Array/program4
#include<iostream>
using namespace std;
main( )
{
char a[5];
int b;
cout<<"enter any
value"<<endl;
for(b=0;b<5;b++)
cin>>a[b];
cout<<"............."<<endl<<endl;
for(b=0;b<=4;b++)
{
cout<<a[b]<<'t';
}
}
9
10. One Dimensional Array/program5
#include<iostream>
using namespace std;
main( ) {
char ary [5];
int b;
cout<<"enter any five
characters"<<endl;
for(b=1; b<=5; b++)
cin>>ary [b];
cout<<"Numerals"<<'t'<<"Elements"
<<endl;
for(b=5; b>0; b--)
{
cout<<b<<"tt" <<ary [b]<<endl;
}
}
10
11. One Dimensional Array/finding minimum
number
#include <iostream>
using namespace std;
main( ) {
int num[ ] = {8, 36, 2, 44, 1, 60, 5, 89};
int min = num[0];
for (int i = 0; i < 8; ++i) {
if (num[i] < min)
min = num[i];
}
cout << "The lowest element in array = "<<min; }
11
12. One Dimensional Array/finding maximum
number
#include <iostream>
using namespace std;
main( ) {
int numb [ ] = {900, 2, 360, 44, 52, 700, 3400, 89, 16, 76, 8000, 28};
int max = numb[0];
int size=12;
for (int i = 0; i < size; i++) {
if (numb[i] > max)
max = numb[i]; }
cout << "The maximum element in array = "<<max; }
12
13. One Dimensional Array/using Bubble Sort
Technique
Bubble sort is very simple technique in which we can
compare every element with its adjacent element and
swap the elements if they are not in order.
At the every iteration (pass) of the bubble sort the
heaviest element gets bubbled up at the end of the
list.
13
18. One Dimensional Array/using Bubble Sort
Technique
#include<iostream>
using namespace std;
main ( )
{
int a, b , temp;
int ary[5] = {11,3,0,30,15};
for(a = 0; a<5; a++) {
for(b = a+1; b<5; b++)
{
18
if(ary[b] < ary [a]) {
temp = ary [a];
ary [a] = ary [b];
ary [b] = temp;
} } }
cout <<"Sorted Element List..."<<endl;
for(a = 0; a<5; a++)
{
cout <<ary [a]<<'t';
} }
19. 19
Two Dimensional Array
A two dimensional array is an array of arrays. In other words,
it is an array where each member of the array is also an array.
Syntax of one Dimensional Array
Data type array name [number of rows] [number of columns]
={array elements,…….} semicolon
Declaration and initialization
int 2dary [2][3]={1, 2, 3, 5, 6, 8};
Int 2dary [2][3]={ {1, 2, 3}, {5, 6, 8}};
20. Two Dimensional Array/p1
#include<iostream>
using namespace std;
main( ) {
int x[2][3];
x[0][0]=1;
x[0][1]=2;
x[0][2]=3;
x[1][0]=4;
x[1][1]=5;
x[1][2]=6;
int a, b;
for(a=0; a<2; a++)
{
for(b=0; b<3; b++)
cout<<x[a][b]<<'t';
cout<<endl<<endl;
}
}
20
21. Two Dimensional Array/p2
#include<iostream>
using namespace std;
main( ) {
int x[3][3]= { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int a,b;
for(a=0; a<3; a++) {
for(b=0; b<3; b++){
cout<<x[a][b]<<'t';
}
cout<<endl; } }
21
22. Two Dimensional Array/p3
#include<iostream>
using namespace std;
main() {
int x[3][4];
int a,b,sum=0;
cout<<"enter value"<<endl;
for(a=0;a<3;a++)
{
for(b=0;b<4;b++)
cin>>x[a][b]; }
for(a=0;a<3;a++) {
for(b=0;b<4;b++) {
sum+=x[a][b];
cout<<x[a][b]<<'t';
}
cout<<endl; }
cout<<"sum="<<sum;
}
22
23. Three Dimensional Array
To create an array variable that represents various lists, each
list would contain various internal lists, and each internal list
would contain its own components. This is referred to as a
multidimensional array.
One of the rules you must follow is that, as always, all
members of the array must be of the same type.
23
24. Three Dimensional Array
Syntax of Three Dimensional Array
Data type array-name [elements] [rows] [columns]={array
elements, , , ,, …..} semicolon
Declaration and initialization
Int x[2][2][2]={1, 2, 3, 4, 5, 6, 7, 8};
Int x[2][2][2]={ { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } };
24
29. Assignments
Write down a program to implement bubble sorting
technique with two and three dimensional array.
Is it possible to use bubble sort technique for descending
order? If yes then write down a program to implement bubble
sort in one dimensional array.
What is insertion sorting techniques? Why we use it? Briefly
explain stepwise the execution of each steps in your own
words with debugging example.
29
30. Array with Function/1 Dimensional /p1
#include <iostream>
using namespace std;
void info(int mbr [ ] );
main( )
{
int ary [ ]= {1, 2, 3, 4, 5, 6, 7};
info(ary);
}
void info(int a [ ])
{
for(int i = 0; i < 7; ++i){
cout << a[i]<<endl;
}
}
30
31. Array with Function/1 Dimensional /p2
#include <iostream>
using namespace std;
void info(int mbr[ ], int
count);
main( )
{
int ary [ ] = {1, 2, 3, 4, 5};
Info (ary, 5);
}
void info(int a [ ], int counter)
{
for(int i = 0; i < counter; ++i){
cout << a[i]<<endl;
}
}
31
32. Array with Function/1 Dimensional /p3
#include <iostream>
using namespace std;
void display( int [ ], int );
main( ) {
int a[3];
cout<<"enter any value"<< endl;
for(int i=0; i<3; i++) {
cin>>a[i];
}
display(a , i);
}
void display(int x[], int w)
{
cout<<"--------------"<<endl;
for(w=0; w<3; w++)
{
cout<<x[w]<<endl;
}
}
32
33. Array with Function/2 Dimensional
#include<iostream>
using namespace std;
void display(int [ ][3], int, int );
main( ) {
int a[2][3]={{1, 2, 3}, {4, 5, 6} };
display(a, 2, 3);
}
void display(int x[][3], int row,
int col)
{
for(int i=0; i<row; i++){
cout <<endl << endl;
for(int b=0; b<col ; b++)
{
cout<<x[i][b];
}
}}
//Note: In multidimensional
array must have bounds for
all dimensions except the first
33
34. Array with Function/3 Dimensional
#include<iostream>
using namespace std;
void display(int [ ][2][2], int, int,
int );
main( ) {
int ary [2][2][2]={ { {1, 2},{3, 4} },
{ {5, 6}, {7, 8} } };
display(ary, 2, 2, 2);
void display(int x[][2][2], int e, int
row, int col)
{
for(int a=0; a<e; a++)
for(int b=0; b<row; b++){
cout <<endl << endl;
for(int c=0; c<col ; c++)
{
cout<<x[a][b][c];
}
}}
34
35. Array with pointer/1 Dimensional /p1
#include<iostream>
using namespace std;
void display(int*,int );
main() {
int a[]={1, 2, 3};
display(& a[0], 3);
//a[0] it is necessary to give index
}
void display(int *x, int w)
{
int i;
for(i=0;i<w;i++)
{
cout<<x[i]<<endl;
//cout<<&x[i]<<endl;
} }
35
36. Array with pointer/1 Dimensional /p2
#include<iostream>
using namespace std;
void info(int &, int );
main()
{
int ary[4]={1, 2, 3, 4};
info( ary[0], 4);
}
void info(int & x, int w)
{
int i, *xx= &x;
for(i=0; i<w; i++)
{
cout<<xx[i]<<endl;
}
}
36
37. Array with pointer/2 Dimensional
#include<iostream>
using namespace std;
void display(int*, int, int );
main() {
int a[2][3]={1, 2, 3, 4, 5, 6};
display(&a[0][0],2,3);
}
void display(int *x,int row, int col)
{
for(int i=0;i<row;i++){
cout<<endl<<endl;
for(int b=0;b<col;b++) {
cout<<*x;
++x;
}
}}
37
38. Array with pointer/3 Dimensional
#include<iostream>
using namespace std;
void display(int*, int, int, int );
main( )
{
int x[2][2][2]={1, 2, 3, 4, 5, 6, 7, 8};
display(&x[0][0][0], 2, 2, 2);
}
void display(int *x, int ary, int
row, int col) {
for(int a=0; a<ary; a++){
cout<<endl<<endl;
for(int b=0; b<row; b++){
cout<<endl;
for(int c=0; c<col; c++) {
cout<<*x;
++x;} }
}}
38