SlideShare a Scribd company logo
Object Oriented Programming
Arrays
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
arr [0]
arr [1]
arr [2]
arr [3]
arr [4]
arr [5]
arr [6]
arr [7]
arr [8]
arr [9]
int arr[ 10];
arr reference
An array of 10 elements
of type int
Array position starts
from 0
Declaring Array Variables
 Datatype arrayname[arraysize];
Example:
int numbers[10];
 datatype arrayname[arraysize]={arrayvalue};
Example:
int numbers[5]={2,3,4,5,6};
Once an array is created, its size is fixed. It cannot
be changed. You can find its size using
Initializing Arrays
 Using a loop:
for (int i = 0; i < 4; i++)
myList[i] = i;
 Declaring & initializing in one step:
double myList[] = {1.9, 2.9, 3.4, 3.5};
Declaring & initializing in one step
double myList[] = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the
following statements:
double myList[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
Example 1
Write a pogram that read and display an array
int main()
{
int a[5], i;
for(i=0;i<5;i++) {
cin>>a[i];
}
for(i=0;i<5;i++) {
cout<<a[i];
}
return 0;
}
Example 2
Find the largest of 10 numbers using array.
void main()
{
int i, a[20],max=0;
for (i=1; i< 10; i++)
{
cout<<“enter number n”;
cin>>a[i];
If (a[i] > max) max = a[i];
}
cout<<max;
}
Example 3
Write a pogram that reads some values and display
average
void main()
{
int i, a[20],sum=0,avg;
for (i=1; i< 10; i++)
{
cout<<“enter number n”;
cin>>a[i];
sum=sum+a[i];
}
avg=sum/10;
cout<<“average is”<<avg;
}
Example 4
Write a program that searches any number from an array
int main()
{
int i,n,a[100],num,found;
cout<<“How many numbers: ";
cin>>n;
for(i=0; i<n; i++){
cin>>a[i];
}
cout<<“Enter any number to search: ";
cin>>num;
Example 4
for(i=0; i<n; i++)
{
if(a[i]==num)
{
cout<<“Found.";
break;
}
}
if(i==n)
cout<<“Not found.”;
return 0;
}
Copying Arrays
A simple assignment cannot copy arrays. Rather simply
creates two arrays and attempts to copy one to the
other, using an assignment statement.
int list1[] = {2, 3, 1, 5, 10};
int list2[] = {1, 5, 7, 8, 9};
for (int i = 0; i < 5; i++)
list2[i] = list1[i];
Copying Arrays
Contents
of list1
list1
Contents
of list2
list2
Before the assignment
list2 = list1;
Contents
of list1
list1
Contents
of list2
list2
After the assignment
list2 = list1;
Garbage
Multidimensional Arrays
Declaring Variables of Multidimensional Arrays and Creating
Multidimensional Arrays
int array[5][4];
or
int array[][4];
Assigning value at[0][2]position: array[0][2] = 3;
for (i=0; i<5; i++)
for (j=0; j<4; j++)
{
array[i][j] = rand()*1000;
}
double[][] x;
Multidimensional Array Illustration
0 1 2 3 4
0
7
0 1 2 3 4
1
2
3
4
0
1
2
3
4
int arr[2][1] = 7;int arr[5][5];
3
7
0 1 2
0
1
2
int arr[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
1 2 3
4 5 6
8 9
10 11 12
Declaring, Creating, and Initializing
You can also use a shorthand notation to declare, create and initialize a
two-dimensional array. For example,
int array[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
This is equivalent to the following statements:
int array[4][3];
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
Ragged Arrays
Each row in a two-dimensional array is itself an array. So, the rows
can have different lengths. Such an array is known as a ragged
array.
For example,
int matrix[][] = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};
Example 6
Write a program to print a matrix.
int main()
{
int i,j,a[10][10];
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cin>>a[i][j];
}
}
Example 6
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<a[i][j];
}
cout<<“n”;
}
return 0;
}

More Related Content

What's hot

Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
Muthukumaran Subramanian
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
Hareem Naz
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Array in-c
Array in-cArray in-c
Array in-c
Samsil Arefin
 
Arrays in C
Arrays in CArrays in C
Arrays in C
Kamruddin Nur
 
Array in c language
Array in c language Array in c language
Array in c language
umesh patil
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 

What's hot (20)

Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array in-c
Array in-cArray in-c
Array in-c
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Array in c language
Array in c language Array in c language
Array in c language
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Array lecture
Array lectureArray lecture
Array lecture
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 

Viewers also liked

Ch1- Introduction to dbms
Ch1- Introduction to dbmsCh1- Introduction to dbms
Ch1- Introduction to dbms
Shakila Mahjabin
 
Computer processing
Computer processingComputer processing
Computer processing
Shakila Mahjabin
 
Normalization
NormalizationNormalization
Normalization
Shakila Mahjabin
 
String operation
String operationString operation
String operation
Shakila Mahjabin
 
Arrays
ArraysArrays
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
Shakila Mahjabin
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
Shakila Mahjabin
 

Viewers also liked (8)

Ch1- Introduction to dbms
Ch1- Introduction to dbmsCh1- Introduction to dbms
Ch1- Introduction to dbms
 
Computer processing
Computer processingComputer processing
Computer processing
 
Normalization
NormalizationNormalization
Normalization
 
