How to find size of array

so I have an array size 10 and I need to find out how many spaces are open and then fill those spaces with information from another array and count how many spaces are used. so my question is how do I find the amount of space used in the first array.
So the question really isn't how to find the size of the array. So size 10.

To answer your question, I must say that it depends on the type of the array. If we are talking about good old C++ arrays for non-nullable types, as in:

 
int *myIntArray = new int[10];


then there is not enough information in the array itself to determine which spots are "unused". Because the type int is not nullable, any array spot will have a valid int. Things improve if you define a valid range of values. For example, you could say that you will only consider as valid values those that are greater than, or equal to zero. In that case, you can define an "empty spot" as one containing a number less than zero. Therefore, you could then do something like:

1
2
3
4
5
6
7
8
9
10
11
int *myIntArray = new int[10];
for(int i = 0; i < 10; i++)
{
    myIntArray[i] = -1;
}
//Or do all at once with memset().  I don't know which initialization method is more efficient.
memset(myIntArray, 0xFF, 10 * sizeof(int));
//At this point you have an array with 10 spots free because all 10 spots have a value of -1,
//which fulfills the assumption that any negative value implies an unused spot.

//Now all you have to do is count instances of negative numbers to find the number of "empty spots". 


If, on the other hand, you have an array of some kind of pointer, then pointers are nullable, which means that you can define "empty spot" as any spot being null. That would be a lot easier.
it is c++ and the empty are just set to 0
the function is suppost to follow these conditions

int add(const IntStore& addend)
// Pre: (none)
// Post: As much of the contents of "addend" as possible
// are added to the invoking IntStore and the number
// of distinct values (in "addend") so added is returned.
// Note: Contents of "addend" are added in order of position
// (i.e., those with lowest positions are added first).
// Note: addend is allowed to be the invoking IntStore itself.

I need to add one array into another array without exceeding the MAX_SIZE
so I find the amount of spaces in the first array that are used then add the second array into the "empty " spaces and count the "empty" spaces used and return that number
Topic archived. No new replies allowed.