SlideShare a Scribd company logo
www.cppforschool.com
Array
An array is a collection of data elements of same data type. It is described by a
single name and each element of an array is referenced by using array name
and its subscript no.
Declaration of Array
Type arrayName[numberOfElements];
For example,
int Age[5] ;
float cost[30];
Initialization of One Dimensional Array
An array can be initialized along with declaration. For array initialization it is
required to place the elements separated by commas enclosed within braces.
int A[5] = {11,2,23,4,15};
It is possible to leave the array size open. The compiler will count the array
size.
int B[] = {6,7,8,9,15,12};
Referring to Array Elements
In any point of a program in which an array is visible, we can access the value
of any of its elements individually as if it was a normal variable, thus being able
to both read and modify its value.
The format is as simple as:
name[index]
Examples:
cout << age[4]; //print an array element
age[4] = 55; // assign value to an array element
cin >> age[4]; //input element 4
Using Loop to input an Array from user
int age [10], i ;
for (i = 0 ; i < 10; i++)
{
cin >> age[i];
}
Arrays as Parameters
At some moment we may need to pass an array to a function as a parameter.
In C++ it is not possible to pass a complete block of memory by value as a
parameter to a function, but we are allowed to pass its address.
For example, the following function:
void print(int A[])
accepts a parameter of type "array of int" called A.
In order to pass to this function an array declared as:
int arr[20];
we need to write a call like this:
print(arr);
Here is a complete example:
#include <iostream>
using namespace std;
void print(int A[], int length)
{
for (int n = 0; n < length; n++)
cout << A[n] << " ";
cout << "n";
}
int main ()
{
int arr[] = {5, 10, 15};
print(arr,3);
return 0;
}
Basic Operation On One Dimensional Array
Function to traverse the array A
void display(int A[], int n)
{
cout << "The elements of the array are:n";
for(int i = 0; i < n; i++)
cout << A[i];
}
Function to Read elements of the array A
void Input(int A[], int n)
{
cout << "Enter the elements:";
for(int i = 0; i < n; i++)
cin >> A[i];
}
Function to Search for an element from A by Linear Search
void lsearch(int A[], int n, int data)
{
for(int i = 0; i < n; i++)
{
if(A[i] == data)
{
cout << "Data Found at : " << i;
return;
}
}
cout << "Data Not Found in the array" << endl;
}
Function to Search for an element from Array A by Binary
Search
int BsearchAsc(int A[], int n, int data)
{
int Mid, Lbound = 0, Ubound = n-1, Found=0;
while((Lbound <= Ubound) && !(Found))
{
Mid =(Lbound+Ubound)/2; //Searching The Item
if(data > A[Mid])
Lbound = Mid+1;
else if(data < A[Mid])
Ubound = Mid-1;
else
Found++;
}
if(Found)
return(Mid+1); //returning 1ocation, if present
else
return(-1); //returning -1,if not present
}
Function to Sort the array A by Bubble Sort
void BSort(int A[], int n)
{
int I, J, Temp;
for(I = 0; I < n-1; I++) //sorting
{
for(J = 0; J < (n-1-I); J++)
if(A[J] > A[J+1])
{
Temp = A[J]; //swapping
A[J] = A[J+1];
A[J+1] = Temp;
}
}
}
Function to Sort the array ARR by Insertion Sort
void ISort(int A[], int n)
{
int I, J, Temp;
for(I = 1; I < n; I++) //sorting
{
Temp = A[I];
J = I-1;
while((Temp < A[J]) && (J >= 0))
{
A[J+1] = A[J];
J--;
}
A[J+1]=Temp;
}
}
Function to Sort the array by Selection Sort
void SSort(int A[], int n)
{
int I, J, Temp, Small;
for(I = 0; I < n-1; I++)
{
Small = I;
for(J = I+1; J < n; J++) //finding the smallest element
if(A[J] < A[Small])
Small = J;
if(Small != I)
{
Temp = A[I]; //Swapping
A[I] = A[Small];
A[Small] = Temp;
}
}
}
Function to merge A and B arrays of lenghts N and M
void Merge(int A[], int B[], int C[], int N, int M, int &K)
{
int I = 0, J = 0;
K = 0;
while (I < N && J < M)
{
if (A[I] < B[J])
C[K++] = A[I++];
else if (A[I] > B[J])
C[K++] = B[J++];
else
{
C[K++] = A[I++];
J++;
}
}
int T;
for (T = I; T < N; T++)
C[K++] = A[T];
for (T = J; T < M; T++)
C[K++] = B[T];
}

More Related Content

What's hot

Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
Riki Afriansyah
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
HNDE Labuduwa Galle
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
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 Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Arrays in c
Arrays in cArrays in c
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
Michael Gordon
 
Arrays C#
Arrays C#Arrays C#
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 
Data Structure (Static Array)
Data Structure (Static Array)Data Structure (Static Array)
Data Structure (Static Array)
Adam Mukharil Bachtiar
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Janpreet Singh
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
Data structure array
Data structure  arrayData structure  array
Data structure array
MajidHamidAli
 

What's hot (20)

Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
Array ppt
Array pptArray ppt
Array ppt
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
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
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Array in c
Array in cArray in c
Array in c
 
Data Structure (Static Array)
Data Structure (Static Array)Data Structure (Static Array)
Data Structure (Static Array)
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 

Viewers also liked

Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
Deepak Singh
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
Deepak Singh
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
Deepak Singh
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
Deepak Singh
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
Deepak Singh
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
Deepak Singh
 
Chapter20 class-example-program
Chapter20 class-example-programChapter20 class-example-program
Chapter20 class-example-program
Deepak Singh
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
Deepak Singh
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
Deepak Singh
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
Deepak Singh
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
Deepak Singh
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 

