Array full or not

Can anyone tell a simple way to check if an array is full or not?
Have you considered using std::vector instead of an array? A std::vector knows how many elements have be "inserted" and can add space for more as needed.

But no there is no real way of knowing if an array is full.

You, the programmer, need to keep track of how many elements were put into the array and the size of the array.

What if I assign the array with NULL and then insert few elements . Then make a function which checks if all the indexs are Null or not. Null is assigning zero to the indexes, isn't it?
Hello lost110,

As much as I agree with jlb I hate to be contrary.

This should give you an idea of what you might do:
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

constexpr size_t MAX_ARRAY_SIZE{ 10 };

bool CheckArraySize(size_t arrayUsed); // <--- Comment or remove if you can not use functions.

int main()
{
	bool arrayGood{};
	size_t arrayUsed{ 5 };
	int numArr[MAX_ARRAY_SIZE]{ 1, 2, 3, 4, 5 };
	
	arrayGood = CheckArraySize(arrayUsed); // <--- Comment or remove if you can not use functions.

	if (!arrayGood) // <--- Or use arrayUsed >= MAX_ARRAY_SIZE inside the ()s.
	{
		std::cout << "\n    Array is full! \n" << std::endl;
	}
	else
	{
		std::cout << "\n    You have " << MAX_ARRAY_SIZE - arrayUsed << " Numbers left." << std::endl;
	}

        // A fair replacement for "system("pause")" or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}

bool CheckArraySize(size_t arrayUsed)
{
	return arrayUsed < MAX_ARRAY_SIZE;
}

I used a function here, but it can easily be changed. The comments should help you with that.

Changing the value on line 13 will allow you to test the code. Otherwise this value would come from the program when numbers are entered.

Not seeing any code from you it is hard to exactly where you might use this. This is where the function comes in handy.

I think you should be able to do something with this code in your program.

Hope that helps,

Andy
Hello lost110,

Actually if you write the line int numArr[MAX_ARRAY_SIZE]; as int numArr[MAX_ARRAY_SIZE]{}; the empty {}s will initialize all elements to (0) zero. Which is a good idea in the first place.

Andy

Edit: typo
Last edited on
You are manually managing the number of elements. A std::vector itself manages the number of elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

int main()
{
   // create an empty vector
   std::vector<int> vec1;

   std::cout << "vec1's size: " << vec1.size() << '\n';
   std::cout << "vec1 empty? " << std::boolalpha << vec1.empty() << "\n\n";

   // add some elements to the vector
   vec1.push_back(1);   vec1.push_back(2);   vec1.push_back(3);

   std::cout << "vec1's size is now: " << vec1.size() << '\n';
   std::cout << "vec1 empty? " << std::boolalpha << vec1.empty() << "\n\n";

   // create a sized vector
   std::vector<int> vec2(10);

   std::cout << "vec2's size: " << vec2.size() << '\n';
   std::cout << "vec2 empty? " << std::boolalpha << vec2.empty() << '\n';
}

vec1's size: 0
vec1 empty? true

vec1's size is now: 3
vec1 empty? false

vec2's size: 10
vec2 empty? false
I have not been taught vectors. So I cannot use it!
What if I assign the array with NULL and then insert few elements .

NULL is not that good:
https://channel9.msdn.com/Shows/Going+Deep/Stephan-T-Lavavej-Everything-you-ever-wanted-to-know-about-nullptr

C++ introduced nullptr. As far as I know, the usage of NULL is currently discouraged.

Anyway, NULL was conceived for pointers: you should not use it to assign values to int variables.
https://en.cppreference.com/w/cpp/types/NULL

It might turn out to be zero. It might turn out to be nullptr.

Either way, trying to use it when you actually do mean zero is monstrous (if you want the number zero, just write 0 ; if you want a null pointer, write nullptr ), and also, what happens when the user does actually store the number zero? Your code would think the array hadn't been used when it had been, and you'd overwrite the user's data. That would be a huge bug.

As Handy Andy suggests, just use a counter to track how many items you've put in the array.
Last edited on
Null is assigning zero to the indexes, isn't it?
What does that mean?

Then make a function which checks if all the indexs are Null or not.
It looks like you confuse the index with the content of an array element.

Whether you can use null or 0 or something depends on the type of the array. If you have int as the type 0 might not be an indicator.
pretty much the same question as in your hash question, see answer there.
It doesn't really matter what you choose as a sentry value, it just has to be a value your array is not likely to ever hold.

For an int array, INT_MAX from <climits>.

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
#include <iostream>
#include <climits>

bool empty_array(int[]);
int array_size(int[]);

// have to have some value as a sentry to indicated the end of the array,
// INT_MAX is as good as any
const int SENTRY { INT_MAX };

// similar idea as C-string, one greater than the max size wanted
const int MAX_ARR { 101 };

int main()
{
   // let's create an array with the first value being the sentry
   int arr[MAX_ARR] { SENTRY };

   // let's confirm the first element is the sentry
   // (no need to check all the elements)
   for (int i = 0; i < 5; i++) { std::cout << arr[i] << ' '; }
   std::cout << "\n\n";

   std::cout << "The array's size is: " << array_size(arr) << '\n';

   std::cout << "The array is ";
   if (!empty_array(arr)) { std::cout << "NOT "; }
   std::cout << "empty.\n\n";

   // let's add some elements
   for (int i = 0; i < 5; i++) { arr[i] = (10 * i) + 100; }

   // DO NOT FORGET you have to manually place the sentry EVERY TIME!!!!!
   // adding or removing values
   arr[5] = SENTRY;

   std::cout << "The array's size is: " << array_size(arr) << '\n';

   for (int i = 0; i < array_size(arr); i++) { std::cout << arr[i] << ' '; }
   std::cout << '\n';
}

bool empty_array(int arr[])
{
   if (arr[0] == SENTRY) { return true; }
   else { return false; }
}

int array_size(int arr[])
{
   for (int i { }; i < MAX_ARR; i++)
   {
      if (arr[i] == SENTRY) { return i; }
   }

   return -1;  // error!!!!
}

2147483647 0 0 0 0

The array's size is: 0
The array is empty.

The array's size is: 5
100 110 120 130 140

This is not even remotely bullet-proof code, just a quick mash-up to show why using C++ containers is a good idea.

A marginally better idea would be to use a struct/class that contains two data members:

An array fixed at a large size, and a variable to indicate the number of elements.

The struct/class could have member functions for returning the size, if it is empty, element access, and adding/removing array members.

IOWs, you are rewriting a C++ container.
Last edited on
Can anyone tell a simple way to check if an array is full or not?
An array is always full. If you define int myArray[SOME_SIZE] (where SOME_SIZE is a constant) then there are always exactly SOME_SIZE ints in the array.

If you're asking how many elements you're using in the array then that's something you must keep track of yourself. The typical way for beginners to do this is to create an array with more elements than you expect to use, and then keep a separate variable to track how many items you have in use.

Hope this helps.
^^^ It won't help because he is doing hashing, if its the same question/issue.
hashing puts the data all over the array, not in a solid block. So you can't just track # used, you have to track used locations or use a sentinel idea. Or make the table a table of something that can help you out here, like a table of vectors.
Topic archived. No new replies allowed.