Answer to Question #155729 in C++ for Talha

Question #155729

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.


1
Expert's answer
2021-01-14T16:27:25-0500
#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;
	}
	
}

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