Answer to Question #228007 in C++ for Zohaib

Question #228007
Create a dynamic array and copy its contents to another array delete its elements until you press - 1 and find the index in original array and then remove it from the original array. Check when array is empty and print after each removal of element
1
Expert's answer
2021-08-20T12:58:15-0400
#include <iostream>
#include <string>
using namespace std;


int main()
{
	int* originalArray;
	int* copyArray;
	int size;
	int index=0;
	int copyArrayCounter=0;
	//ge size of the aray
	cout<<"Enter size: ";
	cin>>size;
	originalArray=new int[size];
	copyArray=new int[size];


	for(int i=0;i<size;i++){
		cout<<"Enter the element "<<i<<": ";
		cin>>originalArray[i];
	}


	while(index!=-1 && size!=0){
		index=-1;
		cout<<"Enter index of the element to copy [0-"<<(size-1)<<"]: ";
		cin>>index;
		if(index>=0 && index<size){
			copyArray[copyArrayCounter]=originalArray[index];
			copyArrayCounter++;
			cout<<"The element "<<originalArray[index]<<" has been deleted from the original array\n";
			for(int i=index;i<size-1;i++){
				originalArray[i]=originalArray[i+1];
			}
			size--;
			cout<<"\nAll elements in the original array:\n";
			for(int i=0;i<size;i++){
				cout<<originalArray[i]<<" ";
			}
			cout<<"\n";
		}
	}


	cout<<"All elements in the copy array:\n";
	for(int i=0;i<copyArrayCounter;i++){
		cout<<copyArray[i]<<" ";
	}




	//Free memory
	delete[] originalArray;
	delete[] copyArray;
	cin>>index;
	return 0;
}

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