-6

What is wrong in this code to find the greatest of four numbers using function? This is a question from Hackerrank C++ practice. Please give solution.

This is the error i am getting:

Solution.cpp: In function ‘int max(int, int, int, int)’: Solution.cpp:21:5: error: expected ‘}’ before ‘else’ else { cout << "b is greatest" << endl; } ^~~~ Solution.cpp:9:16: note: to match this ‘{’ if (a > b) { ^ Solution.cpp:22:5: error: no return statement in function returning non-void [-Werror=return-type] } ^ Solution.cpp: At global scope: Solution.cpp:23:1: error: expected declaration before ‘}’ token } ^ cc1plus: some warnings being treated as errors

#include <iostream>
#include <stdio.h>
using namespace std;

/*
    Add `int max_of_four(int a, int b, int c, int d)` here.
    */
int max(int a, int b, int c, int d)
{
    if (a > b) {
        if (a > c) {
            if (a > d) {
                cout << "a is greatest" << endl;
            }
            else {
                cout << "d is greatest" << endl;
            }
        }
        else {
            cout << "c is greatest" << endl;
        }
        else { cout << "b is greatest" << endl; }
    }
}

int main()
{
    int a, b, c, d;

    cin >> a >> b >> c >> d;

    int ans = max(a, b, c, d);

    cout << ans;

    return 0;
}
user438383
  • 5,716
  • 8
  • 28
  • 43
  • why do you think there is something wrong? Is there a compiler error? Unexpected output? – 463035818_is_not_an_ai Sep 06 '21 at 13:50
  • 5
    You promised to return an `int` from your max function but broke your promise causing your program to exhibit undefined behavior. Your compiler should have warned about this. – drescherjm Sep 06 '21 at 13:52
  • 2
    Judging by the prototype and the use, the function is supposed to return a value, not output a message. Your using the non-existent returned value has undefined behaviour. – molbdnilo Sep 06 '21 at 13:53
  • 1
    Don't confuse "output" as in "print a message on the terminal" with "output" as in "return a value to the caller of a function". It's unfortunate that we use the same term for both, but the former usually refers to a *program's* output, while the latter refers to a *function's* output. – molbdnilo Sep 06 '21 at 13:57
  • then what in place of int? – Shashaank Kumar Sep 06 '21 at 13:58
  • What if `b > a`? – Sani Huttunen Sep 06 '21 at 14:00
  • this is the error i am getting... – Shashaank Kumar Sep 06 '21 at 14:01
  • Solution.cpp: In function ‘int max(int, int, int, int)’: Solution.cpp:21:5: error: expected ‘}’ before ‘else’ else { cout << "b is greatest" << endl; } ^~~~ Solution.cpp:9:16: note: to match this ‘{’ if (a > b) { ^ Solution.cpp:22:5: error: no return statement in function returning non-void [-Werror=return-type] } ^ Solution.cpp: At global scope: Solution.cpp:23:1: error: expected declaration before ‘}’ token } ^ cc1plus: some warnings being treated as errors – Shashaank Kumar Sep 06 '21 at 14:01
  • What if `c > a` and `d > c`? PS: do not use hackerrank or similar, they are garbage. – ChrisMM Sep 06 '21 at 14:02
  • 2
    [`std::max( { a,b,c,d } );`](https://en.cppreference.com/w/cpp/algorithm/max) – 463035818_is_not_an_ai Sep 06 '21 at 14:02
  • 1
    @ShashaankKumar Okay, your function find the maximum and prints it, what do you expect `ans` to be when you execute `int ans = max(a, b, c, d);`. It sure won't return the maximum, you did not write it that way. – Quimby Sep 06 '21 at 14:10
  • 1
    If you want to print the text and return the integer you could place return statements after each cout: for example after: `cout << "a is greatest" << endl;` write `return a;` do the same for your couts for the other variables. With that said your logic is not correct. If `a` is not greater than `b` nothing gets printed and you don't check the other conditions. – drescherjm Sep 06 '21 at 14:14
  • If you aren't allowed the modern standard `max`, `int max(int a, int b) { return a > b ? a : b; } int max_of_four(int a, int b, int c, int d) { return max(max(a,b), max(c,d)); }` would work nicely. – molbdnilo Sep 06 '21 at 14:50

4 Answers4

1

The error is in line 22: you put else inside if bracket instead out of it, that's why you get compiler error:

#include <iostream>
#include <stdio.h>
using namespace std;

/*
    Add `int max_of_four(int a, int b, int c, int d)` here.
    */
int max(int a, int b, int c, int d)
{
    if (a >= b && a >= c && a >= d) return a;
    if (b >= a && b >= c && b >= d) return b;
    if (c >= b && c >= a && c >= d) return c;
    return d;
}

int main()
{
    int a, b, c, d;

    cin >> a >> b >> c >> d;

    int ans = max(a, b, c, d);

    cout << ans;

    return 0;
}

This function will return max out of 4 ints.

Michał Turek
  • 701
  • 3
  • 21
  • 3
    That's wrong function should return `int` but you are returning `void`. – foragerDev Sep 06 '21 at 14:05
  • okay i will fix it, now i understand what he wants – Michał Turek Sep 06 '21 at 14:09
  • thank you so much guys got it.....i was not returning anything to the max function ....right!!!! – Shashaank Kumar Sep 06 '21 at 14:14
  • i added return 0 in int max function is it ok? – Shashaank Kumar Sep 06 '21 at 14:15
  • flag question as solved so others will know – Michał Turek Sep 06 '21 at 14:15
  • ***i added return 0 in int max function is it ok?*** No. That would silence the warning or error when treating warnings as errors but not produce the correct value. – drescherjm Sep 06 '21 at 14:17
  • 2
    @ShashaankKumar i corrected it the way that it will ALWAYS return some value, so now even if for ex. a= 1 b =1 c = 1 d =1, function will return 1 instead of undefined result – Michał Turek Sep 06 '21 at 14:19
  • You can simplify this a bit; after the first condition, you know that `a` is not greatest, so you don't need to compare it any more; the answer must be `b`, `c`, or `d`. Similarly, after the second condition has ruled out `b`, you know that it can only be `c` or `d`. The fourth condition is unnecessary since `d` is the only possible answer. – molbdnilo Sep 06 '21 at 14:44
  • supposed you know that the function returns a value on all paths, the last line should be just `return d;`, because just in case you are wrong and you missed something with the `if`s it would be just a wrong result rather than undefined behvior – 463035818_is_not_an_ai Sep 06 '21 at 17:04
  • @ShashaankKumar why did you un accept answer? Is there something wrong? – Michał Turek Sep 07 '21 at 07:50
1

Here is how I would do it:

#include <iostream>

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

int max(int a, int b, int c, int d)
{
    return max(max(a, b), max(c, d)); // Only 3 comparisons
}

int main()
{
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    std::cout << max(a, b, c, d);
}

Two things here:

  1. Avoid using namespace std; because it pollutes the global namespace. See here for more.
  2. In this version, only 3 comparisons are made (instead of possibly 12 if you compare every number against all the others).
Zakk
  • 1,935
  • 1
  • 6
  • 17
0
  1. Hackerrank was not accepting four if's so I used if else and else also in my function of max.

  2. In the original code my mistake was that i was not returning anything to my function which was corrected by returning a, b, c, d in the max function.

     #include <iostream>
     #include <stdio.h>
     using namespace std;
    
    
     int max(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 >= b && c >= a && c >= d) return c;
     else return d;
     }
    
    
    
    int main()
    {
     int a, b, c, d;
    
     cin >> a >> b >> c >> d;
    
     int ans = max(a, b, c, d);
    
     cout << ans;
    
     return 0;
    }
    
  • 1
    it is fine to answer your own question, but you should explain what was wrong with the original code, what you changed and why to make this a good answer – 463035818_is_not_an_ai Sep 06 '21 at 17:05
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 06 '21 at 18:41
0
#include <iostream>
#include <cstdio>
using namespace std;

/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/

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

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