There are small easy to fix problems like return 0;
and return rev(n-1, t+1);
. The first one should be just
return;
because you can't return anything from a function returning void
.
The other should be a call to rev()
itself
rev(n-1, t+1);
because that's what recursive functions do (and also because you can't return anything)
Then you should use int main( void )
or at least int main()
Difference between int main() and int main(void)?
Finally, you have a logic error here
x=*t;
*t=*(t+(n-1));
*(t+(n-1))=x;
rev(n-1, t+1);
*(t+(n-1))
will always be the value of the last element of the array: yes you pass n-1
so you expect that if *(t+(n-1))
was 8th element, in the next call it will be 7th, however you are also passing t+1
so *(t+(n-1))
will always be the 8th element of the array.
And even this one is an easy-to-fix problem. You just pass n-2
.
Here's your recursive function
void rev(int n, float *t)
{
float x;
if(n > 0) {
x=*t;
*t=*(t+(n-1));
*(t+(n-1))=x;
rev(n-2, t+1);
}
return;
}
Doing
if(n != 0) {
.... something....
}
return;
Is the same of doing
if(n == 0) {
return;
}
else {
... something...
}
I put n > 0
instead of n != 0
because since n is always initially positive the two conditions are equivalent, however since you pass n-2
if n
is an odd number you are going to have negative values of n
without passing for 0.