CSE1002 Generic Right Shift
Given a set of 'n' elements and 'r', write a generic function to right shift the set of elements by 'r' position. If the elements are to moved to position greater than 'n' then wrap the shift process to the beginning of the collection. For example, if the set of five elements are 1,7,8,9,12 and value of 'r' is 3 then the set of elements would be 8, 9, 12, 1, 7.PSUEDOCODE:
DEFINE right_Shift(T a[],int n,int r)
for i->0 to r
T temp = a[n-1];
for j->n-2 to 0
a[j+1]=a[j];
end for
a[0]=temp;
end for
end definition
CODE:
#include<iostream>
using namespace std;
template <class T>
void right_Shift(T a[],int n,int r)
{
for(int i=0;i<r;i++)
{
T temp = a[n-1];
for(int j=n-2;j>=0;j--)
a[j+1]=a[j];
a[0]=temp;
}
}
No comments:
Post a Comment