Answer to Question #233664 in C++ for Umer

Question #233664
Your task is to design Dynamic Array (Class).
What is Dynamic Array?
Which can grow and shrink at run time.
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.
1
Expert's answer
2021-09-06T01:59:16-0400
#include <iostream>
using namespace std;
class Dynamic{


};
int main()
{
  	  int rows , cols, x = 0;
  cout<<"Insert the dimensions of the array\n";
   cout<<"Enter the rows of the 2D array\n";
   cin>>rows;
   cout<<"Enter the columns of the 2D array\n";
   cin>>cols;
    int** arr = new int*[rows];
 
    for (int i = 0; i < rows; i++) {
 
        
        arr[i] = new int[cols];
    }
	cout<<"Insert the elements of the 2D array"<<endl;
	cout<<" Press -1 to stop inserting elements\n";
   
 	for(int i=0; i<rows; i++){
 		for(int j=0; j<cols; j++){
 			cin>>arr[i][j];
 			if(arr[i][j] == -1){
 				break;	} 
			 }
			 x++;
			 break;
}


   int arr1[rows][cols];
   for(int i=0; i<rows; i++){
   	for(int j=0; j<cols; j++ ){
   		arr1[i][j] = arr[i][j];
   	
	   }
	   delete [] arr[i];
   }
   cout<<"The array contains the elements\n";
    
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << arr1[i][j] << " ";
        }
        cout << endl;
    }   
      
      delete [] arr;            
   cout<<"Enter the elements of the 2D array to be deleted"<<endl;
	cout<<" Press -1 to stop \n";
		for(int i=0; i<rows; i++){
 		  for(int j=0; j<cols; j++){
 			cin>>arr1[i][j];
 			if(arr1[i][j] == -1){
 				break;	}
}
			 x++;
			 break;}




}

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