emptying an int array

Okay so i need to create a program about sorting and those kinds of things. My professor wants us todisplay an array with several names and array with the link list and an array with the empty list. My question is that after i use an available space from the empty list i am supposed to clear that sapce but i don't how toclearit in order to be blank and it cannot show 0 because that would mean there is an empty space in the position 0 of the names array and that would necesseraly be true. How can i clear an int array to its default form as when i first initialized it.
An int array has no "default form" like you are implying. There is always a number stored there, though it is likely an arbitrary one based on whatever data happened to be lying around in its memory space.
+1 @Zhuge
Hi @jva2794

If you run your program in debug mode you will see that in the array elements (and any other variable) there is different kind of data, unless you initialize the variable with a specific value, so when you create the array its not "empty" or "clear" it always has something as @Zhuge said, so you would need to define which data you need to input and create the code to consider this data as if the variable was "empty"
Oh okay, now i'm confused cause my professor showed us in the way that he draws a box with several rectangles within and says that is the array and he say initialized it has nothing inside and that is how we have to leave when the names array is completely full and there is no space available for new information. So when a new array is intitialized and I display it some data will show?
Random garbage data will show... unless you expressly initialise it.

Consider this:
1
2
int x;
cout << x << endl;


What would you expect to see? Give that a try and run it 10 times or so, then try this:
1
2
int x = 0;
cout << x << endl;


What do you expect to see this time?

So, if you've run those two examples then you're beginning to really learn about programming in C++. So, your array issue, what does this code give you... can you guess?
1
2
int x[9];
for(int j = 0; j < 9; j++) cout << x[j] << endl;


What about this time? Any different to what you expected before? Now try this:
1
2
int x[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int j = 0; j < 9; j++) cout << x[j] << endl;


OK, so initialising arrays isn't as easy, you probably noticed that from the code I gave you... play around with this and try to understand it a bit better.

If you have more questions then please ask. :~)
Last edited on
Hi @jva2794

The arrays you are using will be texts?

If yes, then if you write 0 in it you will "clear" the array because 0 is the end of string character

For example, if you have one string saying "Hello World!" in the output for c++ it will be stored like

'H'0 'e'1 'l'2 'l'3 'o'4 (Space)5 'W'6 'o'7 'r'8 'l'9 'd'10 '!'11 (0)12

So you have 12 characters, including the blank space + the end of string value that is 0

If you set all the values to 0 as a number and not as a character (this would be using the single quote, like this: '0') then your string will look empty because when it tries to display the text the first character is the end of string, but if you set it as '0' then you will see 0 in your string.

I don't know the type of variables you are using, maybe an array of chars, maybe directly using a string but anyway 0 (NULL) represents the end of string and your array will look like "empty"
Topic archived. No new replies allowed.