0
int moveToEnd(string a[], int n, int pos);

Eliminate the item at position pos by copying all elements after it one place to the left. Put the item that was thus eliminated into the last position of the array. Return the original position of the item that was moved to the end. Here's an example:

string actors[5] = { "peter", "lois", "meg", "chris", "stewie" };
int j = moveToEnd(actors, 5, 1);  // returns 1
// actors now contains:  "peter"  "meg"  "chris"  "stewie"  "lois"

This is what I have so far:

int moveToEnd(string a[], int n, int pos)
{int i = 0;
int initial;
int initial2;
int initial3;
for (i=0; i<n; i++)
    {if (i<pos)
        initial=i-1;
    if (i>pos)
        initial2=i-1;
    else 
        pos + (n-pos);
}}

it is totally wrong but I am stuck trying to figure out how to move the position to the end and than shift all the other elements to the left.

0

2 Answers 2

1
string save = arr[pos];

for (int i = pos; i < n - 1; i++)
   arr[i] = arr[i + 1];

arr[n - 1] = save;
Sign up to request clarification or add additional context in comments.

6 Comments

Thank You! What do I return at the end because it is giving me a message saying no value returned. I was thinking that the new position called save needs to be but that is a string instead of an int
@Brandyn - arr is the input array. In your case it is called a.
Slowly getting somewhere. It now compiles but once it does so it gives an error saying the project is not working. this is what i formatted it to be.
{ int i; string save = a[pos]; for (i = pos; i < n - 1; i++) a[i] = a[i + 1]; a[n] = save; return pos; }
@Brandyn - It should be arr[n - 1] = save.
|
1

You don't need your second argument, as you can get the length of the array with a.length

The basic logic is this:

  • store the element at a[pos] in a temp String variable.
  • iterate from a[pos] to a[a.length - 2] storing a[pos + 1] to a[pos]
  • store the temp String to a[a.length - 1]

Comments

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.