Answer to Question #236740 in C++ for Lusamine

Question #236740
Your task is to design Dynamic Array (Class).
Your class should work for one and two dimensional Arrays
The class should enable the user to perform the following tasks:
1) Create a dynamic array of size specified by the user. Use an integer to keep track of the
size
2) Ask the user for numbers to insert until the user presses -1. Insert each value in the array.
Create a new larger array when the array is full, copy all data to the new array, and release
the memory for the old array. Print the array after each insert.
3) Ask the user for any numbers to delete until the user presses -1. Find the index of the
entered value in the original array and then remove that index from the original array.
Stop when the array is empty or when the user presses -1. Keep on reducing the array size
and print the array after each removal.
4) Make sure to release any allocated memory not yet released.
Note: make class or classes for these and call the functions in main
1
Expert's answer
2021-09-16T16:54:54-0400
#include<iostream>
using namespace std;
class Dynamic{
	public:
		int data;
		int maxList;
		int length;
		int *arr;
	Dynamic(int max){
		maxList = max;
		arr=  new int[maxList];
	}
	void insert(){
		length = 0;
		int element;
		do{
		cout<<"Enter an element\n ";
		cin>>element;
		arr[length] = element;
		length++;
		cout<<"Elements of the array and enter -1 to stop inserting element: \n";
		
		for(int i=0; i<length; i++ ){
			cout<<arr[i]<<" ";
		}
		cout<<endl;
		
		
		if(length == maxList){
	 maxList = maxList + 100;	
	int new_size  = maxList;
	int* new_arr = new int[new_size];
   copy(arr, arr + min(maxList, new_size), new_arr);
   delete[] arr;
   arr = new_arr;
}
		}while(element != -1);
		cout<<"Stopped\n";
	}
		
	void deleteElement(int del){
		int count = 0;
		 for(int i=0; i<length; i++)
        {
                if(arr[i]==del)
                {
                        for(int j=i; j<(length-1); j++)
                        {
                                arr[j]=arr[j+1];
                        }
                        count++;
                        break;
                }
        }
	}
    


};


int main(){
	Dynamic d(10);
	d.insert();
	

}

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