Answer to Question #228138 in C++ for Sohaib

Question #228138
Create a class containing
-Create a dynamic 2d array according to size provided by user and an integer that keeps track of its size
-insert values until user presses - 2
-Create another bigger array when previous one is full
-copy all values in the new array and release the old memory print after each insert
-ask the user to delete numbers until he presses - 2
-find the index in the original array and then remove that index from the original array
-stop when array is empty of when user presses - 2
1
Expert's answer
2021-08-22T00:27:29-0400
#include <iostream>
using namespace std;
int main()
{
  
  int row , col, c = 0;
  cout<<"Enter the dimensions of the array\n";
   cout<<"Enter the rows of the 2D array\n";
   cin>>row;
   cout<<"Enter the columns of the 2D array\n";
   cin>>col;
    
 
   
    int** arr = new int*[row];
 
    for (int i = 0; i < row; i++) {
 
        
        arr[i] = new int[col];
    }
 
    cout<<"Enter the elements of the 2D array\n. Press -2 to stop inserting elements\n";
   
 	for(int i=0; i<row; i++){
 		for(int j=0; j<col; j++){
 			cin>>arr[i][j];
 			if(arr[i][j] == -2){
 				break;
			 }
			 c++;
			 break;
			 
		 }
	 }
	  
    
 
   int arr1[row][col];
   for(int i=0; i<row; i++){
   	for(int j=0; j<col; j++ ){
   		arr1[i][j] = arr[i][j];
   	
	   }
	   delete [] arr[i];
   }
   cout<<"The array contains "<<c<<" elements\n";
    
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
 
            
            cout << arr1[i][j] << " ";
        }
        cout << endl;
    }
      
      for(int i=0;i<row;i++)    
      
      delete [] arr;             
     
      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