-1

Sir/Ma'am,
I do not know what may be that I'm doing wrong. Logic- wise everything appears correct to me. The braces seem placed in the correct positions as well. Then why is it that I'm receiving the following error messages?

32 8 [Error] expected constructor, destructor, or type conversion before '(' token
33 2 [Error] expected unqualified-id before 'return'
34 1 [Error] expected declaration before '}' token

What am I doing wrong with this particular program? And, I was hoping for a correction using the same statements, and not any other.

Please help!

The program:

#include <stdio.h>
main()
{
    int a, b, c, d, big;
    printf("\n Enter value to a, b, c, d: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (a >b)
    {
        if(a>c)
        {
            if(a>d)
            big = a;
            else
            big=d;
        }
        else
           {
           if(c>d)
            big = c;
            else
            big = d;
        }
        else
            if(b>c)
            {
                if(b>d)
                big = b;
                else
                big = d;
            }
    }
    printf("\n The biggest number is %d", big);
    return 0;
}
Bao
  • 25
  • 1
  • 1
  • 10
  • Constructors and Destructors are not a part of the C language. – Christian Gibbons Oct 30 '19 at 21:49
  • 1
    You can't have an 2nd else statement after another else statement. Around line 23 you need an else if statement. – jmq Oct 30 '19 at 21:57
  • You're using a C++ compiler. You must therefore use an ISO-conformant definition of `main`: `int main()`. In addition to the `else` issue (which is just a misplaced `}` that could have been avoided if you indented code properly), you are missing some logic. Note the "fixed" program compiles but doesn't work: https://godbolt.org/z/RoWlRj – paddy Oct 30 '19 at 22:01
  • Of course, since this is C++, you could just use `int big = std::max(std::max(a, b), std::max(c, d))` – paddy Oct 30 '19 at 22:02
  • This is C, not C++. But yes, the compiler is both C & C++ compatible. @paddy – Bao Oct 31 '19 at 06:32

4 Answers4

1

You can use the ternary operator to solve your problem. Thus, the code simplifies to

int main() {
    int a, b, c, d;
    printf("\n Enter value to a, b, c, d: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    const int big1 = (a > b) ? a : b;
    const int big2 = (c > d) ? c : d;
    int big = (big1 > big2) ? big1 : big2;

    printf("\n The biggest number is %d", big);
}
schorsch312
  • 5,553
  • 5
  • 28
  • 57
0

I do not know what may be that I'm doing wrong. Logic-wise everything appears correct to me.

There are some syntactic errors, though. A construct like

// ...
if ( a > c ) {
    // ...
}
else {               // <----
    // ...
}
else if ( b > c ) {
    // ...
}

Is not valid in C.

It's unclear to me how the OP was planning to structure the nested ifs in order to solve the task, but before going on, I'd suggest to write some simple tests to verify the correctness of the algorithm:

#include <stdio.h>

// Prototype of the function which returns the biggest out of the four arguments
int max_of_four(int a, int b, int c, int d);

// Helper function used to verify the correctness of the results
int test_(int a, int b, int c, int d, int expected)
{
    int result = max_of_four(a, b, c, d);
    if ( result != expected )
    {
        printf("FAILED - Expected: %d (given %d, %d, %d and %d), instead of %d.\n", 
               expected, a, b, c, d, result);
        return 1;
    }
    return 0;
}

// Test runner
int main(void)
{
    int failed = 0;

    // The function should be able to find the biggest regardless of its "position"
    failed += test_(1, 2, 3, 4,  4);
    failed += test_(4, 3, 2, 1,  4);
    failed += test_(2, 1, 4, 3,  4);
    failed += test_(3, 4, 1, 2,  4);

    // The function should manage negative values
    failed += test_(1, -2, -3, -4,    1);
    failed += test_(-4, -3, -2, -1,  -1);
    failed += test_(0, -3, -1, -2,    0);

    // The function should manage duplicate values
    failed += test_(1, -2, 1, 2,      2);
    failed += test_(-4, -3, -3, -5,  -3);
    failed += test_(1, 1, 1, 1,       1);

    if ( failed == 0 )
        puts("So far so good.");

    return 0;
}

// A simple implentation.
int max_of_four(int a, int b, int c, int d)
{
    int big = a;
    if ( b > a )
        big = b;
    if ( c > big )
        big = c;
    if ( d > big )
        big = d;
    return big;    
}

You can test it here and try to rewrite the function using nested if statements, if you want. Maybe something like

int max_of_four(int a, int b, int c, int d)
{
    if ( b > a )
    {
        if ( d > c )
        {
            if ( b > d )
                return b;
            else
                return d;
        }
        else // d <= c
        {
            if ( b > c )
                return b;
            else
                return c;
        }            
    }
    else // b <= a
    {
        // You got the idea...
    }
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
0

I used the ternary operator instead of if-else statements it works the same as if-else you can check how ternary operator works.

int max_of_four(int a,int b,int c,int d){
     return (a < b ? ((b > c)? ((b > d)? b:d) :((c > d)? c: d))  : ((a < c)? ((c > d)? c: d) : ((a > d) ? a : 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;
}
Shivam Suchak
  • 221
  • 3
  • 9
-1

You did not consider case when a <= b or it places in wrong position (inside a < b block).

You can imagine problem in a decision tree similar to this (3 numbers) for clearer.

#include <stdio.h>

main() {
  int a, b, c, d, big;
  printf("\n Enter value to a, b, c, d: ");
  scanf("%d %d %d %d", &a, &b, &c, &d);

  if (a > b) {
    if (a > c) {
      if (a > d)
        big = a;
      else
        big = d;
    } else {
      if (c > d)
        big = c;
      else
        big = d;
    }

  } else {
    if (b > c) {
      if (b > d)
        big = b;
      else
        big = d;
    }
    else {
      if (c > d)
        big = c;
      else
        big = d;
    }
  }

  printf("\n The biggest number is %d", big);

  return 0;
}
Hank
  • 21
  • 8
  • Will you please explain what you did to solve the problem? OP might not notice, and therefore he cannot learn anything from this. Not my down-vote. – virolino Oct 31 '19 at 07:49