String operation
String operationString operation
String operation
 
Arrays
ArraysArrays
Arrays
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 

Similar to Arrays in CPP

Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
MithuBose3
 
Arrays
ArraysArrays
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
surajthakur474818
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Md. Imran Hossain Showrov
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
FareedIhsas
 
Data structure array
Data structure  arrayData structure  array
Data structure array
MajidHamidAli
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
pptt.pptx
pptt.pptxpptt.pptx
Array
ArrayArray
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
ssuser6478a8
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
coding9
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
ssuser99ca78
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
Ferdin Joe John Joseph PhD
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 

Similar to Arrays in CPP (20)

Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
Arrays
ArraysArrays
Arrays
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Array
ArrayArray
Array
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
pptt.pptx
pptt.pptxpptt.pptx
pptt.pptx
 
Array
ArrayArray
Array
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 

More from Shakila Mahjabin

CSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL QuestionCSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL Question
Shakila Mahjabin
 
Solution of Erds
Solution of ErdsSolution of Erds
Solution of Erds
Shakila Mahjabin
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Shakila Mahjabin
 
Algo analysis
Algo analysisAlgo analysis
Algo analysis
Shakila Mahjabin
 
Codes on structures
Codes on structuresCodes on structures
Codes on structures
Shakila Mahjabin
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
Shakila Mahjabin
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
Shakila Mahjabin
 

More from Shakila Mahjabin (7)

CSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL QuestionCSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL Question
 
Solution of Erds
Solution of ErdsSolution of Erds
Solution of Erds
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Algo analysis
Algo analysisAlgo analysis
Algo analysis
 
Codes on structures
Codes on structuresCodes on structures
Codes on structures
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 

Recently uploaded

Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 

Recently uploaded (20)

Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 

Arrays in CPP

  • 2. Introducing Arrays Array is a data structure that represents a collection of the same types of data. arr [0] arr [1] arr [2] arr [3] arr [4] arr [5] arr [6] arr [7] arr [8] arr [9] int arr[ 10]; arr reference An array of 10 elements of type int Array position starts from 0
  • 3. Declaring Array Variables  Datatype arrayname[arraysize]; Example: int numbers[10];  datatype arrayname[arraysize]={arrayvalue}; Example: int numbers[5]={2,3,4,5,6}; Once an array is created, its size is fixed. It cannot be changed. You can find its size using
  • 4. Initializing Arrays  Using a loop: for (int i = 0; i < 4; i++) myList[i] = i;  Declaring & initializing in one step: double myList[] = {1.9, 2.9, 3.4, 3.5};
  • 5. Declaring & initializing in one step double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
  • 6. Example 1 Write a pogram that read and display an array int main() { int a[5], i; for(i=0;i<5;i++) { cin>>a[i]; } for(i=0;i<5;i++) { cout<<a[i]; } return 0; }
  • 7. Example 2 Find the largest of 10 numbers using array. void main() { int i, a[20],max=0; for (i=1; i< 10; i++) { cout<<“enter number n”; cin>>a[i]; If (a[i] > max) max = a[i]; } cout<<max; }
  • 8. Example 3 Write a pogram that reads some values and display average void main() { int i, a[20],sum=0,avg; for (i=1; i< 10; i++) { cout<<“enter number n”; cin>>a[i]; sum=sum+a[i]; } avg=sum/10; cout<<“average is”<<avg; }
  • 9. Example 4 Write a program that searches any number from an array int main() { int i,n,a[100],num,found; cout<<“How many numbers: "; cin>>n; for(i=0; i<n; i++){ cin>>a[i]; } cout<<“Enter any number to search: "; cin>>num;
  • 10. Example 4 for(i=0; i<n; i++) { if(a[i]==num) { cout<<“Found."; break; } } if(i==n) cout<<“Not found.”; return 0; }
  • 11. Copying Arrays A simple assignment cannot copy arrays. Rather simply creates two arrays and attempts to copy one to the other, using an assignment statement. int list1[] = {2, 3, 1, 5, 10}; int list2[] = {1, 5, 7, 8, 9}; for (int i = 0; i < 5; i++) list2[i] = list1[i];
  • 12. Copying Arrays Contents of list1 list1 Contents of list2 list2 Before the assignment list2 = list1; Contents of list1 list1 Contents of list2 list2 After the assignment list2 = list1; Garbage
  • 13. Multidimensional Arrays Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int array[5][4]; or int array[][4]; Assigning value at[0][2]position: array[0][2] = 3; for (i=0; i<5; i++) for (j=0; j<4; j++) { array[i][j] = rand()*1000; } double[][] x;
  • 14. Multidimensional Array Illustration 0 1 2 3 4 0 7 0 1 2 3 4 1 2 3 4 0 1 2 3 4 int arr[2][1] = 7;int arr[5][5]; 3 7 0 1 2 0 1 2 int arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; 1 2 3 4 5 6 8 9 10 11 12
  • 15. Declaring, Creating, and Initializing You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example, int array[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; This is equivalent to the following statements: int array[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
  • 16. Ragged Arrays Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int matrix[][] = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };
  • 17. Example 6 Write a program to print a matrix. int main() { int i,j,a[10][10]; for(i=0;i<3;i++){ for(j=0;j<2;j++){ cin>>a[i][j]; } }