Write a function in C that does the following The function m

Write a function in C++ that does the following:

The function must not use any function templates from the algorithms portion of the Standard C++ library.

int moveToBeginning(string a[], int n, int pos);

Eliminate the item at position pos by copying all elements before it one place to the right. Put the item 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:

The function must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning.

Notwithstanding each function\'s behavior described below, all functions that return an int must return 1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we\'re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array.

Solution

int moveToBeginning(string a[], int n, int pos)
{
   int i = 0;  

       string s = a[pos]; /* saving the element which is at pos */

   for (int i = pos; i <=0; i--) /* now running loop from postion towards the begin by decrementing the value of i from pos to 0 */
        arr[i] = a[i - 1];
   a[0] = s;
   return poss;

}

Write a function in C++ that does the following: The function must not use any function templates from the algorithms portion of the Standard C++ library. int m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site