Write a program that initialize an array of size 10. Create a generic code to shift array left and right. Your program should be menu based, after each shifting show menu and array to user again and again.
#include <iostream>
#include <vector>
using namespace std;
void shiftleft(vector<double>& a)
{
double x = a[0];
for (int i = 0; i<a.size()-1; i++)
{
a[i] = a[i + 1];
}
a[a.size() - 1] = x;
}
void shiftright(vector<double>& a)
{
double x = a[a.size()-1];
for (int i = a.size()-1; i >0; i--)
{
a[i] = a[i - 1];
}
a[0] = x;
}
int main()
{
vector<double> a(10);
for (int i = 0; i < 10; i++)
{
cin >> a[i];
}
int x = 0;
while (x != 3)
{
cout << "enter 1 to shift left" << endl;
cout << "enter 2 to shift right" << endl;
cout << "enter 3 to exit" << endl;
cin >> x;
if (x == 1)
shiftleft(a);
else if (x == 2)
shiftright(a);
for (int i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
}
}
Comments
Leave a comment