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: