How do I shift my arrays .. starting at a position in the middle of the arrays .. to one place to the right?
I tried using a for loop, but it doesn't work.
Here is my task:
int insert(string a[], int n, string s, int pos, int max);
Insert the string s into the array at index pos, moving the n minus pos existing elements starting at pos one place to the right to make room. Return the index of the element inserted. It is legitimate for pos to be 0 or n. Return -1 if inserting the element would cause the array to have more than max elements. For example,
string h[6] = { "peter", "lois", "meg", "chris", "stewie", "brian" };int k = insert(h, 3, "quagmire", 1, 6); // returns 1
// h is now "peter" "quagmire" "lois" "meg" "stewie" "brian"
and my progress so far:
int insert(string a[], int n, string s, int pos, int max);
int n;
int pos;
if (n + 1 > max)
return -1;
else
for (pos; pos <= n-pos; pos++)
{
a[n-1] = a[n];
}
string s = a[pos];
return pos;