Traversing
Traversing
Output :
10
20
30
40
50
#include<iostream>
using namespace std;
int main()
{
int arr[5];
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5
Algorithm
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable 'i' as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
Example
#include<iostream>
using namespace std;
int main()
{
int arr[6], i, elem;
cout<<"Enter 5 Array Elements: ";
for(i=0; i<5; i++)
cin>>arr[i];
cout<<"\n Enter Element to Insert: ";
cin>>elem;
arr[i] = elem;
cout<<"\nThe New Array is:\n";
for(i=0; i<6; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output:
Enter 5 Array Elements:
50
43
37
35
16
Enter Element to insert: 99
int main()
{
// Original array with some unused space at the end
int arr[6] = { 10, 20, 30, 40, 50 };
int n = 5; // Number of elements in the array
// Element to add
int element_to_add = 5;
Output
Original Array: 10 20 30 40 50
Updated Array: 5 10 20 30 40 50
Insert an Array Element at a Specific Position
To insert an element in an array in C++ programming, you have to ask
the user to enter the size and elements of the array. And then ask to enter
the element to insert and at what position, as shown in the program
given below:
After inserting the element at the desired position, don't forget to display
the new array on the screen.
#include<iostream>
using namespace std;
int main()
{
int arr[10], i, elem, pos, n;
cout<<"Enter the Size for Array: ";
cin>>n;
cout<<"Enter Array Elements: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\nEnter Element to Insert: ";
cin>>elem;
cout<<"At What Position ? ";
cin>>pos;
for(i=n; i>=pos; i--)
arr[i] = arr[i-1];
arr[i] = elem;
n++;
cout<<"\nThe New Array is:\n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output :
Enter the Size for Array: 5
Enter Array Elements: 1 2 3 4 5
Enter Element to Insert: 6
At What Position ? 6
The New Array is:
1 2 3 4 5 6
Algorithm
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example
#include <iostream>
using namespace std;
int main()
{
int LA[3] = {1,3,5};
int i, n=3;
cout << "The original array elements are :"<<endl;
for(i = 0; i<n; i++)
{
cout <<LA[i] << endl;
}
for(i = 1; i<n; i++)
{
LA[i] = LA[i+1];
n = n - 1;
}
cout << "The array elements after deletion :"<<endl;
for(i = 0; i<n; i++)
{
cout <<LA[i] <<endl;
}
}
Output
The original array elements are :
1
3
5
The array elements after deletion :
1
5
Searching Methods
Searching is the fundamental process of locating a specific
element or item within a collection of data.
This collection of data can take various forms, such as arrays, lists,
trees, or other structured representations.
The primary objective of searching is to determine whether the
desired element exists within the data, and if so, to identify its
precise location or retrieve it.
It plays an important role in various computational tasks and real-
world applications, including information retrieval, data analysis,
decision-making processes, and more.