SlideShare a Scribd company logo
1 of 39
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)

Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
Arrays
ArraysArrays
Arrays
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Array i imp
Array  i impArray  i imp
Array i imp
 
C arrays
C arraysC arrays
C arrays
 
Array
ArrayArray
Array
 
About Array
About ArrayAbout Array
About Array
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 

Recently uploaded

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Recently uploaded (20)

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

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