Answer to Question #294768 in C++ for Nice

Question #294768

Sample Program Run

Enter the number of test cases: 1

Enter the size of the array: 5

Enter the number of rotation to perform: 2

Enter the elements of the array: 1 2 3 4 5

Resulting array: 4 5 1 2 3


1
Expert's answer
2022-02-07T06:26:49-0500
#include<iostream>
#include<cmath>

using namespace std;

int* Rotation(int* arr, int sz, int numOfrot)
{
	int* res=new int[sz];
	int begin=abs(sz-numOfrot);
	int j=0;
	for(int i=begin;i<sz;i++,j++)
	{
		res[j]=arr[i];
	}
	for(int i=0;i<begin;i++,j++)
	{
		res[j]=arr[i];
	}
	return res;
}

int main()
{
	int testCases, sizeOfArray, numOfRot;
	
	cout<<"Please, enter the number of test cases: ";
	cin>>testCases;
	cout<<"Please, enter the size of array: ";
	cin>>sizeOfArray;
	int* arr=new int[sizeOfArray];
	for(int i=0;i<testCases;i++)
	{
		cout<<"Please, enter the number of rotation to perform: ";
		cin>>numOfRot;
		cout<<"Please, enter the elements of the array: ";
		for(int j=0;j<sizeOfArray;j++)
		{
			cin>>arr[j];
		}
		int* res=Rotation(arr,sizeOfArray,numOfRot);
		cout<<"Resulting array is ";
		for(int k=0;k<sizeOfArray;k++)
		{
			cout<<res[k]<<" ";
		}
		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