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?
#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;
}
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.
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.
#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
constint SENTRY { INT_MAX };
// similar idea as C-string, one greater than the max size wanted
constint 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) { returntrue; }
else { returnfalse; }
}
int array_size(int arr[])
{
for (int i { }; i < MAX_ARR; i++)
{
if (arr[i] == SENTRY) { return i; }
}
return -1; // error!!!!
}
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.
^^^ 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.