SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Arrays
An array is a collection of similar data elements stored at contiguous memory
locations. It is the simplest data structure where each data element can be accessed
directly by only using its index number.
Example:
if we want to store the marks scored by a student in 5 subjects, then there’s no need
to define individual variables for each subject. Rather, we can define an array that
will store the data elements at contiguous memory locations. Array marks [5]
define the marks scored by a student in 5 different subjects where each subject’s
marks are located at a particular location in the array, i.e., marks [0] denote the
marks scored in the first subject, marks [1] denotes the marks scored in 2nd
subject and so on.
Basic Operations of arrays:
• Traverse - print all the array elements one by one.
• Insertion - Adds an element at the given index.
• Deletion - Deletes an element at the given index.
• Searching - Searches an element using the given index or by the value.
Traversing:
Print all the array elements one by one.
Program:
#include <iostream>
using namespace std;
int main()
{
int arr[9] = { 7,11,6,55,98,45,16,96,46 };
cout << "....Taversing...." << endl;
for (int i = 0; i < 9;i++)
{
cout << arr[i]<<" ";
}
return 0;
}
Output:
Insertion:
Adds an element at the given index.
Program:
#include <iostream>
using namespace std;
int main()
{
int p,ele, arr[9] = { 7,11,6,55,98,45,16,96,46 };
cout << "....Taversing...." << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << "nEnter position to insert an element in the array:t";
cin >> p;
cout << "nEnter Element to insert:t";
cin >> ele;
for (int i = 9; i > p; i--)
{
arr[i] = arr[i - 1];
}
arr[p] = ele;
cout << "Array After Insertion of New Element:t";
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
return 0;
}
Output:
Deletion:
Deletes an element at the given index.
Program:
#include<iostream>
using namespace std;
int main()
{
int arr[10], tot = 10, i, elem, j, found = 0;
cout << "Enter 10 Array Elements: ";
for (i = 0; i < tot; i++)
{
cin >> arr[i];
}
cout << "nEnter Element to Delete: ";
cin >> elem;
for (i = 0; i < tot; i++)
{
if (arr[i] == elem)
{
for (j = i; j < (tot - 1); j++)
arr[j] = arr[j + 1];
found++;
i--;
tot--;
}
}
if (found == 0)
cout << "nElement doesn't found in the Array!";
else
cout << "nElement Deleted Successfully!";
cout << endl;
cout << "nArray After Deletion:t";
for(int i = 0; i < tot; i++)
{
cout << arr[i]<<" ";
}
return 0;
}
Output:
Searching:
Searches an element using the given index or by the value.
Program:
#include<iostream>
using namespace std;
int main()
{
int n, k, ans = -1;
int arr[100];
cout << "Enter size of array" << endl;
cin >> n;
cout << "Enter elements of array" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Enter element to be searched" << endl;
cin >> k;
for (int i = 0; i < n; i++)
{
if (arr[i] == k)
{
ans = i;
break;
}
}
if (ans != -1)
cout << "The element " << k << " is present at index " << ans;
else
cout << "The element " << k << " is not there in the array";
return 0;
}
Output:
C++ Arrays different operations .pdf

More Related Content

PPTX
Stacks in c++
PPTX
Constructor and destructor in oop
PPTX
Introduction to Object Oriented Programming
PPTX
Chapter 07 inheritance
PPT
Queue AS an ADT (Abstract Data Type)
PPT
Queue Data Structure
PPTX
Inheritance in java
PPTX
The Stack And Recursion
Stacks in c++
Constructor and destructor in oop
Introduction to Object Oriented Programming
Chapter 07 inheritance
Queue AS an ADT (Abstract Data Type)
Queue Data Structure
Inheritance in java
The Stack And Recursion

What's hot (20)

PDF
C++ Files and Streams
PPTX
Double Linked List (Algorithm)
PPTX
linked list in data structure
PDF
Exception handling
PPTX
PPT
Java Notes
PDF
Java Thread Synchronization
PPTX
Interface in java
PPTX
Queue - Data Structure - Notes
PPT
Data structure lecture 1
PPTX
Circular link list.ppt
PPTX
PPT
Files in c++ ppt
PPTX
single linked list
PPTX
PPTX
JAVA-PPT'S.pptx
PPTX
Constructor in java
PPTX
Templates in C++
PPT
Abstract data types
PPTX
Exception handling
C++ Files and Streams
Double Linked List (Algorithm)
linked list in data structure
Exception handling
Java Notes
Java Thread Synchronization
Interface in java
Queue - Data Structure - Notes
Data structure lecture 1
Circular link list.ppt
Files in c++ ppt
single linked list
JAVA-PPT'S.pptx
Constructor in java
Templates in C++
Abstract data types
Exception handling
Ad

Similar to C++ Arrays different operations .pdf (20)