Viewers also liked (16)

Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Chapter20 class-example-program
Chapter20 class-example-programChapter20 class-example-program
Chapter20 class-example-program
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 

Similar to Chapter12 array-single-dimension

SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
NehaJain919374
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Array
ArrayArray
Array
hjasjhd
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Array
ArrayArray
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
11STEM2PGROUP1
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
Qundeel
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
sayalishivarkar1
 
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
 
Array
ArrayArray
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Array data structure
Array data structureArray data structure
Array data structure
harsh112327
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 

Similar to Chapter12 array-single-dimension (20)

SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
02 arrays
02 arrays02 arrays
02 arrays
 
Array
ArrayArray
Array
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Array
ArrayArray
Array
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
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
 
Array
ArrayArray
Array
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Array data structure
Array data structureArray data structure
Array data structure
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 

More from Deepak Singh

Computer networks - CBSE New Syllabus (083) Class - XII
Computer networks - CBSE  New Syllabus (083) Class - XIIComputer networks - CBSE  New Syllabus (083) Class - XII
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
Deepak Singh
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
Deepak Singh
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
Deepak Singh
 
Chapter17 oop
Chapter17 oopChapter17 oop
Chapter17 oop
Deepak Singh
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
Deepak Singh
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
Deepak Singh
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
Deepak Singh
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
Deepak Singh
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
Deepak Singh
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
Deepak Singh
 

More from Deepak Singh (14)

Computer networks - CBSE New Syllabus (083) Class - XII
Computer networks - CBSE  New Syllabus (083) Class - XIIComputer networks - CBSE  New Syllabus (083) Class - XII
Computer networks - CBSE New Syllabus (083) Class - XII
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
 
Chapter17 oop
Chapter17 oopChapter17 oop
Chapter17 oop
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 

Chapter12 array-single-dimension

  • 1. www.cppforschool.com Array An array is a collection of data elements of same data type. It is described by a single name and each element of an array is referenced by using array name and its subscript no. Declaration of Array Type arrayName[numberOfElements]; For example, int Age[5] ; float cost[30]; Initialization of One Dimensional Array An array can be initialized along with declaration. For array initialization it is required to place the elements separated by commas enclosed within braces. int A[5] = {11,2,23,4,15}; It is possible to leave the array size open. The compiler will count the array size. int B[] = {6,7,8,9,15,12}; Referring to Array Elements In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value.
  • 2. The format is as simple as: name[index] Examples: cout << age[4]; //print an array element age[4] = 55; // assign value to an array element cin >> age[4]; //input element 4 Using Loop to input an Array from user int age [10], i ; for (i = 0 ; i < 10; i++) { cin >> age[i]; } Arrays as Parameters At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. For example, the following function: void print(int A[]) accepts a parameter of type "array of int" called A. In order to pass to this function an array declared as: int arr[20]; we need to write a call like this: print(arr); Here is a complete example: #include <iostream> using namespace std; void print(int A[], int length) { for (int n = 0; n < length; n++) cout << A[n] << " "; cout << "n"; }
  • 3. int main () { int arr[] = {5, 10, 15}; print(arr,3); return 0; } Basic Operation On One Dimensional Array Function to traverse the array A void display(int A[], int n) { cout << "The elements of the array are:n"; for(int i = 0; i < n; i++) cout << A[i]; } Function to Read elements of the array A void Input(int A[], int n) { cout << "Enter the elements:"; for(int i = 0; i < n; i++) cin >> A[i]; } Function to Search for an element from A by Linear Search void lsearch(int A[], int n, int data) { for(int i = 0; i < n; i++) { if(A[i] == data) { cout << "Data Found at : " << i; return; } } cout << "Data Not Found in the array" << endl; }
  • 4. Function to Search for an element from Array A by Binary Search int BsearchAsc(int A[], int n, int data) { int Mid, Lbound = 0, Ubound = n-1, Found=0; while((Lbound <= Ubound) && !(Found)) { Mid =(Lbound+Ubound)/2; //Searching The Item if(data > A[Mid]) Lbound = Mid+1; else if(data < A[Mid]) Ubound = Mid-1; else Found++; } if(Found) return(Mid+1); //returning 1ocation, if present else return(-1); //returning -1,if not present } Function to Sort the array A by Bubble Sort void BSort(int A[], int n) { int I, J, Temp; for(I = 0; I < n-1; I++) //sorting { for(J = 0; J < (n-1-I); J++) if(A[J] > A[J+1]) { Temp = A[J]; //swapping A[J] = A[J+1]; A[J+1] = Temp; } } }
  • 5. Function to Sort the array ARR by Insertion Sort void ISort(int A[], int n) { int I, J, Temp; for(I = 1; I < n; I++) //sorting { Temp = A[I]; J = I-1; while((Temp < A[J]) && (J >= 0)) { A[J+1] = A[J]; J--; } A[J+1]=Temp; } } Function to Sort the array by Selection Sort void SSort(int A[], int n) { int I, J, Temp, Small; for(I = 0; I < n-1; I++) { Small = I; for(J = I+1; J < n; J++) //finding the smallest element if(A[J] < A[Small]) Small = J; if(Small != I) { Temp = A[I]; //Swapping A[I] = A[Small]; A[Small] = Temp; } } }
  • 6. Function to merge A and B arrays of lenghts N and M void Merge(int A[], int B[], int C[], int N, int M, int &K) { int I = 0, J = 0; K = 0; while (I < N && J < M) { if (A[I] < B[J]) C[K++] = A[I++]; else if (A[I] > B[J]) C[K++] = B[J++]; else { C[K++] = A[I++]; J++; } } int T; for (T = I; T < N; T++) C[K++] = A[T]; for (T = J; T < M; T++) C[K++] = B[T]; }