2017-02-11 6 views
0

Ich mache Wettbewerb Codierung und auf der Website dh Hacker mein Programm gibt die "Fehlerkontrolle erreicht Ende der nicht-void-Funktion [-Werror = Rückgabetyp]" aber auf Codeblocks funktioniert gut hier ist mein Code. Fehler im Programm vor Ort, aber nicht auf Codeblocks

#include <math.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <limits.h> 
#include <stdbool.h> 
int count_max_freq(int *a,int n,int i) 
{ static int max_freq=0,index; 
    int t=a[i],f=0; 
    for(int j=i;j<n;j++) 
     if(a[i]==a[j]) 
      f++; 
    if(max_freq<f) 
    { max_freq=f; 
     index=i; 
    } 
    if(i<n) 
     count_max_freq(a,n,i+1); 
    else 
     return a[index]; 
} 
int main(){ 
    int n; 
    scanf("%d",&n); 
    int *types = malloc(sizeof(int) * n); 
    for(int types_i = 0; types_i < n; types_i++){ 
     scanf("%d",&types[types_i]); 
    } 
    // your code goes here 
    printf("%d",count_max_freq(types,n,0)); 
    return 0; 
} 

+2

müssen Sie das beheben: 'if (i 'if (i

Antwort

1

Einer dieser Rückwege gibt nichts zurück (die rekursive) daher die Warnung.

Die Tatsache, dass es funktioniert, ist nur Glück (nicht sicher, was Sie Arbeits sind rufen: „kompiliert“ bedeutet nicht „es richtig läuft“, und es konnte durch bloßes Glück, aber ich würde nicht wetten drauf)

Mein Vorschlag: ersetzen, dass:

if(i<n) 
    count_max_freq(a,n,i+1); // should return the value! 
else 
    return a[index]; 

durch einen ternären Ausdruck, so dass Sie nur eine return-Anweisung haben, keine Warnung, und es funktioniert überall:

return (i<n) ? count_max_freq(a,n,i+1) : a[index];