4

Whatever the input, the result is always 0. Why is that ?

#include <stdio.h>
#include <conio.h>

int rekursiv( int v[], int i, int n, int *min );

int main( void )
{
    int v[ 100 ];
    int n, i, min;

    printf( "Shkruanni n: " );
    scanf( "%d", &n );
    printf( "Shkruani elementet e vektorit.\n" );
    for( i = 0; i < n; i++ ){
         scanf( "%d", &v[ i ] );

         }//end for
    min = v[ 0 ];
    i = 1;
    printf( "Minimumi eshte %d.", rekursiv( v, i, n, &min ) );

    getche();
    return 0;
}//end main

int rekursiv( int v[], int i, int n, int *min )
{
    if( i == n - 1 ) {
        return *min;
    }//end if
    else {
        if( *min < v[ i ] ) {
            *min = v[ i ];
        }//end if
        rekursiv( v, i + 1, n, min );
    }//end else

}//end rekursiv
4
  • Perhaps you should include a small description of the code Commented Jun 18, 2015 at 8:19
  • 1
    #include stdio.h #include conio.h? Where did the <> go? Commented Jun 18, 2015 at 8:31
  • Why recursive? This is an overkill. Commented Jun 18, 2015 at 8:33
  • Because it's homework :D Commented Jun 24, 2015 at 14:11

2 Answers 2

6

You should compile with warnings switched on. rekursiv does not always return a value.

Change

rekursiv( v, i + 1, n, min );

to

return rekursiv( v, i + 1, n, min );
Sign up to request clarification or add additional context in comments.

1 Comment

I'm pretty sure both msvc and gcc would give warnings on this with the default settings. I would assume he just didn't read them because everyone knows warnings are normal.
0

In function (int rekursiv( int v[], int i, int n, int *min ) ), must be return integer so your last condition does not return anything. You should check last condition ELSE

else {
         if( *min < v[ i ] ){
             *min = v[ i ];

             }//end if
         rekursiv( v, i + 1, n, min );

         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.