Question

In C++, develop a class that supports array rotation. Rotating an array is an operation where...

In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively.

For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]. If we rotate it to the left by 2 (or to the right by 3), we get [3, 4, 5, 1, 2].

Your rotate function only needs to accept arguments in the range -(size - 1) to (size - 1), with negative values indicating a left rotation and positive values a right rotation, though it is reasonably easy to support larger values using modular arithmetic. You may choose to implement the left and right rotations using two different cases. Your class should also support adding an arbitrary number of elements to the array via push_back, similar to a vector. To accomplish this, your class should have data members that store the size (number of elements added) and capacity (allocated size) of the array. When pushing back another element, if size == capacity, you should allocate an array twice as large as the previous array (and update the capacity) and copy the elements in the previous array to the new one, before freeing the old array and updating the pointer data member to point to the new array. At this point, you can add the new element as normal.

  • RotatableArray(): creates an empty array with capacity = 8
  • RotatableArray(const RotatableArray&): copy constructor; makes a deep copy of its argument
  • RotatableArray(RotatableArray&&): move constructor; takes the array from its argument
  • RotatableArray& operator=(const RotatableArray&): copy assignment; makes a deep copy of its argument
  • RotatableArray& operator=(RotatableArray&&): move assignment; takes the array from its argument
  • ~RotatableArray(): destructor; deallocates the array
  • void push_back(int): adds the given integer to the back of the array, doubling the capacity if out of space
  • int pop_back(): removes an element from the array and returns it. If array is empty, no change and returns 0.
  • int& operator[](int): returns a reference to the given element of the array (no bounds checking required)
  • int operator[](int) const: similar to previous, as an accessor
  • void rotate(int): rotates the array; positive arguments rotate to the right, while negative arguments rotate to the left. You may assume the argument is in the range -(size - 1) to size - 1.
  • ostream& operator<<(ostream&, const RotatableArray&) **Not a member function**: prints the elements of the array, separated by spaces. You may print an extra space at the end for convenience

***SEE HEADER FILE BELOW***

#ifndef __ROTATE_ARRAY_H
#define __ROTATE_ARRAY_H

#include
using namespace std;

const int DEFAULT_CAPACITY = 8;

//Class for storing an extensible array
//of ints that can rotate
class RotatableArray
{
friend ostream& operator<<(ostream&, const RotatableArray&);
private:
int* data;
int size;
int capacity;
public:
//Default constructor
//Creates an empty RotatableArray
RotatableArray();

//Copy and move constructors and assignment
RotatableArray(const RotatableArray&);
RotatableArray(RotatableArray&&);
RotatableArray& operator=(const RotatableArray&);
RotatableArray& operator=(RotatableArray&&);
//Destructor
~RotatableArray();

//Basic getters
int getSize() const { return size; }
int getCapacity() const { return capacity; }

//Adding, removing, and accessing elements
void push_back(int);
int pop_back();
int& operator[](int);
int operator[](int) const;

//Array rotation
//Positive argument: rotate right
//Negative argument: rotate left
void rotate(int);
};

//Print function
//Separates elements with spaces
//May add extra space at the end
ostream& operator<<(ostream&, const RotatableArray&);

#endif

***SEE DRIVE FILE TO IMPLEMENT BELOW***

#include
using namespace std;

#include "rotate-array.h"

//Tests rotate function
//Also uses constructor, push_back,
//and print function (cout << arr)
int test1()
{
ofstream out("test1.txt");
RotatableArray arr;
for (int i = 0; i < 10; i++)
arr.push_back(i+1);
out << "Original array:\n" << arr << endl;
  
//Test rotation
arr.rotate(3);
out << "Rotated by 3:\n" << arr << endl;

out.close();
return 0;
}

//Test push_back, pop_back, and operator[]
//Also uses rotate and print function
int test2()
{
ofstream out("test2.txt");
RotatableArray arr;
arr.push_back(1);
out << "rotate(1) and push_back * 10:\n" << arr << '\n';
for (int i = 2; i <= 10; i++)
{
arr.rotate(1);
arr.push_back(i);
out << arr << '\n';
}

//Test operator[]
for (int i = 0; i < arr.getSize(); i++)
arr[i]--;
out << "Decrementing every element:\n" << arr << '\n';

//Test pop_back
out << "rotate(1) and pop_back() * 10:\n";
for (int i = 0; i < 10; i++)
{
arr.rotate(1);
arr.pop_back();
out << arr << '\n';
}
out.close();
return 0;
}

int main()
{
test1();
test2();
return 0;
}

TEST CASE 1:

Original array:
1 2 3 4 5 6 7 8 9 10
Rotated by 3:
8 9 10 1 2 3 4 5 6 7

TEST CASE 2:

rotate(1) and push_back * 10:
1
1 2
2 1 3
3 2 1 4
4 3 2 1 5
5 4 3 2 1 6
6 5 4 3 2 1 7
7 6 5 4 3 2 1 8
8 7 6 5 4 3 2 1 9
9 8 7 6 5 4 3 2 1 10
Decrementing every element:
8 7 6 5 4 3 2 1 0 9
rotate(1) and pop_back() * 10:
9 8 7 6 5 4 3 2 1
1 9 8 7 6 5 4 3
3 1 9 8 7 6 5
5 3 1 9 8 7
7 5 3 1 9
9 7 5 3
3 9 7
7 3
3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Attached the completed implementation. If you get any queries, feel free to comment.

