pointers and arrays c++

I have a project to dynamically create an array and do some actions into it namely:

  • Function to create the dynamic array based on user input.
  • Function to store elements in the array based on user input.
  • Function to prints the elements in the array based on user input.
  • Function to delete the dynamic array.

and here’s the part of my code to action #1:

    int menu(int choice)
    {
    displayMenu();
    cin >> choice;
    cout << "\n";
    if(choice == 1)
    {
    cout << "Enter size of the array: ";
    cin >> sizes;
    if(sizes > 0)
    {
    arr = new int[sizes];
    cout << "\nArray has been created.";
    }

    cout << "\n\n";
    menu(choice);
    }

And now my problem is that, when I do action 1, it creates the array, then IF i do action 3(show output) it output random numbers but the output must be a prompt (“array is empty”) because I have to store elements first using action 2 to manually input the elements of the array. The creation of the array has random numbers in it. How can i get rid of that?

Will you please tell what are you using to display the elements? As you are allocating using new and new always calls constructor when allocates. Thus, when you haven’t given the input, i.e. choice 2, it’ll store 0.

    int size;
//cout<<"Enter size : ";
cin>>size;
int *a;
if(size>0)
a = new int[size+1];

for(int i=0;i<size;i++)
 cout<<*(a+i)<<" ";  // output is 0 for size number of elements, because the space has already been allocated

After the below comments, there is this possibility.

    int size,choice,*a;
    //When you are declaring the array, mark its first entry some invalid entry to denote that the array is currently empty.
     if(choice==1)
     {
     cin>>size;
     if(size>0)
      a = new int[size+1];
       *(a+0)= -1;// set the first entry in array as invalid
     }


else if(choice==3)
{   // array will be empty if the following is true
	if(*(a+0)==-1) 
	cout<<"Array empty\n";
	else{  // array not empty
 for(int i=0;i<size;i++)
  cout<<*(a+i)<<" "; 
	}
}
else if(choice==2)
{
  *(a+0)=1; // now we are inserting some elements.
  cin>>num; // no. of elements to be inserted
  for(int i=1; i<=num;i++)
   {
     cin>>ele;
      *(a+i)=ele;
   }
 }

Alternately you can use vector which is a very useful tool for handling dynamic allocation.

EDIT: Because you are dealing with the array, you must be needing some variable num which tells you the size of the array (not the allocated ones), it refers to the number of elements in the array. Don’t make an external global variable to check for condition 3, use this num variable only. Initially and obviously, you must have set num to 0. When you go for choice 3, check if num is 0 which means array is empty and then when you go for choice 2, increment num with the number of times you insert in you array. This may not annoy your professor :stuck_out_tongue:

1 Like

In your Function3(which prints array elements) you should first check whether the array is empty or not.

One of the approach could be to take a global variable say flag, initialized to 0.
When user calls Function2 ( read user input ) there update flag variable as 1. So that it means that now the array is not empty.

In Function3 check:
if( flag == 0 ) print(“Array is empty”)

Also, when you delete the array change the value of flag to 0 again.

else if(choice == 3)

         {
         if(sizes < 1)

         {
        cout << "Array is empty. Unable to print.\n\n";
        menu(choice);

         }

    else
    {
    cout << "Array Contents: ";
    for(int x = 0; x < sizes; x++)
    {
        cout << *(arr+x) << " ";
    }
    cout << "\n\n";
    menu(choice);
    }

}

That will create some problems if the requirements comes that delete certain elements, then it’ll have to be handled in delete query too. I thought the very same

This isn’t gonna work, because your size is not less than 1, in query 1 you are specifying the size. Isn’t it. You give input 2, an array of 2 element gets allocated. So the size is not less than 1. Can u use stl? vector is what you need here… dynamic allocation will be handled very easily using that…

uhm, I had trapped inputs to action 1 (assuming an input to action 1 is 0 or a negative number, that gives a negative size to an array, and an error, bad_alloc (something like that) appears)

look for the else statement, that works if the input to action 1 is 2.

I’m sorry, this is a project, we’re only allowed to use arrays :frowning:

But I don’t think the functions he is trying to implement involve any such functionality.

That is what I’m saying, if you give size as 2 in choice 1 and then go for choice 3, then also you array is currently empty because no input is there in the array by the user. Are you getting this point of mine? I’ll tell it again if you want some… , and deletion of whole array you want or deletion of some elements in the array?? That is gonna decide what we can do in such case, however, see the edit above…

I asked that in the comment, no answer though, We can use a global boolean variable or that can be done without a variable also as my above code says… :slight_smile: Problem was his code for choice=3 option was wrong if you’ll see in his comments. He was checking size which is not the decision variable here for checking if the array is empty or not.

Sorry for late reply, i have been trying to update my code. Well, a global variable would be helpful, (and can be done in our project) but I dont think our prof would be happy if I use that :3

Ill try my best to understand, here’s my case, in my code, if I do action 1, input 2 in there, then do action 3, it output random numbers even I had not chose action 2. Well, Ill scan your given code. Ill update you if error happens. THANKS :slight_smile:

An easy way out without using that global variable. See the edit once again :stuck_out_tongue:

Thank you for your response, I deeply appreciate your time to explain this to me, I now understand how to deal with my problem without relying to global variables. I wish I can think the same way as you do :3

Haha… just practise, that’ll make you such. Also, please close the thread after making the answer as accepted. It helps others know the correct possible ans correct answer… and it’s not about time or my ability to think such, its just that the best way to learn is to make others learn (y)