So my lab instructor told me I can rotate my function in a clockwise fashion by using modular within the array (see last 6 lines), thereby helping me move the entire array's elements depending on how much the user wants to rotate.
When I tried it didn't work, can it really do that though?
#include <iostream>
#include <iomanip>
usingnamespace std;
void turn(int [], int &);
void main()
{
int numbers [10];
int turns;
char k;
bool done;
done=false;
cout<<"Enter 10 numbers: ";
for (int i=0;i<10;i++)
{
cin>>numbers [i];
}
cout<<"Enter the number of turns that will be made (value must be positive)";
cout<<"\n";
cout<<"x= ";
while (!done)
{
cin>>turns;
if (turns<0)
{
done=false;
}
else
{
done=true;
}
}
cout<<"\n";
turn (numbers, turns);
cout<<"\n";
cout<<"Enter any character to continue...";
cout<<"\n";
cin>>k;
}
void turn(int numbers[], int &turns)
{
int backup[10];
int j;
char k;
if (turns==0)
{
cout<<"Number arrangement will remain the same.";
cout<<"\n";
cout<<"Enter any character to exit...";
cout<<"\n";
cin>>k;
exit (1);
}
for (int k=0;k<10;k++)
{
backup[k]=numbers[(k-turns)%10]; //Turns is x
cout<<backup[k]<<" ";
}
}
Suppose k is 3 and n is 6.
So the 1st array element should have 4th array element after turning.
i.e backup[0] = numbers[4];
But for boundary cases we use modular operation.
i.e backup[k] = numbers[(k+turns)%10];