0

Ok, so I was getting some pretty off course answers so I thought I would edit this post and add the notes from the textbook for clarity:

Sometimes, the number of elements in the array might be less than the size of the array. For example, the number of elements in an array storing student data might increase or decrease as students drop or add courses. In such situations, we want to process only the components of the array that hold actual data. To write a function to process such arrays, in addition to declaring an array as a formal parameter, we declare another formal parameter specifying the number of elements in the array, as in the following function:

void initialize(int list[], int listSize)
{
int count;
for (count = 0; count < listSize; count++)
list[count] = 0;
}

The first parameter of the function initialize is an int array of any size. When the function initialize is called, the size of the actual array is passed as the second parameter of the function initialize.

Ok, now that I posted the entire example with textbook notes in it, my confusion is why they set the array to zero. The notes give me the impression that this function is allowing a user to use the array for any size that they wish because the size is set to zero which (I am guessing here) allows the user to pick any size array they want? and it will just reset every time back to zero so if you need more or less units for the next time, it will be default to zero so you can fill it again?

2
  • This code is initializing all of the 'slots' in the list array to the value zero. It could be used to initialize an array before re-using it, to ensure old values are left in place. Most compilers will 'default initialize' an array (to zero for integers for instance). Commented Mar 15, 2013 at 1:45
  • Just so you know, your function is not taking an array as a parameter, but rather a pointer to int. Commented Mar 15, 2013 at 1:46

5 Answers 5

2

you said:

I know the function initialize is used to determine the value of the array list by passing the value of the array to listsize

no. it's not true. this function is not to determine something but to INITIALIZE all the array (up to listsize index, btw: it might be dangerous since you can pass listsize greater than this list size in fact) with 0.

and

by passing the value of the array to listsize

no! listsize is here not the value of element, it is array size, look at "for" loop @Jason xD. have you tried to call this function on some array with some listsize?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi cf, thanks for the notes! I actually did mean array size, I just worded it like a fool.
1

If your code was properly formatted, it might be more apparent that the statement list[count] = 0; gets executed each time through your for loop.

That is, it sets an element to zero each time through the loop. The result is that after the loop is complete, all elements in the array will be set to zero.

Comments

0

If you are talking about the "count = 0", then:

The more usual syntax is: for(int blah = 0; blah < max; blah++)

But there's no reason why 'blah' has to be declared inside the for() statement itself:

int blah;
for(blah = 0; blah < max; blah++)

...is also acceptable.

Or:

int blah = 0;
for( ; blah < max; blah++)

Sometimes (but not in your example) it's desired to have 'blah' exist beyond the scope of the for() statement, so you can do something with it afterward:

int fileNum = 0;
for( ; fileNum < maxFiles && file_exists(fileNum); fileNum++)
{
    //...do something...
}

int lastFileWas = fileNum; //Either it's 'maxFile', or the first file that didn't exist.

Another reason for putting variables outside of the for() statement, is when variables are really really large, and it would make the code easier to read if it's outside of the statement:

std::vector< std::pair<std::string, int> >::iterator myIterator = myVector.begin();
for( ; myIterator != myVector.end(); myIterator++)

This would be very messy if it was inside the for-statement itself (std::vector< std::pair >::iterator is a very long variable name to write out that would be messy to cram into a for() statement). (Though this is less a problem with C++11's 'auto' keyword).

If you are talking about the "list[count] = 0;", then:

With arrays, to assign a value, you "index into" the array using the square brackets (called the 'subscript operator'), and you can access individual variables (called 'elements') held in the memory of the array:

int myArray[10]; //10 integers in a block of memory.

myArray[0] = 5; //Accessing the first element in the block of memory.
myArray[3] = 17; //Accessing the fourth element in the block of memory.

In general:

Since you are using C++, you usually (90% of the time) would be better off using a std::vector. Arrays are rather odd, because they are very similar to pointers to blocks of memory, and can't be treated like regular variables. A std::vector, aside from many other benefits, wraps the array so you can treat it like a regular variable (because a vector is one).

2 Comments

Thanks for that! It does sound like Vectors would be more useful. I wish I could use them but we are not allowed to use those yet until we reach that chapter. So, for now, just to prove we figured out arrays, we can't use vectors for this weeks assignment. The exercise before this one was showing how to initialize arrays with a for loop. So, I dont think this one is so much about that as it is about being able to re-size the array variable count. That is why it being set to zero baffles me. Do you see where I am confused at?
With arrays, you can't resize them. Vectors can be resized, but arrays are always a fixed size. Using dynamic memory (which you most likely haven't reached) you can create a new array and then "resize" the array by deleting the old one and creating a new larger one, but that's not what's going on in the code you posted. The code you posted simply initializes/sets every variable in the array to the value 0... because integers (in C++) aren't initialized by default. The "= 0" is setting each int in the array to 0.
0

Another quick way of accomplishing the same thing:

memset(list, 0, sizeof(int)*listSize);

This gets the whole block of memory allocated to list by calculating the size of the data type times the number of elements and setting it all to 0.

1 Comment

Hmm, that sounds like a great shortcut. Alas, I am denied privilage to use such a commnd since we did not go over memset in the book yet. My teacher won't allow us to use it since we didn't go over it in class :(
0

It is doing what it says, it is initializing. In this case the method is just setting every element of list to 0. It is probably more clear if you add braces:

for (count = 0; count < listSize; count++)
{
  list[count] = 0;
}

So based on your updated post, the book's description is saying 1) You may not want to process the whole array because only a portion of it may have valid data 2) In order to write functions to process arrays that behave this way, functions that process these array's must not only take the array as a parameter but also the number of valid elements. 3) We are going to provide an example function initialize which follows the rules we just described BUT nothing in the text actually speaks to end result of initialize.

My above description as well as the other posts provide an accurate description of initialize.

1 Comment

That was very helpful actually, I appreciate your input. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.