Code Screenshot for Indentation Reference:

#ifndef #define ROTATE ARRAY H ROTATE ARRAY H #include <iostream> #include <cstring> #include <cstdlib> using namespace std;

Sample Output:

test1.txt:

Original array:
1 2 3 4 5 6 7 8 9 10
Rotated by 3:
8 9 10 1 2 3 4 5 6 7

test2.txt:

rotate(1) and push_back * 10:
1
1 2
2 1 3
3 2 1 4
4 3 2 1 5
5 4 3 2 1 6
6 5 4 3 2 1 7
7 6 5 4 3 2 1 8
8 7 6 5 4 3 2 1 9
9 8 7 6 5 4 3 2 1 10
Decrementing every element:
8 7 6 5 4 3 2 1 0 9
rotate(1) and pop_back() * 10:
9 8 7 6 5 4 3 2 1
1 9 8 7 6 5 4 3
3 1 9 8 7 6 5
5 3 1 9 8 7
7 5 3 1 9
9 7 5 3
3 9 7
7 3
3

Program code to copy:

#ifndef __ROTATE_ARRAY_H
#define __ROTATE_ARRAY_H

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

const int DEFAULT_CAPACITY = 8;


//Class for storing an extensible array//of ints that can rotate

class RotatableArray{
  
    friend ostream& operator<<(ostream&, const RotatableArray&);
  
    private:
        int* data;
        int size;
        int capacity;
    public:
    //Default constructor //Creates an empty RotatableArray
    RotatableArray() {
        // set cappacity to default capacity
        capacity = DEFAULT_CAPACITY;
        size = 0;
        // allocate array
        data = new int[capacity];
    }
    //Copy and move constructors and assignment
    RotatableArray(const RotatableArray& o) {
        // copy values
        capacity = o.capacity;
        size = o.size;
        // allocate array
        data = new int[capacity];
        // copy array elements
        memcpy(data, o.data, sizeof(int) * size);
    }

    RotatableArray(RotatableArray&& o) {
        // copy values
        capacity = o.capacity;
        size = o.size;

        // move array
        data = o.data;
        // set other data null
        o.data = nullptr;
    }

    RotatableArray& operator=(const RotatableArray& o) {
        // delete memory
        delete [] data;
        // copy values
        capacity = o.capacity;
        size = o.size;
        // allocate array
        data = new int[capacity];
        // copy array elements
        memcpy(data, o.data, sizeof(int) * size);
        return *this;
    }

    RotatableArray& operator=(RotatableArray&& o) {
        // delete
        delete [] data;
        // copy values
        capacity = o.capacity;
        size = o.size;

        // move array
        data = o.data;
        // set other data null
        o.data = nullptr;
        return *this;
    }

    //Destructor
    ~RotatableArray() {
        // delete the allocated memory
        delete [] data;
    }
  
    //Basic getters
    int getSize() const { return size; }
    int getCapacity() const { return capacity; }
  
    //Adding, removing, and accessing elements
    void push_back(int item) {
        // check if full then reallocate the memory
        if (size == capacity) {
            // double capacity
            capacity += capacity;
            // reallocate memory
            data = (int *)realloc(data, capacity);
        }
        // add the item and increase size
        data[size++] = item;
    }

    int pop_back() {
        // if size is 0 then return -1
        if (size == 0) {
            return -1;
        }

        // remove and return the last item
        // and decrease size
        return data[--size];
    }
  
    int& operator[](int i) {
        // return element at given index
        return data[i];
    }

    int operator[](int i) const {
        return data[i];
    }
  
    //Array rotation
    //Positive argument: rotate right
    //Negative argument: rotate left
  
    void rotate(int shift) {
      
        // mod shift by size to cover all cases
        // only need to implement right shoft
        shift = shift % size;
        if (shift == 0) {
            return; // 0 then return
        }
        // right shift
        // a temp array
        int *temp = new int[size];
        // copy elemtns
        memcpy(temp, data, sizeof(int) * size);
        // put all elements thent will be from start
        int ind = 0; // the index
        for (int i = (size - shift); i < size; i++) {
            data[ind++] = temp[i];
        }
        // put the shifted items
        for (int i = 0; i < (size - shift); i++ ) {
            data[ind++] = temp[i];
        }
        // delete temp memory
        delete [] temp;
    }
};
  
    //Print function
    //Separates elements with spaces
    //May add extra space at the end
  
    ostream& operator<<(ostream& out, const RotatableArray& arr) {
        /// output items
        for (int i = 0; i < arr.getSize(); i++) {
            out << arr.data[i] << " ";
        }
        // return stream
        return out;
    }
  
#endif

Add a comment
Know the answer?
Add Answer to:
In C++, develop a class that supports array rotation. Rotating an array is an operation where...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT