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;
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.
#include <iostream>
usingnamespace 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 = newint*[n];
for (int i = 0; i < n; ++i)
arr[i] = newint[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;
}
elseif (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;
}
elseif (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
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.
#include <memory>
#include <iostream>
usingnamespace std;
int main()
{
size_t columns;
cout<<"enter your columns\n ";
cin>>columns;
unique_ptr<int[][10]> ar(newint[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 ...
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.