#include <iostream>
using namespace std;
// Reading the elements into the array
void readArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout<<"\n";
cout << "Enter element " << i + 1 << ": ";
cin >> arr[i];
}
}
// Function to display elements of the array
void displayArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
cout << endl;
}
void sumofelements(int arr[], int n){
int sum = 0;
for(int i = 0; i < n; i++){
sum = sum + arr[i];
}
cout<<"Sum of elements of the array : "<<sum;
}
void searchmin(int arr[], int n){
int minval = arr[0];
int i;
int minvalposition;
for(int i = 1; i < n; i++){
if(minval > arr[i]){
minval = arr[i];
minvalposition = i;
}
}
cout<<"The minimum value in array is at " << minvalposition+1 << "th position, and its value is:
"<<minval;
}
void searchmax(int arr[], int n){
int maxval = arr[0];
int i;
int maxvalposition = 0;
for(int i = 1; i < n; i++){
if(maxval < arr[i]){
maxval = arr[i];
maxvalposition = i;
}
}
cout<<"The maximum value in array is at " << maxvalposition+1 << "th position, and its value is:
"<<maxval;
}
void searchval(int arr[], int n, int target){
int targetpos = -1 ;
int i;//i.e if its not on the array its value should be -1
for(int i = 0; i < n; i++){
if(arr[i] == target){
targetpos = i+1;//if data is found it will store it position + 1 value in targetpos
break;
}else{
targetpos = -1;
}
}
if(targetpos == -1){
cout<<"Data not found!!";
}else{
cout<<"Data found at location : "<<targetpos;
}
}
void sortarray(int arr[], int n){
int i , j;
int temp = 0;
for(i = 0; i < n-1 ; i++){
for(j = 0; j < n - i -1; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"Sorted array is :\n";
for (i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void insert_atstart(int arr[], int n, int data){
int i;
for(i = n;i > 0; i--){
arr[i] = arr[i-1];
}
n++;
arr[0] = data;
cout<<"New array is: ";
for (i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void insert_atend(int arr[], int n, int data2){
arr[n] = data2;
n++;
for (int i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void insert_atarbitary(int arr[], int n, int data3, int pos3){
int i;
for (int i = n; i >= pos3; i--) {
arr[i] = arr[i - 1];
}
// Insert the new element
arr[pos3 - 1] = data3;
n++; // Increase the size of the array
cout<<"New array is :";
for (i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void delete_start(int arr[], int n){
int trash ,i;
trash = arr[0];
for(i = 0; i < n-1; i++){
arr[i] = arr[i+1];
}
n--;
cout<<"New array :";
for (i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void delete_end(int arr[], int n){
int trash ;
trash = arr[n-1];
n--;
cout<<"New array : ";
for (int i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void delete_arbitary(int arr[], int n, int delpos){
int trash,i;
trash = arr[delpos];
for(i = n ; i > delpos; i--){
arr[i] = arr[i-1];
}
n--;
cout<<"New array: ";
for (i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}
void update(int arr[],int n,int newval, int newpos) {
int ij,i;
ij = newpos-1;
arr[ij] = newval;
cout<<"New array is :";
for (i = 0; i < n; i++) {
cout <<"\nElement ["<<i+1<<"]="<<arr[i] << " ";
}
}//updating the array by overwritting the value
int main() {
// Declaring the array
int arr[100];
int n;
int choice;
char option;
cout << "\nEnter length of array: ";
cin >> n;
// Checking the length validity
if (n <= 0 || n > 100) {
cout << "Invalid array length" << endl;
return 1;// This says if length is out of the given length then invalid
}
do {
// Mneu option
cout << "\n1. Read array";
cout << "\n2. Display array";
cout << "\n3. Sum of Array Elements";
cout << "\n4. Search Minimum";
cout << "\n5. Search Maximum";
cout << "\n6. Search Value";
cout << "\n7. Sort";
cout << "\n8. Insert Element at Start";
cout << "\n9 Insert Element at End";
cout << "\[Link] Element at Arbitary Position";
cout << "\n11 Delete Element at start";
cout << "\n12 Delete Element at End";
cout << "\n13 Delete Element at Arbitary";
cout << "\n14 Update Array";
cout << "\n15 Exit\n";
cout<<"_________________________________________________________________________";
cout << "\nEnter choice: ";
cin >> choice;
// We use the switch case here to execute the user choice
switch (choice) {
case 1:
readArray(arr, n); // Call function to read array elements
break;
case 2:
displayArray(arr, n); // Call function to display array elements
break;
case 3:
sumofelements(arr,n);
break;
case 4:
searchmin(arr, n); // Call function to display array elements
break;
case 5:
searchmax(arr,n);
break;
case 6:
int target;
cout<<"Enter the target element: ";
cin>>target;
searchval(arr, n , target); // Call function to display array elements
break;
case 7:
sortarray(arr, n);
break;
case 8:
int element;
cout<<"Enter the value of new value to be added: ";
cin>>element;
insert_atstart(arr, n , element); // Call function to display array elements
break;
case 9:
int element2;
cout<<"Enter the value of new value to be added: ";
cin>>element2;
insert_atend(arr, n, element2);
break;
case 10:
int value,pos;
cout<<"Enter the value and position of new element: ";
cin>>value>>pos;
insert_atarbitary(arr, n,value, pos); // Call function to display array elements
break;
case 11:
delete_start(arr, n);
break;
case 12:
delete_end(arr, n);
break;
case 13:
int pos4;
cout<<"Enter the position you want to delete: ";
cin>>pos4;
delete_arbitary(arr, n, pos4);
break;
case 14:
int upval,uppos;
cout<<"Enter the updated value and position to be updated: ";
cin>>upval>>uppos;
update(arr, n ,upval,uppos);
break;
case 15:
cout<<"code is terminated thankyou!";
default:
cout << "\nWrong choice" << endl; // any other option error mssg
}
if (choice != 3) {
cout << "\nDo you wish to continue (Y/N)? ";
cin >> option;
} else {
option = 'N'; // Automatically set option to 'N' to exit loop
}
} while (option == 'Y' || option == 'y'); // Continue if user chooses 'Y' or 'y'
return 0; // Return 0 to indicate successful execution
}