Question

void reverse_in_place(string& s){ int last = s.length() -1; for (int i = 0; i <= last/2;...

void reverse_in_place(string& s){
int last = s.length() -1;
for (int i = 0; i <= last/2; i++){
char temp = s[i]; //i is initially 0
s[i] = s[last-1]; //swapping first and last character
s[last- i] = temp;
}
}

This is a c++ program.

convert the same function for array and vectors.

Homework Answers

Answer #1

//The required function for vectors:

void reverse_in_place_vector( vector <int> &v){ //passing vector to function
int last = v.size() - 1;
for (int i=0; i <= last ; i++ )
    {
        int temp = v[i]; // i is initially zero
        v[i] = v[last]; // swapping first and last character
        v[last] = temp;
        last--;
    }

  
}

//The required function for arrays:

void reverse_in_place_array( int a[] , int n){ // passing array and its size n to function
   int last = n - 1;
    for (int i=0; i <= last ; i++ )
    {
        int temp = a[i];
        a[i] = a[last];
        a[last] = temp;
        last--;
    }

}

Please refer to the screenshot of the code to understand the indentation of the code:

Hope this helped. Please do upvote and if there are any queries please ask in the comments section.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
void phase03(){ int a = atoi(next_input()); int b = atoi(next_input()); int c = atoi(next_input()); int d...
void phase03(){ int a = atoi(next_input()); int b = atoi(next_input()); int c = atoi(next_input()); int d = atoi(next_input()); int targ = 1 << ((hash % 7)+24) | 1 << (hash % 17) | 1 << (hash % 19); int shot = 1 << a | 1 << b | 1 << c | 1 << d; int hit = shot ^ targ; hit = !hit; if(hit){ return; } failure("Shifty bits hit? Xor not it seems..."); } void substring(char *dest, char...
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
2. Consider the following program written in C syntax: void swap(int a, int b) {   ...
2. Consider the following program written in C syntax: void swap(int a, int b) {    int temp;    temp = a;    a = b;    b = temp; } void main() {    int value = 2, list[5] = {1, 3, 5, 7, 9};    swap(value, list[0]);    swap(list[0], list[1]);    swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT