You are on page 1of 14

1

Lecture #8

Operations in the Arrays

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


2
Array Operation in C/C++
 Traversal
 Insertion
 Deletion
 Search
 Updation
are the major types of operations that can be performed on arrays.

Traversal
Traversal in an array means printing the elements of the array one by one. This means you are
traversing through the elements of the array one after the other. A simple program for traversal in
an array is shown below.
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
3 Traversal of Array

# include<iostream>
using namespace std;
int main()
{
int arr[10] = {1,5,7,9,2,4,6,8,10,15};
int i;
cout<<"The array elements are \n";
for(i = 0 ; i < 10 ; i++)
{ Output:
cout<<arr[i]<<" ";
The array elements are
} 1 5 7 9 2 4 6 8 10 15
return 0;
} © LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
4 Insertion:

Insertion operation is used to insert an element into the array. Insertion of


an element can happen at the beginning, middle or at the end of the array.
#include <iostream> for (i = n; i >= pos; i--)
using namespace std; {
int main() arr[i] = arr[i - 1];
{ }
int arr[] = {1, 2, 3, 4, 5, 6}; arr[i] = insElem;
int n = sizeof(arr)/sizeof(arr[0]);
int insElem, pos, i; //Display new array
cout<<"The original array elements are :\n"; cout << "New Array after Insertion:\n";
for(i = 0 ; i < n ; i++) for (i = 0; i < (n + 1); i++)
{ cout << arr[i] << " ";
cout<<"arr="<<i<<":"<<arr[i]<<endl;
} return 0;
cout << "\nEnter element to be inserted: "; }
cin >> insElem;
cout << "Enter the position: "; //position specified
cin >> pos;

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


5 Insertion Output:

The original array elements are : The array elements after insertion :
arr[0] = 1 arr[0] = 1
arr[1] = 2 arr[1] = 2
arr[2] = 3 arr[2] = 33
arr[3] = 4 arr[3] = 3
arr[4] = 5 arr[4] = 4
arr[5] = 6 arr[5] = 5
arr[6] = 6

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


6
Deletion

Deletion operation is used to delete an element from the array. A simple program to perform deletion in an
array is shown below. for(i=0; i<n; i++)
{
#include <iostream>
if(arr[i]==elem)
using namespace std; {
int main() for(j=i; j<(n-1); j++)
{ arr[j] = arr[j+1];
int arr[ ] = {7,4,5,9,8,15}; found=1;
int n = sizeof(arr)/sizeof(arr[0]); n--;
int elem = 9, found=0; }
}
int i, j;
if(found==0)
cout<<"The original array elements are :\n"; cout<<"\nElement doesn't found in the Array!"<<endl;
for(i = 0; i<n; i++) else
{ {
cout<<"arr:"<<i<<"="<<arr[ i ]<<endl; cout<<"The array elements after deletion :\n";
} for(i = 0; i<n; i++)
{
cout<<"arr"<<i<<"="<<arr[i]<<endl;
}
}
return 0;
} © LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
7

The original array elements are : The array elements after deletion :
arr[0] = 7 arr[0] = 7
arr[1] = 4 arr[1] = 4
face[2] = 5 arr[2] = 9
arr[3] = 9 arr[3] = 8
arr[4] = 8 arr[4] = 15
arr[5] = 15

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


8 Searching

A search operation is used to search for an element in the array. It can also be used to search
for an element greater than or less than the specified element. A simple program to perform
this operation is shown below.
while(j <= 5)
{
#include <iostream>
if( face[ j ] == item )
using namespace std;
{
int main()
flag=1;
{
break;
int face[ ] = {7,4,5,9,8,15};
}
int j=0, flag, item = 9;
j++;
flag=0;
}
cout << "The original array elements are :\n";
if(flag==1)
for(int i = 0; i <= 5; i++)
cout << "Found element " << item << " at index " << j << endl;
{
else
cout << "face[" << i << "] = " << face[ i ] << endl;
cout<<"Not Found";
}
return 0;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


9 Write a program in C++ to Reverse of an arrays

#include <iostream>
using namespace std;

int main() {
// Reverse an integer array
int myArray[] = {1, 2, 3, 4, 5};
int size = sizeof(myArray) / sizeof(myArray[0]); Output:
int i;
for(i=size-1;i>=0;i--) 54321
{
cout<<myArray[i]<<"\t";
}
return 0;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


10 Write a program in C++ for Merging of two arrays

#include <iostream>
using namespace std;

int main() {
int size1 = 5;
int size2 = 3;

int arr1[size1] = {1, 2, 3, 4, 5};


int arr2[size2] = {6, 7, 8}; Output:
int merged[size1 + size2];
12345678
for (int i = 0; i < size1; ++i) {
merged[i] = arr1[i];
}

for (int i = 0; i < size2; ++i) {


merged[size1 + i] = arr2[i];
}

for (int i = 0; i < size1 + size2; ++i) {


cout << merged[i] << " ";
}
return 0;
} © LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
11 Write a program in C++ for Concatenation of two array Strings

#include <iostream> // Copy characters from str2 to concatenated


#include <cstring> // For strlen and strcpy for (int i = 0; i < len2; ++i) {
using namespace std; concatenated[len1 + i] = str2[i];
}
int main() {
char str1[] = "Hello, "; // Null-terminate the concatenated string
char str2[] = "World!"; concatenated[len1 + len2] = '\0';

// Calculate the lengths of the strings // Print the concatenated string


int len1 = strlen(str1); cout << concatenated << endl;
int len2 = strlen(str2);
return 0;
// Create a new character array with enough space for both strings }
char concatenated[len1 + len2 + 1]; // +1 for the null terminator

// Copy characters from str1 to concatenated


for (int i = 0; i < len1; ++i) {
concatenated[i] = str1[i];
Output: Hello, World!
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


12 Updating

Updating refers to updating an existing array element at a particular value to a new element value.

#include <iostream> if(flag==1)


using namespace std; {
int main() { for(int i=0;i<=n-1;i++)
int arr[] = {7,4,5,9,8,15}; {
int n = sizeof(arr)/sizeof(arr[0]); cout<<arr[i]<<"\t";
int ele = 5; }
int newValue = 17; }
int flag=0; else
for(int i=0;i<=n-1;i++) {
{ cout<<"Element Not Found";
if(arr[i]==ele) }
{ return 0;
arr[i]=newValue; }
flag=1;
}
} The array elements after updation :
arr[0] = 7
arr[1] = 4
arr[2] = 17
arr[3] = 9
arr[4] = 8
arr[5] = 15 © LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
13

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


14

Thank You

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh

You might also like