-1

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;
}

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by eng.M
0

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

0

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

0

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.

0

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.

0

thanks

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

??

0

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.

0

thanks alot :)

Edited by eng.M: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.