PPTX
Data structures and algorithms arrays
PDF
PPTX
CC-104_Lesson-2_array.pptx. introduction
PPTX
DSA - Array.pptx
PDF
Notes-10-Array.pdf
PPTX
arrays in c programming - example programs
PPT
PPTX
Module_3_Arrays - Updated.pptx............
PPTX
6_Array.pptx
PPTX
CSE 1102 - Lecture 6 - Arrays in C .pptx
PPTX
arrays-120712074248-phpapp01
PPTX
array lecture engineeringinformatin_technology.pptx
PDF
Array (data structure using c++).PPT presentation
PPTX
Arrays_in_c++.pptx
PPT
Arrays in C++
PPTX
Arrays in Data
PDF
1-Intoduction ------------- Array in C++
PPTX
Data structures in c#
PDF
SlideSet_4_Arraysnew.pdf
PPTX
JavaScript.pptx
Data structures and algorithms arrays
CC-104_Lesson-2_array.pptx. introduction
DSA - Array.pptx
Notes-10-Array.pdf
arrays in c programming - example programs
Module_3_Arrays - Updated.pptx............
6_Array.pptx
CSE 1102 - Lecture 6 - Arrays in C .pptx
arrays-120712074248-phpapp01
array lecture engineeringinformatin_technology.pptx
Array (data structure using c++).PPT presentation
Arrays_in_c++.pptx
Arrays in C++
Arrays in Data
1-Intoduction ------------- Array in C++
Data structures in c#
SlideSet_4_Arraysnew.pdf
JavaScript.pptx
Ad

Recently uploaded (20)

PPTX
Derivatives of integument scales, beaks, horns,.pptx
PDF
HPLC-PPT.docx high performance liquid chromatography
PPTX
microscope-Lecturecjchchchchcuvuvhc.pptx
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
Comparative Structure of Integument in Vertebrates.pptx
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PPTX
BIOMOLECULES PPT........................
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
PDF
. Radiology Case Scenariosssssssssssssss
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PPTX
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
PDF
AlphaEarth Foundations and the Satellite Embedding dataset
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PPTX
2. Earth - The Living Planet Module 2ELS
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PDF
The scientific heritage No 166 (166) (2025)
DOCX
Viruses (History, structure and composition, classification, Bacteriophage Re...
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
Derivatives of integument scales, beaks, horns,.pptx
HPLC-PPT.docx high performance liquid chromatography
microscope-Lecturecjchchchchcuvuvhc.pptx
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
Comparative Structure of Integument in Vertebrates.pptx
The KM-GBF monitoring framework – status & key messages.pptx
BIOMOLECULES PPT........................
ECG_Course_Presentation د.محمد صقران ppt
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
. Radiology Case Scenariosssssssssssssss
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
AlphaEarth Foundations and the Satellite Embedding dataset
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
2. Earth - The Living Planet Module 2ELS
Biophysics 2.pdffffffffffffffffffffffffff
The scientific heritage No 166 (166) (2025)
Viruses (History, structure and composition, classification, Bacteriophage Re...
Classification Systems_TAXONOMY_SCIENCE8.pptx

C++ Arrays different operations .pdf

  • 1. Arrays An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number. Example: if we want to store the marks scored by a student in 5 subjects, then there’s no need to define individual variables for each subject. Rather, we can define an array that will store the data elements at contiguous memory locations. Array marks [5] define the marks scored by a student in 5 different subjects where each subject’s marks are located at a particular location in the array, i.e., marks [0] denote the marks scored in the first subject, marks [1] denotes the marks scored in 2nd subject and so on. Basic Operations of arrays: • Traverse - print all the array elements one by one. • Insertion - Adds an element at the given index. • Deletion - Deletes an element at the given index. • Searching - Searches an element using the given index or by the value.
  • 2. Traversing: Print all the array elements one by one. Program: #include <iostream> using namespace std; int main() { int arr[9] = { 7,11,6,55,98,45,16,96,46 }; cout << "....Taversing...." << endl; for (int i = 0; i < 9;i++) { cout << arr[i]<<" "; } return 0; } Output:
  • 3. Insertion: Adds an element at the given index. Program: #include <iostream> using namespace std; int main() { int p,ele, arr[9] = { 7,11,6,55,98,45,16,96,46 }; cout << "....Taversing...." << endl; for (int i = 0; i < 9; i++) { cout << arr[i] << " "; } cout << "nEnter position to insert an element in the array:t"; cin >> p; cout << "nEnter Element to insert:t"; cin >> ele; for (int i = 9; i > p; i--) { arr[i] = arr[i - 1]; } arr[p] = ele; cout << "Array After Insertion of New Element:t"; for (int i = 0; i < 10; i++) { cout << arr[i] << " "; } return 0; } Output:
  • 4. Deletion: Deletes an element at the given index. Program: #include<iostream> using namespace std; int main() { int arr[10], tot = 10, i, elem, j, found = 0; cout << "Enter 10 Array Elements: "; for (i = 0; i < tot; i++) { cin >> arr[i]; } cout << "nEnter Element to Delete: "; cin >> elem; for (i = 0; i < tot; i++) { if (arr[i] == elem) { for (j = i; j < (tot - 1); j++) arr[j] = arr[j + 1]; found++; i--; tot--; } } if (found == 0) cout << "nElement doesn't found in the Array!"; else cout << "nElement Deleted Successfully!"; cout << endl; cout << "nArray After Deletion:t"; for(int i = 0; i < tot; i++) { cout << arr[i]<<" "; } return 0; }
  • 6. Searching: Searches an element using the given index or by the value. Program: #include<iostream> using namespace std; int main() { int n, k, ans = -1; int arr[100]; cout << "Enter size of array" << endl; cin >> n; cout << "Enter elements of array" << endl; for (int i = 0; i < n; i++) { cin >> arr[i]; } cout << "Enter element to be searched" << endl; cin >> k; for (int i = 0; i < n; i++) { if (arr[i] == k) { ans = i; break; } } if (ans != -1) cout << "The element " << k << " is present at index " << ans; else cout << "The element " << k << " is not there in the array"; return 0; } Output: