Answer to Question #234492 in C++ for Myname

Question #234492
Given an array, write a user defined function to reverse every sub-array formed by
consecutive k elements. The array should be passed by reference to the function.
1
Expert's answer
2021-09-07T23:22:11-0400
#include <iostream>
using namespace std;
void reverse(int array[], int x, int y)
{
    for (int i = 0; i < x; i += y)
    {
        int l = i;
        int r = min(i + y - 1, x- 1);
        while (l < r)
            swap(array[l++], array[r--]);
    }
}
int main()
{
    int array[] = {11, 12, 13, 14, 15, 16, 17, 18};
    int y = 13;
    int x = sizeof(array) / sizeof(array[0]);
    reverse(array, x, y);
    for (int i = 0; i < x; i++)
        cout << array[i] << " ";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS