wageeh
wageeh

Reputation: 84

What's wrong with this function that reverses arrays using recursivity? ( code and outcome included )

So this is the code I wrote, trying to reverse that array t using recursivity

#include <stdio.h>
#include <stdlib.h>

void rev(int n, float *t)
{
    float x;
    if(n==0)
  {
     return 0 ;
  }
  else
  {
       x=*t;
       *t=*(t+(n-1));
       *(t+(n-1))=x;
       return rev(n-1, t+1);
  }
}

void main()
{
    int i;
    float t[]={1,2,3,4,5,6,7,8,9};
    int n=sizeof t /sizeof *t;
    rev (n,t);
    for(i=0;i<n;i++) printf("%f",t[i]);
}

I'd like to understand why this solution does not work, I'm not that interested in the solution overall but I want to understand what mistakes I made in this one for it not to work. Outcome after execution

Upvotes: 0

Views: 49

Answers (2)

anotherOne
anotherOne

Reputation: 1573

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.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

For starters a function that has the return type void shall return nothing value. So this statement

return 0 ;

is invalid.

The function swaps two elements so the size of the array must be decremented by 2.

The function can look like

void rev( float a[], size_t n )
{
    if ( !( n < 2 ) )
    {
        float tmp = *a;
        *a = *( a + n - 1 );
        *( a + n - 1 ) = tmp;
        rev( a + 1, n - 2 );
    }
}

and be called like

size_t n = sizeof t /sizeof *t;
rev( t, n);

Here is a demonstrative program.

#include <stdio.h>

void rev( float a[], size_t n )
{
    if ( !( n < 2 ) )
    {
        float tmp = *a;
        *a = *( a + n - 1 );
        *( a + n - 1 ) = tmp;
        rev( a + 1, n - 2 );
    }
}

int main(void) 
{
    float a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    size_t n = sizeof a /sizeof *a;
    
    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%.0f ", a[i] );
    }
    
    putchar( '\n' );

    rev( a, n );
    
    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%.0f ", a[i] );
    }
    
    putchar( '\n' );
    
    return 0;
}

The program output is

1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 

Pay attention to that according to the C Standard the function main shall be declared like

int main( void )

instead of

void main()

Upvotes: 1

Related Questions