C++ Arrays different operations .pdf

Arrays traversing, insertion, deletion and searching with C++ program and output

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

Recomendados

Arrays in Java von
Arrays in JavaArrays in Java
Arrays in JavaNaz Abdalla
2K views22 Folien
Array in Java von
Array in JavaArray in Java
Array in JavaAli shah
1.8K views12 Folien
Abstract data types von
Abstract data typesAbstract data types
Abstract data typesLuis Goldster
3K views12 Folien
Introduction to class in java von
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
16.3K views58 Folien
Introduction to Array ppt von
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
6.8K views15 Folien
Java script errors &amp; exceptions handling von
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handlingAbhishekMondal42
352 views16 Folien

Más contenido relacionado

Was ist angesagt?

Arrays von
ArraysArrays
ArraysTrupti Agrawal
3.3K views18 Folien
Chapter2 Encapsulation (Java) von
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Dyah Fajar Nur Rohmah
3.7K views10 Folien
Awt, Swing, Layout managers von
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
292 views55 Folien
Class and Objects in Java von
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
7K views14 Folien
Java Generics - by Example von
Java Generics - by ExampleJava Generics - by Example
Java Generics - by ExampleCodeOps Technologies LLP
10K views49 Folien
Encapsulation von
EncapsulationEncapsulation
EncapsulationBurhan Ahmed
265 views14 Folien

Was ist angesagt?(20)

Awt, Swing, Layout managers von swapnac12
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12292 views
Class and Objects in Java von Spotle.ai
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai7K views
C++ Data Structure PPT.ppt von Mukesh Thakur
C++ Data Structure PPT.pptC++ Data Structure PPT.ppt
C++ Data Structure PPT.ppt
Mukesh Thakur807 views
Function template von Kousalya M
Function templateFunction template
Function template
Kousalya M253 views
classes and objects in C++ von HalaiHansaika
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika14K views
02 JavaScript Syntax von Ynon Perek
02 JavaScript Syntax02 JavaScript Syntax
02 JavaScript Syntax
Ynon Perek3.9K views
Exception Handling in JAVA von SURIT DATTA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA8.5K views
Classes, objects in JAVA von Abhilash Nair
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair28.4K views
Two dimensional arrays von Neeru Mittal
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal10K views

Similar a C++ Arrays different operations .pdf

Data structures and algorithms arrays von
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrayschauhankapil
32 views9 Folien
DSA - Array.pptx von
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx11STEM2PGROUP1
10 views18 Folien
Notes-10-Array.pdf von
Notes-10-Array.pdfNotes-10-Array.pdf
Notes-10-Array.pdfumeshraoumesh40
3 views27 Folien
9 Arrays von
9 Arrays9 Arrays
9 Arrayspraveenjigajinni
950 views68 Folien
arrays-120712074248-phpapp01 von
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
147 views45 Folien

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

Data structures and algorithms arrays von chauhankapil
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
chauhankapil32 views
arrays-120712074248-phpapp01 von Abdul Samee
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
Abdul Samee147 views
Arrays in python von moazamali28
Arrays in pythonArrays in python
Arrays in python
moazamali283.3K views
Aj unit2 notesjavadatastructures von Arthik Daniel
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructures
Arthik Daniel413 views
Arrays.pptx von Epsiba1
Arrays.pptxArrays.pptx
Arrays.pptx
Epsiba14 views

Último

