Answer to Question #267250 in C++ for Roe

Question #267250

Your task is to write array implementation of a list with the following functions


Insert


Delete


Search


Sort


Note:No global declarations

1
Expert's answer
2021-11-17T06:28:41-0500
#include<iostream>
using namespace std;
class Array{
	public:
		void insert(){
			  int arr[100] = { 0 };
	    int i, M, P, n = 10;
	    for (i = 0; i < 10; i++)
	        arr[i] = i + 1;
	 
	    cout<<"Original list \n";
	    for (i = 0; i < n; i++)
	        cout<< arr[i]<<" ";
	     cout<<"\n";
	    M = 50;
	    P = 5;
	    n++;
	    for (i = n-1; i >= P; i--)
	        arr[i] = arr[i - 1];
	    arr[P - 1] = M;
	    
	    cout<<"List after inserting: \n";
	    for (i = 0; i < n; i++)
	        cout<< arr[i]<<" ";
		}
		void deleteE(){
			int arr[10], i, n, number, p = 0;


		    cout << "Enter size of an array:";
		    cin>>n;
		
		    cout << "Enter array elements:";
		    for (i = 0; i < n; i++) {
		        cin >> arr[i];
		    }
		    cout << "Enter element for delete:";
		    cin>>number;
		
		    for (i = 0; i < n; i++) {
		        if (arr[i] == number) {
		            arr[i] = arr[i + 1];
		            p = 1;
		        } else if (p > 0) {
		            arr[i] = arr[i + 1];
		        }
		    }
		    cout << "New array list is:";
		    for (i = 0; i < n - p; i++) {
		        cout << arr[i] << " ";
    }
		}
		void search(){
			int arr[10], i, number, n, X=0, P;
        cout<<"Enter Array Size : ";
        cin>>n;
        cout<<"Enter Array Elements : \n";
        for(i=0; i<n; i++)
        {
                cout<<" ";
                cin>>arr[i];
        }
        cout<<"\n Enter Element to be Searched : ";
        cin>>number;
        for(i=0; i<n; i++)
        {
                if(arr[i]==number)
                {
                        X=1;
                        P=i+1;
                        break;
                }
        }
        if(X==0)
        {
                cout<<"\n Element Not Found..!!";
        }
        else
        {
                cout<<"\n Element "<<number<<" Found At Position "<<P;
        }
		}
		void sort(){
			 int arr[100];
    int size, i, j, temp;
    cout<<"Enter size of array: ";
    cin>>size;
 
    cout<<"Enter elements in array: ";
    for(i=0; i<size; i++)
    {
        cin>>arr[i];
    }
    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    cout<<"Elements of array in sorted ascending order:"<<endl;
    for(i=0; i<size; i++)
    {
        cout<<arr[i]<<endl;
    }
		}
};
int main()
{
	int option;
	cout<<"1.Insert\n2.Delete\n3.Search\n4.Sort";
	cout<<"\nEnter your option: ";
	cin>>option;
	Array A;
	if(option==1){
		A.insert();
	}
	else if(option==2){
		A.deleteE();
	}
	else if(option==3){
	A.search();
	}
	else if(option==4){
	A.sort();
	}
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS