Member-only story
moveToBeginning: Put the value that was elimiated into the first position of the array
2 min readMay 4, 2022
int moveToBeginning(string a[], int n, int pos);
Eliminate the value at position pos
by copying all elements before it one place to the right. Put the value that was thus eliminated into the first position of the array. Return the original position of the item that was moved to the beginning. Here's an example:
string people[5] = { "juliet", "sierra", "samuel", "echo", "november" };
int s = moveToBeginning(people, 5, 2); // returns 2
// people now contains: "samuel" "juliet" "sierra" "echo" "november"
Psuedocode
int moveToBeginning(string a[], int n, int pos){for (unsigned int i=0; i<n; i++){if (i==0){//switch a[0] with a[pos]/*how to swap is like this:string temp1 = a[i]; //first put a[i] into a variablestring temp2 = a[pos]; //first put a[pos] into a variablea[i]=temp1; //then we do the switcha[pos]=temp2;*///else if it's NOT pos and it's less than pos} else if (i!=pos && i<pos){//switch a[i] with a[i+1]/*how to swap is like this:string temp1 = a[i]; //first put a[i] into a variable