Hello
This program uses a dynamic array , which shifts x elements to the right. the program works, but my question is whether i defined shift_elements(int index,int pl) in the right way or is there a different way to do the function in the main()
thank you
class MyArray {
public:
MyArray(int size);
~MyArray();
int read_element(int index);
void write_element(int index, int value);
void shift_elements(int index,int pl);
private:
int *storage;
};
#include <iostream>
#include "myarray.h"
using namespace std;
int main()
{
int i,size;
int places;
int x;
cout<<"please enter the size of the dynamic array"<<endl;
cin >> size;
cout<<"please enter the dynamic array"<<endl;
MyArray p(size);
for(i=0 ; i<size; i++){
cin >> x;
p.write_element(i, x);
}
cout<<"This is your dynamic array"<<endl;
for(i=0 ; i<size; i++){
cout << p.read_element(i)<<" ";
}
cout<<endl;
cout<<"how many place you want to shift? " <<endl;
cin>>places;
cout<<"This is the shifted array"<<endl;
p.shift_elements(size,places);
}
#include <iostream>
#include "myarray.h"
using namespace std;
MyArray::MyArray(int size)
{
storage = new int [size];
}
MyArray::~MyArray()
{
delete [] storage;
}
int MyArray::read_element(int index)
{
return storage[index];
}
void MyArray::write_element(int index, int value)
{
storage[index] = value;
}
void MyArray::shift_elements(int index,int pl)
{
for(int k=0;k<pl;k++)
{
int temp;
for (int i=0; i<(index -1); i++)
{
temp = storage[index-1];
storage[index-1] = storage[i];
storage[i] = temp;
}
}
for(int e=0;e<index;e++)
{
cout<<storage[e]<<" ";
}
}