SIDE NOTE : Please use C++, thank you! Question #3. Write a program that will Pr
ID: 3706174 • Letter: S
Question
SIDE NOTE : Please use C++, thank you!
Question #3. Write a program that will Programming Using Arrays (4 pts) Prompt for and read the number of integers they want to manipulate (minimum 4) Create an array of integers that size. Read that many integers values which you store in the array. Display the content of the array. Ask the user if they want to rotate the numbers in the array left or right. o Rotate right means: rotate the content of the array to the right by one slot. The last number in the array becomes the first number in the array. o Rotate left means: rotate the content of the array to the left by one slot. The first number in the array becomes the last number in the array. Show the content of the array again. For example if the array contained 1 2 3 4, after the right rotate operation it is to contain 4 1 2 3. 1 2 3 4 rotated left results in 2 3 4 1 in the array Welcome to the ROTATE Progran Here are a couple of Please enter 10 integers separated by a space: L 2 3 4 5 6 7 8 9 10 hich way do you want to rotate?1 for left, 2 for right ple 3 hich way do you want to rotate? 1 for left. 2 for right please 8 hich way do you want to rotate? 1 for left. 2 for right please 1 umbers before rotation: 1 umbers after rotation: 2 rhank you for using the ROTATION progran 2 5 6 2 8 10 3 5 6 10Explanation / Answer
Hello, here is the required code for you. Defined a couple of methods (printArray and rotate) to make the program work easier. Everything is explained in comments included. Drop a comment if you have any doubts. Thank you.
//code.cpp
#include<iostream>
using namespace std;
/*method to print an array of length- size*/
void printArray(int array[],int size){
for(int i=0;i<size;i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
/*method to rotate an array of given size, in given direction.
will rotate left if i is 1, right otherwise*/
void rotate(int *array,int size,int i){
if(i==1){
//rotating left
int temp;
for(int i=0;i<size-1;i++){
if(i==0){
//storing first element in a temp variable
temp=array[0];
}
//continuously shifting elements to the left
array[i]=array[i+1];
}
//updating the last element with previously stored value
array[size-1]=temp;
}else{
//rotating right
int temp;
for(int i=size-1;i>0;i--){
if(i==(size-1)){
//storing last element in a temp variable
temp=array[size-1];
}
//continuously shifting elements to the right
array[i]=array[i-1];
}
//updating the first element with previously stored value
array[0]=temp;
}
}
int main(){
int size=10; //size of array
int array[size]; //defining an array
cout<<"Please enter 10 integers seperated by a space"<<endl;
//initializing the array
for(int i=0;i<size;i++){
cin>>array[i];
}
int input;
//continuosly looping until user enter either 1 or 2
do{
cout<<"Which way do you want to rotate? (1 for left, 2 for right): ";
cin>>input;
}while(input!=1 && input!=2);
//displaying numbers before rotation
cout<<"Numbers before rotation: ";
printArray(array,size);
//rotating in given direction
rotate(array,size,input);
//displaying numbers after rotation
cout<<"Numbers after rotation: ";
printArray(array,size);
return 0;
}
/*OUTPUT 1*/
Please enter 10 integers seperated by a space
1 2 3 4 5 6 7 8 9 10
Which way do you want to rotate? (1 for left, 2 for right): 1
Numbers before rotation: 1 2 3 4 5 6 7 8 9 10
Numbers after rotation: 2 3 4 5 6 7 8 9 10 1
/*OUTPUT 2*/
Please enter 10 integers seperated by a space
1 2 3 4 5 6 7 8 9 10
Which way do you want to rotate? (1 for left, 2 for right): 4
Which way do you want to rotate? (1 for left, 2 for right): 2
Numbers before rotation: 1 2 3 4 5 6 7 8 9 10
Numbers after rotation: 10 1 2 3 4 5 6 7 8 9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.