SlideShare ist ein Scribd-Unternehmen logo
1 von 7
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

Weitere Àhnliche Inhalte

Was ist angesagt?

Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arraysjanani thirupathi
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded treeRaj Sarode
 
stack & queue
stack & queuestack & queue
stack & queuemanju rani
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentationkunal kishore
 
queue.pptx
queue.pptxqueue.pptx
queue.pptxDr.Shweta
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxPrakash Zodge
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 

Was ist angesagt? (20)

Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
stack & queue
stack & queuestack & queue
stack & queue
 
Stack
StackStack
Stack
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Stack
StackStack
Stack
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 

Ähnlich wie C++ Arrays different operations .pdf

Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrayschauhankapil
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx11STEM2PGROUP1
 
Notes-10-Array.pdf
Notes-10-Array.pdfNotes-10-Array.pdf
Notes-10-Array.pdfumeshraoumesh40
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptxshafat6712
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptxMrMaster11
 
Arrays in Data
Arrays in DataArrays in Data
Arrays in DataAfzal Badshah
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfHimanshuKansal22
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptxpramod599939
 
Array data structure
Array data structureArray data structure
Array data structureharsh112327
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: ArraysSvetlin Nakov
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresArthik Daniel
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptxreddy19841
 

Ähnlich wie C++ Arrays different operations .pdf (20)

Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Notes-10-Array.pdf
Notes-10-Array.pdfNotes-10-Array.pdf
Notes-10-Array.pdf
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays in Data
Arrays in DataArrays in Data
Arrays in Data
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Array data structure
Array data structureArray data structure
Array data structure
 
Array data structure
Array data structureArray data structure
Array data structure
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructures
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 

KĂŒrzlich hochgeladen

Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensorsonawaneprad
 
preservation, maintanence and improvement of industrial organism.pptx
preservation, maintanence and improvement of industrial organism.pptxpreservation, maintanence and improvement of industrial organism.pptx
preservation, maintanence and improvement of industrial organism.pptxnoordubaliya2003
 
《QueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹
《QueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹ă€ŠQueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹
《QueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹rnrncn29
 
Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxEran Akiva Sinbar
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -INandakishor Bhaurao Deshmukh
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxpriyankatabhane
 
Call Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCRlizamodels9
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPirithiRaju
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxpriyankatabhane
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingNetHelix
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPirithiRaju
 
Radiation physics in Dental Radiology...
Radiation physics in Dental Radiology...Radiation physics in Dental Radiology...
Radiation physics in Dental Radiology...navyadasi1992
 
Microteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringMicroteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringPrajakta Shinde
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPirithiRaju
 
Pests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdfPests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdfPirithiRaju
 

KĂŒrzlich hochgeladen (20)

Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensor
 
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort ServiceHot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
 
preservation, maintanence and improvement of industrial organism.pptx
preservation, maintanence and improvement of industrial organism.pptxpreservation, maintanence and improvement of industrial organism.pptx
preservation, maintanence and improvement of industrial organism.pptx
 
《QueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹
《QueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹ă€ŠQueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹
《QueenslandæŻ•äžšæ–‡ć‡­-æ˜†ćŁ«ć…°ć€§ć­ŠæŻ•äžšèŻæˆç»©ć•ă€‹
 
Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀8860477959 EscorTs Service In 24/7 Delh...
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptx
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptx
 
Call Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❀8860477959 Looking Escorts In 24/7 Delhi NCR
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
 
Radiation physics in Dental Radiology...
Radiation physics in Dental Radiology...Radiation physics in Dental Radiology...
Radiation physics in Dental Radiology...
 
Microteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringMicroteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical Engineering
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdf
 
Pests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdf
 
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdfPests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdf
 

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: