Program for DELETING AN ELEMENT of an Array

PHOTO EMBED

Tue Sep 10 2024 06:20:38 GMT+0000 (Coordinated Universal Time)

Saved by @Rohan@99

#include <iostream>

 using namespace std;

 int main() 
 {
    int array_length, element_to_delete, index = -1;

    cout << "--------------Program for DELETING AN ELEMENT of an Array--------------\n" << endl;
    cout << "Enter the size of your Array: ";
    cin >> array_length;
    
    int nums[array_length];
    
    cout << "\n";

    cout << "Input " << array_length << " elements: " << endl;
    for(int i = 0; i < array_length; i++)
    {
        cin >> nums[i];
    }
    
    cout << "\n";
    
    cout << "Enter the element you wish to delete: ";
    cin >> element_to_delete;
    
    for(int i = 0; i < array_length; i++)
    {
        if(nums[i] == element_to_delete)
        {
            index = i;
            break;
        }
    }
    
    if(index == -1)
    {
        cout << "Element not found. No deletion performed." << endl;
        return 0;
    }
    
    cout << "\n";
    
    for(int i = index; i < array_length; i++)
    {
        nums[i] = nums[i + 1];
    }
    
    array_length--;
    
    cout << "\n";
    
    cout << "Updated Array: " << endl;
    for(int i = 0; i < array_length; i++)
    {
        cout << nums[i] << " ";
    }
 }
content_copyCOPY