NUTRITION IN BACTERIA.pdf von
NUTRITION IN BACTERIA.pdfNUTRITION IN BACTERIA.pdf
NUTRITION IN BACTERIA.pdfNandadulalSannigrahi
40 views14 Folien
Generative AI to Accelerate Discovery of Materials von
Generative AI to Accelerate Discovery of MaterialsGenerative AI to Accelerate Discovery of Materials
Generative AI to Accelerate Discovery of MaterialsDeakin University
7 views47 Folien
Presentation on experimental laboratory animal- Hamster von
Presentation on experimental laboratory animal- HamsterPresentation on experimental laboratory animal- Hamster
Presentation on experimental laboratory animal- HamsterKanika13641
6 views8 Folien
DNA manipulation Enzymes 2.pdf von
DNA manipulation Enzymes 2.pdfDNA manipulation Enzymes 2.pdf
DNA manipulation Enzymes 2.pdfNetHelix
6 views42 Folien
Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor... von
Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor...Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor...
Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor...Trustlife
184 views17 Folien
Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy... von
Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy...Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy...
Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy...Anmol Vishnu Gupta
8 views10 Folien

Último(20)

Generative AI to Accelerate Discovery of Materials von Deakin University
Generative AI to Accelerate Discovery of MaterialsGenerative AI to Accelerate Discovery of Materials
Generative AI to Accelerate Discovery of Materials
Presentation on experimental laboratory animal- Hamster von Kanika13641
Presentation on experimental laboratory animal- HamsterPresentation on experimental laboratory animal- Hamster
Presentation on experimental laboratory animal- Hamster
Kanika136416 views
DNA manipulation Enzymes 2.pdf von NetHelix
DNA manipulation Enzymes 2.pdfDNA manipulation Enzymes 2.pdf
DNA manipulation Enzymes 2.pdf
NetHelix6 views
Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor... von Trustlife
Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor...Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor...
Ellagic Acid and Its Metabolites as Potent and Selective Allosteric Inhibitor...
Trustlife184 views
Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy... von Anmol Vishnu Gupta
Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy...Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy...
Evaluation and Standardization of the Marketed Polyherbal drug Patanjali Divy...
Best Hybrid Event Platform.pptx von Harriet Davis
Best Hybrid Event Platform.pptxBest Hybrid Event Platform.pptx
Best Hybrid Event Platform.pptx
Harriet Davis11 views
RADIATION PHYSICS.pptx von drpriyanka8
RADIATION PHYSICS.pptxRADIATION PHYSICS.pptx
RADIATION PHYSICS.pptx
drpriyanka815 views
Factors affecting fluorescence and phosphorescence.pptx von SamarthGiri1
Factors affecting fluorescence and phosphorescence.pptxFactors affecting fluorescence and phosphorescence.pptx
Factors affecting fluorescence and phosphorescence.pptx
SamarthGiri19 views
INTRODUCTION TO PLANT SYSTEMATICS.pptx von RASHMI M G
INTRODUCTION TO PLANT SYSTEMATICS.pptxINTRODUCTION TO PLANT SYSTEMATICS.pptx
INTRODUCTION TO PLANT SYSTEMATICS.pptx
RASHMI M G 5 views
Indian council for child welfare von RenuWaghmare2
Indian council for child welfareIndian council for child welfare
Indian council for child welfare
RenuWaghmare211 views
Vegetable grafting: A new crop improvement approach.pptx von Himul Suthar
Vegetable grafting: A new crop improvement approach.pptxVegetable grafting: A new crop improvement approach.pptx
Vegetable grafting: A new crop improvement approach.pptx
Himul Suthar9 views
XUE: Molecular Inventory in the Inner Region of an Extremely Irradiated Proto... von Sérgio Sacani
XUE: Molecular Inventory in the Inner Region of an Extremely Irradiated Proto...XUE: Molecular Inventory in the Inner Region of an Extremely Irradiated Proto...
XUE: Molecular Inventory in the Inner Region of an Extremely Irradiated Proto...
Sérgio Sacani787 views
A giant thin stellar stream in the Coma Galaxy Cluster von Sérgio Sacani
A giant thin stellar stream in the Coma Galaxy ClusterA giant thin stellar stream in the Coma Galaxy Cluster
A giant thin stellar stream in the Coma Galaxy Cluster
Sérgio Sacani23 views
Note on the Riemann Hypothesis von vegafrank2
Note on the Riemann HypothesisNote on the Riemann Hypothesis
Note on the Riemann Hypothesis
vegafrank29 views

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: