does anyone know how to create the delete function?

does anyone know how to create the delete function?
i tried many ways and i cant make it.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
int a;
int del;
//int r;

//int c;
//int temp;


int **arr;
int n = 0, m = 0;
char del;




cout << "DYNAMIC ARRAY" << endl;
cout << "(1) Please insert number of rows and columns for the dynamic array.\n"
<< "(2) Display all number\n"
<< "(3) Delete number\n";

do
{
cout << "Choose your option.";
cin >> a;

if (a == 1)
{
{




cout << "Please input the number of rows of the dynamic array:";
cin >> n;
cout << "Please input the number of columns of the dynamic array:";
cin >> m;
arr = new int*[n];
for (int i = 0; i < n; i++)
{
arr[i] = new int[m];
}
cout << "*Input number*" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << "Please input a value of [" << i << "][" << j << "] in the array : ";
cin >> arr[i][j];
}
}

cout << "*Output*" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << "The array named " << "[" << i << "][" << j << "] is created.:"<< arr[i][j] << endl;
}
}





}





}

else if (a == 2)
{


//int temp;

//arr = new int*[n];


for (int i = 0; i < n; i++)
{

for (int j = 0; j < m; j++)
{

cout << "The current display number is [" << i << "][" << j << "] : " << arr[i][j] << endl;
}
}


}

else if (a == 3)
{

int nn;
cout<<"Select the number you want to delete:";
cin>>del;

for (int i = 0; i < n; i++)
{

for (int j = 0; j < m; j++)
{
del[i][j] = del [i+1][j+1];
del[nn-1]=0;
--nn;

//if( arr[i][j]==del)
//arr[i][j]=i*m+j;


/*delete[] arr[i];
delete[] arr;
arr = NULL;
*/

}

}


} while (a != 0);


return 0;
}
The ID "del" can't be used for multiple objects.

Aceix.
1. Please use code formatting, you can use it by:
- writing "code" YOUR CODE HERE "/code" (code and /code have to be in square brackets), e.g.:
std::cout << "Hello World!";
- selecting text you want to format and clicking on top-left corner button on the right side when writing post

2. You cannot have char variable and int variable named the same. You tried to create char del and int del

3. Your formating of code is ugly. I formated your code the way I like. It's not perfect, you don't have to use it but it's definitely better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

int main(){
	int a;
	int del;
	int **arr = nullptr; // You need to initialize it, otherwise compiler will post error
	//cause you can chose 2 when program starts, there'd be an option to use uninitialized variable
	int n = 0, m = 0;

	cout << "DYNAMIC ARRAY" << endl
		<< "(1) Please insert number of rows and columns for the dynamic array.\n"
		<< "(2) Display all number\n"
		<< "(3) Delete number\n";

	do{
		cout << "Choose your option.";
		cin >> a;

		if (a == 1){
			cout << "Please input the number of rows of the dynamic array:";
			cin >> n;
			cout << "Please input the number of columns of the dynamic array:";
			cin >> m;

			arr = new int*[n];
			for (int i = 0; i < n; ++i)
				arr[i] = new int[m];

			cout << "*Input number*" << endl;

			for (int i = 0; i < n; i++)
				for (int j = 0; j < m; ++j){
				cout << "Please input a value of [" << i << "][" << j << "] in the array:";
				cin >> arr[i][j];
				}

			cout << "*Output*" << endl;
			for (int i = 0; i < n; ++i)
				for (int j = 0; j < m; ++j)
					cout << "The array named " << "[" << i << "][" << j << "] is created.:"
					<< arr[i][j] << endl;
		}
		else if (a == 2){

			for (int i = 0; i < n; ++i)
				for (int j = 0; j < m; ++j)
					cout << "The current display number is [" << i << "][" << j << "]:"
					<< arr[i][j] << endl;
		}
		else if (a == 3){
			int nn;          // You didn't initialize it, but you want to use it's value!
			cout << "Select the number you want to delete:";
			cin >> del;

			// I have no idea what you wanted to do below
			for (int i = 0; i < n; ++i)
				for (int j = 0; j < m; ++j){
				arr[i][j] = arr[i + 1][j + 1];

				if (arr[i][j] == del)
					arr[i][j] = i*m + j;

				delete[] arr[i];
				delete[] arr;
				arr = nullptr;
				}
		}
	} while (a != 0);

	return 0;
}


