Help me .. I can not understand this question..

What does the following function do?

void insert(int a[], int& n, int maxsize, int index, int key) {
if (index < 0 || index > maxsize-1) {
;
}
for (int i=n; i>index; i--) {
a = a[i-1];
}
a[index] = key;
n++;
return;
}

Recommended Answers

All 7 Replies

What do you think it does? Write out what each line means and it should be easier to figure out.

but i can't understand the if statment .
and ii read it many time but i cant catch what does this function do ?!

Break it down into each component part:

what is index used to represent?

what is maxsize used to represent?

The what dose this mean:

if(index < 0)

then this:

if(index > maxsize-1)

then this:

||

and finally this:

if (index < 0 || index > maxsize-1)

Describe it in both the literal sense and what it really means.

It inserts the value key at position index into the array a which currently has n used elements.
The if statement checks whether the index is out of bounds and does nothing if it is and also does nothing if it isn't.

thanks

but here which value will be enter to the array;
a = a[i-1];
a[index] = key;

??

Being told probably won't help much unless you take the information provided, break down the code line by line and apply the description to each line. Having said that

a = a[i-1];

shifts each element already in the array one index to the right (higher) starting with the last used index, n - 1, and working backwards to index to prevent overwriting of the information already in a. At the end of the shifting process the value at index is also available at index + 1, so it is "safe" to overwrite the value at index with the value of key.

thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.