Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:54:14+00:00 2026-06-09T00:54:14+00:00

So my code is suppose to insert numbers into a dynamic array, add more

  • 0

So my code is suppose to insert numbers into a dynamic array, add more capacity to the array if more is needed, remove numbers from the array and then make sure the only NULLS occur at the end of the array. It also tells the user how many numbers are in the array and what is the total size of the array. My problem is when I remove a number from the array, it sometimes prints out there is a number -33686019 in my array. This doesn’t occur much, but I don’t want it to occur at all.

#include <stdio.h>
#include <iostream>

int* gArray = NULL;
int gSize = 0;
int gCapacity = 0;
void Insert(int value);
void Remove(int value);
void Resize(int newCapacity);
void Print(void);


void main()
{
int input = 0;

while(input != 3)
{
    printf(">=== Dynamic Array ===\n");
    printf("What do you want to do?\n");
    printf("1. Insert\n");
    printf("2. Remove\n");
    printf("3. Quit\n");
    printf("Your choice: ");
    scanf_s("%d", &input);
    printf("\n\n");

    int value = 0;

    switch(input)
    {
    case 1:
        {
            printf("Enter a number: ");
            scanf_s("%d", &value);
            Insert(value);
            Print();
            break;
        }
    case 2:
        {
            printf("Enter number you wish to delete: ");
            scanf_s("%d", &value);
            Remove(value);
            Print();
            break;
        }
    case 3:
        {
            break;
        }
    default:
        {
            printf("Invalid selection\n");
        }
    }
}
}
void Insert(int value)
{
bool valueSet = false;

while(valueSet == false)
{
    if(gArray == NULL)
    {
        Resize(1);
        gArray[gSize] = value;
        ++gSize;
        valueSet = true;
    }
    else if(gArray[gCapacity] == NULL)
    {
        gArray[gSize] = value;
        ++gSize;
        valueSet = true;
    }
    else if(gArray[gCapacity] != NULL)
    {
        Resize((gCapacity + 1));
        gArray[gSize] = value;
        ++gSize;
        valueSet = true;
    }
}

}
void Resize(int newCapacity)
{
int* tempArray = new int[newCapacity];
std::copy(gArray, gArray+(newCapacity-1), tempArray);
gArray = new int[newCapacity];
std::copy (tempArray, tempArray+(newCapacity-1), gArray);
gCapacity = newCapacity;
}
void Remove(int value)
{
for(int i = 0; i < gCapacity; ++i)
{
    if(gArray[i] == value)
    {
        gArray[i] = NULL;
        --gSize;
    }
}
for(int i = 0; i < gCapacity; ++i)
{
    if(gArray[i] == NULL)
    {
        gArray[i] = gArray[(i + 1)];
        gArray[(i + 1)] = NULL;
    }
}
}
void Print(void)
{
printf("Array contains: ");
for(int i = 0; i < gCapacity; ++i)
{
    if(gArray[i] != NULL)
    {
        printf("%d, ", gArray[i]);
    }
}
printf("size = %d, capacity = %d\n", gSize, gCapacity);


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:54:16+00:00Added an answer on June 9, 2026 at 12:54 am

    The concrete problem is that you don’t initialize the new array (resp. tempArray) in your Resize function.

    When calling

    int* tempArray = new int[newCapacity];
    

    the array can contain arbitrary values. Only newCapacity-1 values are copied from the old array, so the last value is undefined. It might be 0 but haven’t to be. Use

    std::fill(tempArray, tempArray+newCapacity, 0);
    

    to initialize your array with zero.

    Apart from that, there are a few other problems:

    • You don’t delete the old array before allocating a new one. Use delete[] gArray for that. Also tempArrayisn’t deleted!
    • You don’t need to copy the values twice. Just to a gArray = tempArray (after deleting the old gArray, see above)
    • You assume that newCapacity is just larger by one than gCapacity (you copy newCapacity-1 values from the old array). It would be better to copy gCapacity values instead.
    • Dynamic arrays which only grow by one are inefficient, since adding a value takes linear time (you have to copy all the old values when inserting a single one). Usually, you double the size of the array every time you run out of space, this gives constant insertion time in average.
    • NULL is normally used only for pointers. For ints it is equal to zero which means, you cannot store 0 in your array (given your requirements)
    • In production code, I’d strongly recommend using std::vector instead of any home-grown solution.

    EDIT

    See @StackUnderflows answer for what is probably the real cause of the error. If you run in Debug mode, some compilers will automatically initialize the array for you, which might be the ccase here.

    The gArray[i]=gArray[i+1] line in your Remove function is definitely wrong on the other hand, since it accesses a value which is beyond the limits of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert items from my array into my tree. My function
Suppose I have this code create temporary table somedata (a integer); insert into somedata
This code is suppose to add an onClick event to each of the a
I am trying to insert a block of code into MikTeX which contains many
I am building a Facebook canvas app, and the code suppose to be generated
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):
The following code is suppose to be doing this : - Displaying a string
I have this code that suppose to place the marker by the latlng public
Suppose my code is like this: <td class=apple> <div class=worm> text1 </div> </td> <td
Hello i'm testing the following code it's suppose to list all apps installed on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.