Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Finding minimum with a recursive function

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
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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 );
over 4 years ago · Santiago Trujillo Report

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 );

         }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!