SlideShare a Scribd company logo
1
Array
Program: 2nd Semester
Department: SE- CSF
Due Date : 02-06-20
By: Serat
First Lecture
2
 Introduction to Array
 One Dimensional Array
 Two Dimensional Array
 Array with Function
 Array with Pointer
Outline
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
Types of Array
One dimensional Array
Two dimensional Array
Three dimensional Array
4
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
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
One Dimensional Array/program2
#include<iostream>
using namespace std;
main()
{
int count [6];
//float count [4];
count [0] = 9;
count [1] = 55;
count [2] = 3;
count [3] = 1;
/*count [0] = 99.5;
count [1] = 5.3;
count [2] = 3.2;
count [3] = 1.3; */
cout<<count[0]<<endl;
cout<<count[1]<<endl;
cout<<count[2]<<endl;
cout<<count[3]<<endl;
}
7
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
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
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
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
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
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
One Dimensional Array/using Bubble Sort
Technique
14
One Dimensional Array/using Bubble Sort
Technique
15
One Dimensional Array/using Bubble Sort
Technique
16
One Dimensional Array/using Bubble Sort
Technique
17
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
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}};
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
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
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
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
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
Three Dimensional Array/p1
#include<iostream>
using namespace std;
main( ) {
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} } };
/* int x[2][2][2];
x[0][0][0]=1;
x[0][0][1]=2;
x[0][1][0]=3;
x[0][1][1]=4;
x[1][0][0]=5;
x[1][0][1]=6;
x[1][1][0]=7;
x[1][1][1]=8; */
cout<<x[0][0][0]<<endl;
cout<<x[0][0][1]<<endl;
cout<<x[0][1][0]<<endl;
cout<<x[0][1][1]<<endl;
cout<<x[1][0][0]<<endl;
cout<<x[1][0][1]<<endl;
cout<<x[1][1][0]<<endl;
cout<<x[1][1][1]<<endl; }
25
Three Dimensional Array/p2
#include<iostream>
using namespace std;
main( ) {
int x[2][3][4]={ { {1, 2, 3, 4},
{5, 6, 7, 8}, {9, 10, 11, 12} },
{ {13, 14, 15, 16}, {17, 18, 19, 20},
{21, 22, 23, 24} } };
cout<<x[0][0][0]<<endl;
cout<<x[0][0][1]<<endl;
cout<<x[0][0][2]<<endl;
cout<<x[0][0][3]<<endl;
cout<<x[0][1][0]<<endl;
cout<<x[0][1][1]<<endl;
cout<<x[0][1][2]<<endl;
cout<<x[0][1][3]<<endl;
cout<<x[0][2][0]<<endl;
cout<<x[0][2][1]<<endl;
cout<<x[0][2][2]<<endl;
cout<<x[0][2][3]<<endl;
cout<<x[1][0][0]<<endl;
26
Three Dimensional Array/p2
cout<<x[1][0][1]<<endl;
cout<<x[1][0][2]<<endl;
cout<<x[1][0][3]<<endl;
cout<<x[1][1][0]<<endl;
cout<<x[1][1][1]<<endl;
cout<<x[1][1][2]<<endl;
cout<<x[1][1][3]<<endl;
cout<<x[1][2][0]<<endl;
cout<<x[1][2][1]<<endl;
cout<<x[1][2][2]<<endl;
cout<<x[1][2][3]<<endl; }
27
Three Dimensional Array/p3
#include<iostream>
using namespace std;
main( ) {
int x[3][3][3]={ { {1, 2, 3}, {4, 5, 6},
{7, 8, 9} }, { {10, 11, 12}, {13, 14, 15},
{16, 17, 18} }, { {19, 20, 21}, {22, 23,
24}, {25, 26, 27} } } , a, b, c;
for(a=0; a<3; a++)
{
cout<<endl;
for(b=0;b<3;b++) {
cout<<endl;
for(c=0;c<3;c++) {
cout<<x[a][b][c];
}
//cout<<endl; //space between
adjacent row
}
//cout<<endl; // space between row
} }
28
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
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
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
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
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
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
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
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
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
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
Thanks
39

More Related Content

Similar to SP-First-Lecture.ppt (20)

PPT
Arrays 06.ppt
ahtishamtariq511
 
PPT
Arrays
Saranya saran
 
PDF
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
PPTX
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PPTX
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PPT
array2d.ppt
DeveshDewangan5
 
