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.
***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
Attached the completed implementation. If you get any queries, feel free to comment.
Code Screenshot for Indentation Reference:
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
In C++, develop a class that supports array rotation. Rotating an array is an operation where...
C++ please Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an array of integers. The class contains two data members: array (of type int"), which represents the array of integers, and size (of type int) that represents the size of the array. The class SpecialArray also overloads the following operators: operator: Compares two arrays. An array "A" is smallerhan an array "B" if the size of array "A" is smaller than the size of "B", or if...
Write a small program that uses a function to add array elements from an array into a linked list in-order. Use the following array: const int size=5; int arr[size] = { 4, 1, 7, 2 ,3 }; Output: Array out of order: 4 1 7 2 3 Array in order using Linked List : 1 ,2, 3, 4, 7 Your program should display the linked list in-order using the array given.
In the blank below, write the contents of array arr after the following code is executed up to the comment indicated in the main procedure below? #include <iostream> #include <algorithm> using namespace std; void do something(int list[], const int size); int main() { const int SIZE(10); int arr[SIZE] = { 5, 8, 1, 7, 3, 9, 4, 6, 10, 2 }; do somethingCarr, SIZE); ** Write the contents of array arr in the space below when execution reaches here. */...
Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() { Array<char> c(3); c.setValue(0,'c'); c.setValue(1,'s'); c.setValue(2,'c'); cout << c; Array<int> i(3); i.setValue(0,1); i.setValue(1,2); i.setValue(2,5); cout << i; Array<int> j(3); j.setValue(0,10); j.setValue(1,20); j.setValue(2,50); cout << j; Array<int> ij; ij = i + j; cout << ij;...
C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...
Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....