How to fix "error: control reaches end of non-void function [-Werror=return-type]" at HackerRank?

vcboi

Okay, many answers are already provided, but I couldn't really seem to find the solution.

Our task is to find the greatest of four integers.

https://www.hackerrank.com/challenges/functions-in-c/problem

When I run the following code:

#include <stdio.h>

/*
int max_of_four(int a, int b, int c, int d)
{
    if(a>b && a>c && a>d)
    return a;
    
    else if(b>a && b>c && b>d)
    return b;
    
    else if(c>a && c>b && c>d)
    return c;
    
    else
    return d;
}
*/

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    else if(b>c)
    {
        if(b>d)
        return b;
    }
    
    else if(c>d)
    return c;
    
    else
    return d;
}


int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
    
    return 0;
}

I am returned with this error at HackerRank:

Solution.c: In function ‘max_of_four’:
Solution.c:45:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
cc1: some warnings being treated as errors

But the above code does it's job when run on Dev-C++ 5.11.

If we use the function inside the comments max_of_four, and comment out the latter one, the job gets done as well.

If somebody could get me to know, how max_of_four can be tweaked to get the code to work aptly, I'd be grateful.

And what's causing the error?

Is it an error or a warning? If so, why is a warning being treated as error?

dspr

When a function is not void and you use a if statement to return the value, you must check that every case is really handled. This is not ensured by the last else in this case because you use nested statements which don't have a else.

Here is a fixed version :

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    if(b>c) //<====== I removed the else here to fix the implicit
            //        else of the previous form that was not handled
    {
        if(b>d)
        return b;
    }
    
    if(c>d) //<===== Idem
    return c;
    
    else
    return d;
}

Note however that the nested if in you function could be replaced by and which would be more compact :

int max_of_four(int a, int b, int c, int d)
{
    if (a>b && a>c && a>d) return a;
    else if(b>c && b>d) return b; // Here the else keys are kept even 
    else if(c>d) return c;        // if they are not strictly necessary
    else return d;                // because of the return
}

Finally it would also be more reusable to write :

int max_of_two(int a, int b) { return a > b ? a : b; }

Because then you can reuse it for any number of parameters :

int max_of_three(int a, int b, int c) { 
    return max_of_two(max_of_two(a, b), c);
}
int max_of_four(int a, int b, int c, int d) { 
    return max_of_two(max_of_two(a, b), max_of_two(c, d));
}
//And so on

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive