In: Computer Science
ANSWER :-
GIVEN THAT :-
// to demonstrate use of STL algorithms
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric> //For accumulate operation
using namespace std;
// 1. funtion to sort array
void sort_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
// sort the array with STL sort function
// sorting to enable use of unique()
sort(arr, arr+5);
// print sorted array
cout << "Sorted array: ";
for(int i = 0; i < 5; i++){
cout << arr[i] << " ";
}
}
// 2. funtion to reverse array
void rev_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
// reverse the array with STL reverse function
reverse(arr, arr+5);
// print reversed array
cout << "Reversed array: ";
for(int i = 0; i < 5; i++){
cout << arr[i] << " ";
}
}
// 3. funtion to find min of array
void min_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
//min of array
int ele = *min_element(arr, arr+5);
// print min element in array
cout << "Min element in array: " << ele <<
endl;
}
// 4. funtion to find max of array
void max_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
//max of array
int ele = *max_element(arr, arr+5);
// print max element array
cout << "Max element in array: " << ele <<
endl;
}
// 5. funtion to search for an element in the array
void search_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
int ele;
cout << "Enter element to search: ";
cin >> ele;
//search the array
find(arr, arr+5,5) != arr+5? cout << "\nElement found": cout
<< "\nElement not found";
}
// 6. funtion to sum the array
void sum_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
// sum the array
cout << accumulate(arr, arr+5, 0);
}
// 7. funtion to count occurences of an element in the
array
void count_array(){
int arr[5];
cout << "Enter 5 elements of the array: " ;
// input the array
for(int i = 0; i < 5; i++){
cin >> arr[i];
}
int ele;
cout << "Enter element to be counted: ";
cin >> ele;
// count the array
cout << count(arr, arr+5, ele);
}
// 8. funtion to eliminate duplicate of the array
void remove_dup_array(){
vector<int> vect;
cout << "Enter 5 elements of the array: " ;
int temp;
// input the array
for(int i = 0; i < 5; i++){
cin >> temp;
vect.push_back(temp);
}
// sum the array
// Deletes the duplicate occurrences
vect.erase(unique(vect.begin(),vect.end()),vect.end());
cout << "Array after eliminating duplicates: "
<< endl;
for(auto e: vect){
cout << e << " ";
}
}
// Driver code
int main()
{
while(1){
// Menu
cout << "\nMENU" << endl;
cout << "1. Sort an array: " << endl;
cout << "2. Reverse an array: " << endl;
cout << "3. Find Minimum element of an array: " <<
endl;
cout << "4. Find Maximum element of an array: " <<
endl;
cout << "5. To search an element in an array: " <<
endl;
cout << "6. To find sum of an array: " << endl;
cout << "7. To count occurences of an element in an array: "
<< endl;
cout << "8. Eliminate duplicates of an array: " <<
endl;
cout << "9. Exit: " << endl;
// input the option
int option;
cin >> option;
// lets create a switch case to navigate in the menu
switch(option){
case 1:
sort_array();
break;
case 2:
rev_array();
break;
case 3:
min_array();
break;
case 4:
max_array();
break;
case 5:
search_array();
break;
case 6:
sum_array();
break;
case 7:
count_array();
break;
case 8:
remove_dup_array();
break;
case 9:
return 0;
}
}
}
OUTPUT :-