C++Program to display the circular shift of an array at any position.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],temp,n,i,j,b;
clrscr();
cout<<"Enter the 5 elements in array:";
for(i=0;i<5;i++)
cin>>a[i];
cout<<"Array elements are:\n";
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<"\nEnter the position from which you want to shift";
cin>>b;
i=0;j=b;
while(i<j)
{
temp=a[i];
for(i=0;i<4;i++)
a[i]=a[i+1];
a[i]=temp;
i++;
j--;
}
cout<<"\Now array elements are \n";
for(i=0;i<=5;i++)
cout<<a[i]<<" ";
getch();
}
#include<conio.h>
void main()
{
int a[5],temp,n,i,j,b;
clrscr();
cout<<"Enter the 5 elements in array:";
for(i=0;i<5;i++)
cin>>a[i];
cout<<"Array elements are:\n";
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<"\nEnter the position from which you want to shift";
cin>>b;
i=0;j=b;
while(i<j)
{
temp=a[i];
for(i=0;i<4;i++)
a[i]=a[i+1];
a[i]=temp;
i++;
j--;
}
cout<<"\Now array elements are \n";
for(i=0;i<=5;i++)
cout<<a[i]<<" ";
getch();
}
No comments:
Post a Comment