4. Please tell me want you wanted to do more detailed and I'll do my best to help you
Last edited on
closed account (SECMoG1T)
Do you intend to overload the delete [] operator or just make a function that frees your dynamic array, if freeing your dynamic memory is the problem perhaps you could opt to turn to library containers such as vectors or smart pointers they will get the job done for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <memory>
#include <iostream>

using  namespace std;

int main()
{
    size_t columns;
    cout<<"enter your columns\n  ";
    cin>>columns;

    unique_ptr<int[][10]> ar(new int[columns][10]());///you can change this values
    ///not that you have to be sure of all the other dimensions, the first
    ///dime is optional ;

   //if already you had created a dynamic array and all you want to avoid is 
  /// an erroneous memory leak while manually calling delete

  unique_ptr<int[/*dimensione 1*/][/*dimension two*/]> ptr(mydynamic_array);

 //if mydynamic_array was your array this is possible however you should avoid
 /// deleting the memory pointed to by that pointer [mydynamic_array] or it will
 /// cause you hell. 

    ///more code

    for(int i=0;i<columns;i++)
    {
        for(int j=0;j<10;j++)
            cout<<ar[i][j]<<' ';
        cout<<endl;
    }

/// the dynamic array will be destroyed once the array goes out of
/// scope " however thats not better coz like in these case the 
/// pointer will have to wait till we exit main. you could also use unique_ptr 
/// function release to free the memory after your are done

ar.release(); /// frees the memory pointed to by ar;

}



if your array goes out of scope it wil be deleted automaticaly without you explictily calling delete

For the instance that might be you'r still unaware of smart pointers or containers, here is a simple guide how you could create a functions to manage your array directly, including create, delete ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

///note av used template functions so that our array will be applicable to any type.
/// also we will be passing our array by reference to make work easier

template <typename T>
void print( T** &ar,size_t column,size_t rows)
{
    cout<<"\n\nArray content\n\n";
    for(size_t i=0;i<column;i++)
    {
        cout<<"  ";
        for(size_t j=0;j<rows;j++)
        {
            cout<<ar[i][j]<<" ";
        }
        cout<<endl;
    }
}

template <typename T>
void create_array(T** &ar,size_t column,size_t rows)
{
    ar=new T*[column];

    for(size_t i=0;i<column;i++)
    {
        ar[i]=new T[rows]();///default intialized
    }

}

template <typename T>
void destroy_array(T** &ar,size_t column, size_t rows)
{
   for(size_t i=0;i<column;i++)
   {
       delete ar[i]; ar[i]=nullptr;
   }

   delete [] ar;
   ar=nullptr;
}

int main()
{

    size_t column,rows;
    int **ar=nullptr;

    cout<<" enter your array columns and rows sizes\n  ";
    cin>>column>>rows;

    create_array(ar,column,rows);
    print(ar,column,rows);
    ///more code here
    destroy_array(ar,column,rows);

}


you could also add more functions and manipulations to your code.

hope that helps.
Last edited on
about the delete code , i would like delete column and row number together.


cout << "Select the number you want to delete:";
cin >> del;


for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j){
arr[i][j] = arr[i + 1][j + 1];

if (arr[i][j] == del)
arr[i][j] = i*m + j;

delete[] arr[i];
delete[] arr;
arr = nullptr;
}


closed account (SECMoG1T)
No no you can't do like that , you'll have memory leakage, if you choose to follow that routine you might not be able to properly keep track of what you deleted and what you dint then later you might delete a pointer that had already been deleted resulting into undefined behavior, if these is what you really want i'll advice you to use containers such as vectors instead.
I would suggest vectors and just "delete" the index of the element the user wants to delete
what do u want to delet?
Topic archived. No new replies allowed.