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

PDF
Array data structure
PPTX
PPT
Arrays
PPT
Array
PDF
PPT
2 arrays
PPTX
C++ programming (Array)
Array data structure
Arrays
Array
2 arrays
C++ programming (Array)

What's hot (20)

PPTX
Array BPK 2
PPTX
Array ppt
PPTX
Data Structures - Lecture 3 [Arrays]
PPTX
Arrays in C language
PPTX
C++ lecture 04
PPTX
Programming in c Arrays
PPTX
Array Introduction One-dimensional array Multidimensional array
PPT
Array Presentation (EngineerBaBu.com)
PDF
Arrays in python
PPT
Java: Introduction to Arrays
PDF
Arrays, continued
PPTX
PPT
Array in c
PDF
Data Structure (Static Array)
PPTX
Computer programming 2 Lesson 13
PDF
Arrays In Python | Python Array Operations | Edureka
PPT
Arrays in C++
PPTX
Arrays In C++
PPTX
Data structure array
Array BPK 2
Array ppt
Data Structures - Lecture 3 [Arrays]
Arrays in C language
C++ lecture 04
Programming in c Arrays
Array Introduction One-dimensional array Multidimensional array
Array Presentation (EngineerBaBu.com)
Arrays in python
Java: Introduction to Arrays
Arrays, continued
Array in c
Data Structure (Static Array)
Computer programming 2 Lesson 13
Arrays In Python | Python Array Operations | Edureka
Arrays in C++
Arrays In C++
Data structure array
Ad

Viewers also liked (16)

PDF
Chapter16 pointer
PDF
Chapter25 inheritance-i
PDF
Chapter19 constructor-and-destructor
PDF
Chapter18 class-and-objects
PDF
Chapter15 structure
PDF
Chapter 8 - Conditional Statement
PDF
Chapter26 inheritance-ii
PDF
Chapter20 class-example-program
PDF
Chapter21 separate-header-and-implementation-files
PDF
Chapter 2 - Structure of C++ Program
PDF
Computer science-2010-cbse-question-paper
PDF
Chapter 5 - Operators in C++
PDF
Chapter 10 Library Function
PDF
Chapter23 friend-function-friend-class
PDF
Chapter22 static-class-member-example
PDF
Chapter27 polymorphism-virtual-function-abstract-class
Chapter16 pointer
Chapter25 inheritance-i
Chapter19 constructor-and-destructor
Chapter18 class-and-objects
Chapter15 structure
Chapter 8 - Conditional Statement
Chapter26 inheritance-ii
Chapter20 class-example-program
Chapter21 separate-header-and-implementation-files
Chapter 2 - Structure of C++ Program
Computer science-2010-cbse-question-paper
Chapter 5 - Operators in C++
Chapter 10 Library Function
Chapter23 friend-function-friend-class
Chapter22 static-class-member-example
Chapter27 polymorphism-virtual-function-abstract-class
Ad

Similar to Chapter12 array-single-dimension (20)

PPTX
ARRAYS.pptx
PPTX
Arrays matrix 2020 ab
PPT
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPTX
Arrays_in_c++.pptx
PPT
its arrays ppt for first year students .
PPT
lecture7.ppt
PDF
Chapter13 two-dimensional-array
PPT
Fp201 unit4
PPTX
C-Programming Arrays.pptx
PPTX
C-Programming Arrays.pptx
PPTX
Arrays
PDF
11. Programming(BS-phy6)-Lecture11+12 .pdf
PPT
C++ Arrays
PPT
C++ Arrays
PPTX
The document discusses arrays in data structures using Cpp programming langua...
PPTX
Structured data type
PDF
Data structures arrays
PDF
C++ L04-Array+String
PPT
2DArrays.ppt
ARRAYS.pptx
Arrays matrix 2020 ab
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Arrays_in_c++.pptx
its arrays ppt for first year students .
lecture7.ppt
Chapter13 two-dimensional-array
Fp201 unit4
C-Programming Arrays.pptx
C-Programming Arrays.pptx
Arrays
11. Programming(BS-phy6)-Lecture11+12 .pdf
C++ Arrays
C++ Arrays
The document discusses arrays in data structures using Cpp programming langua...
Structured data type
Data structures arrays
C++ L04-Array+String
2DArrays.ppt

More from Deepak Singh (13)

PDF
Computer networks - CBSE New Syllabus (083) Class - XII
PDF
Chpater29 operation-on-file
PDF
Chapter28 data-file-handling
PDF
Chapter24 operator-overloading
PDF
Chapter17 oop
PDF
Chapter 11 Function
PDF
Chapter 9 - Loops in C++
PDF
Chapter 7 - Input Output Statements in C++
PDF
Chapter 3 - Variable Memory Concept
PPT
Inheritance polymorphism-in-java
PDF
Cbse question-paper-computer-science-2009
PDF
CBSE Question Paper Computer Science with C++ 2011
PDF
Revision notes for exam 2011 computer science with C++
Computer networks - CBSE New Syllabus (083) Class - XII
Chpater29 operation-on-file
Chapter28 data-file-handling
Chapter24 operator-overloading
Chapter17 oop
Chapter 11 Function
Chapter 9 - Loops in C++
Chapter 7 - Input Output Statements in C++
Chapter 3 - Variable Memory Concept
Inheritance polymorphism-in-java
Cbse question-paper-computer-science-2009
CBSE Question Paper Computer Science with C++ 2011
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]; }