DELETING AN ELEMENT FROM AN ARRAY

//there is a simple program that will delete an element from array

language=C++

 #include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()
{
    int n,number = 0;

    cout << "Enter number of elements?";

    cin >>n;

    int la[n];

    for(int i = 0; i < n; i++)
    {
        char c;

        scanf( "%d%c",&la[i],&c );

/*input will terminate when enter is pressed or when the array is full and continude by pressing space.*/

        number += 1;

        if(c == '\n')
        {
            break;
        }
    }

    int delete_position,counter;

    cout << "which position do you want to delete?";

    cin >> delete_position;

    if(delete_position > n+1 || delete_position > number)
    {

        cout << "data overflow\n";
        return 0;

    }

    if(delete_position == 0)
    {

        cout << "data underflow";
        return 0;

    }

    counter = delete_position-1;

    while( counter < number - 1 )
    {

        la[counter] = la[counter+1];

        counter += 1;

    }

    number -= 1;

    cout << "updated linear array:";

    for(int i = 0; i < number; i++)

        cout << la[i] << " ";

    return 0;

}


No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...