Hi, everyone.
Hope you can help me to answer the question,
i am not familiar with recursive function...and i hope someone could help me..

Shift elements means
e.g.

array[ ] = {1, 2, 3, 4, 5, 6};
if the user input a value, say 2,
the array will become {5, 6, 1, 2, 3, 4},
the elements shift to the right by 2;

You may see the file attached as reference. Thanks.

## All 6 Replies

A recursive function calls itself with slightly modified parameters from those it was called with.
Here's an example of a recursive function.

``````unsigned long factorial(unsigned long n){
if(n>1)
return factorial(n-1);
else
return 1;
}
``````

That means the answer to my question should be

``````void move(int array[ ], int n, int m) {
if (m == 0)
return;
else
return move(array, n, m - 1);
}
``````

???

I try it before, but it doesn't work

``````#include <stdio.h>

void move(int array[], int n, int m);

int main(void) {
int number = {1, 2, 3, 4, 5, 6, 7, 8};
int n= 8;
int m;
int i;

printf("How many place you want to move?");
scanf("%d", &m);

move(number, n, m);
printf("The resulting array is:\n");
for(i = 0; i < n; i++) {
printf("%d ", number[i]);
}
printf("\n");

getchar();
getchar();
return 0;
}

void move(int array[], int n, int m)
{
int i;

if(m == 0) {
return;
}else {
move(array, n, m-1);
if(i+m>n) {
array[i+m] = array[i+m-n];
}
array[m-1] = array[m];

}

}
``````

This is my attempt and it fails again...
can anyone tell me where is my mistake??

You're definitely on the right track.

``````void move(int array[], int n, int m)
{
int i;

if(m == 0) {
return;
}else {
move(array, n, m-1);
if(i+m>n) {
array[i+m] = array[i+m-n];
}
array[m-1] = array[m];

}

}
``````

Now tell us what your i variable is supposed to do? You're reading from it (the `i+m>n` counts as a read) before you give it a value. Which is a no-no. Also, you're not changing it in any way. The key to this is fixing the i. And you probably need a temporary value somewhere to hold what you're over-writing.

Lastly, I screwed up on my earlier attempt to explain a recursive function. The web browser ate my post, and I had to redo it. Well, the second time around, I was annoyed and not nearly as cautious.

``````unsigned long factorial(unsigned long n){
if(n>1)
return factorial(n-1);
else
return 1;
}
``````

Should be:

``````unsigned long factorial(unsigned long n){
if(n>1)
return n*factorial(n-1); //notice the "n*"
else
return 1;
}
``````

is it necessary that you use recursion ? i have another code for the same try it

``````#include<iostream.h>
#include<conio.h>

void main()
{
int A,B;
int n,c,i,r;
cout<<"\n Enter the size : ";
cin>>n;
cout<<"\n Enter the array : ";
i=0;
while(i<n)
{
cin>>A[i];
i++;
}
for(i=0;i<n;i++)
B[i]=A[i];
cout<<"\n Enter the rotation factor : ";
cin>>r;
for(i=0;i<n;i++)
{
c=(i+r)%n;
A[c]=B[i];
}
cout<<"\n The updated array is : ";
for(i=0;i<n;i++)
cout<<A[i]<<"\t";
getch();
}
``````
commented: fantabulous!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1 +0

just try it

``````#include<iostream.h>
#include<conio.h>

void main()
{
int A,B;
int n,c,i,r;
cout<<"\n Enter the size : ";
cin>>n;
cout<<"\n Enter the array : ";
i=0;
while(i<n)
{
cin>>A[i];
i++;
}
for(i=0;i<n;i++)
B[i]=A[i];
cout<<"\n Enter the rotation factor : ";
cin>>r;
for(i=0;i<n;i++)
{
c=(i+r)%n;
A[c]=B[i];
}
cout<<"\n The updated array is : ";
for(i=0;i<n;i++)
cout<<A[i]<<"\t";
getch();
}
``````
commented: good dani +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.