PPT
Array i imp
Vivek Kumar
 
PDF
About Array
DharmaKumariBhandari
 
PPT
Arrays in c programing. practicals and .ppt
Carlos701746
 
PPTX
arrays in c programming - example programs
stalin721831
 
PDF
C++ L04-Array+String
Mohammad Shaker
 
PPTX
6_Array.pptx
shafat6712
 
PPT
Lec2&3_DataStructure
Ibrahim El-Torbany
 
PPT
Lec2&3 data structure
Saad Gabr
 
PPT
Lec2
Saad Gabr
 
DOCX
Array assignment
Ahmad Kamal
 
PPTX
Arrays in C language
Shubham Sharma
 
Arrays 06.ppt
ahtishamtariq511
 
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
array2d.ppt
DeveshDewangan5
 
Array i imp
Vivek Kumar
 
Arrays in c programing. practicals and .ppt
Carlos701746
 
arrays in c programming - example programs
stalin721831
 
C++ L04-Array+String
Mohammad Shaker
 
6_Array.pptx
shafat6712
 
Lec2&3_DataStructure
Ibrahim El-Torbany
 
Lec2&3 data structure
Saad Gabr
 
Lec2
Saad Gabr
 
Array assignment
Ahmad Kamal
 
Arrays in C language
Shubham Sharma
 

Recently uploaded (20)

PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPT on the Development of Education in the Victorian England
Beena E S
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 

SP-First-Lecture.ppt

  • 1. 1 Array Program: 2nd Semester Department: SE- CSF Due Date : 02-06-20 By: Serat First Lecture
  • 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
  • 4. Types of Array One dimensional Array Two dimensional Array Three dimensional Array 4
  • 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
  • 7. One Dimensional Array/program2 #include<iostream> using namespace std; main() { int count [6]; //float count [4]; count [0] = 9; count [1] = 55; count [2] = 3; count [3] = 1; /*count [0] = 99.5; count [1] = 5.3; count [2] = 3.2; count [3] = 1.3; */ cout<<count[0]<<endl; cout<<count[1]<<endl; cout<<count[2]<<endl; cout<<count[3]<<endl; } 7
  • 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
  • 14. One Dimensional Array/using Bubble Sort Technique 14
  • 15. One Dimensional Array/using Bubble Sort Technique 15
  • 16. One Dimensional Array/using Bubble Sort Technique 16
  • 17. One Dimensional Array/using Bubble Sort Technique 17
  • 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
  • 25. Three Dimensional Array/p1 #include<iostream> using namespace std; main( ) { 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} } }; /* int x[2][2][2]; x[0][0][0]=1; x[0][0][1]=2; x[0][1][0]=3; x[0][1][1]=4; x[1][0][0]=5; x[1][0][1]=6; x[1][1][0]=7; x[1][1][1]=8; */ cout<<x[0][0][0]<<endl; cout<<x[0][0][1]<<endl; cout<<x[0][1][0]<<endl; cout<<x[0][1][1]<<endl; cout<<x[1][0][0]<<endl; cout<<x[1][0][1]<<endl; cout<<x[1][1][0]<<endl; cout<<x[1][1][1]<<endl; } 25
  • 26. Three Dimensional Array/p2 #include<iostream> using namespace std; main( ) { int x[2][3][4]={ { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }, { {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24} } }; cout<<x[0][0][0]<<endl; cout<<x[0][0][1]<<endl; cout<<x[0][0][2]<<endl; cout<<x[0][0][3]<<endl; cout<<x[0][1][0]<<endl; cout<<x[0][1][1]<<endl; cout<<x[0][1][2]<<endl; cout<<x[0][1][3]<<endl; cout<<x[0][2][0]<<endl; cout<<x[0][2][1]<<endl; cout<<x[0][2][2]<<endl; cout<<x[0][2][3]<<endl; cout<<x[1][0][0]<<endl; 26
  • 28. Three Dimensional Array/p3 #include<iostream> using namespace std; main( ) { int x[3][3][3]={ { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }, { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} }, { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } } , a, b, c; for(a=0; a<3; a++) { cout<<endl; for(b=0;b<3;b++) { cout<<endl; for(c=0;c<3;c++) { cout<<x[a][b][c]; } //cout<<endl; //space between adjacent row } //cout<<endl; // space between row } } 28
  • 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