Home > Blockchain >  Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns

Time:10-09

#include <stdio.h>
/*
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 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;
}

int max_of_four(int a, int b, int c, int d)
{
    if (a > b && a > c && a > d) {
        printf("%d\n", a);
    } else
    if (b > a && b > c && b > d) {
        printf("%d\n", b);
    } else
    if (c > a && c > b && c > d) {
        printf("%d\n", c);
    } else {
        printf("%d\n", d);
    }
    return 0;
}

In this question, we have to find out the greatest number among 4 numbers using functions. What is wrong with my code? Why it is not showing the correct output? Please tell me what I am doing wrong and please correct it.

CodePudding user response:

Your function max_of_four is not returning the maximum number, it's printing out a number and returning 0.

The logic of your code is also wrong in the case when some of the numbers are the same (perhaps you want >= rather than > to handle this).

As a hint, if you have a function max(a, b) which returns the maximum of two numbers, you can find the maximum of four numbers:

int max_of_four(int a, int b, int c, int d) {
    return max(max(a, b), max(c, d));
}

This may help you.

CodePudding user response:

Please tell me what I am doing wrong and please correct it.

What's gone wrong is that you've tried to write intricate code that is too complicated.

Here's a very simple re-write of your function (returning the maximum of the 4 values to the caller.)

int max_of_four( int a, int b, int c, int d ) {
    int max = a;

    if( b > max ) max = b;
    if( c > max ) max = c;
    if( d > max ) max = d;

    return max;
}

Keep things simple and bugs have fewer chances to make you frustrated.

CodePudding user response:

According to the question title

Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them

the function must return the maximum value but your function returns 0.

int max_of_four(int a, int b, int c, int d)
{
    //...
    return 0;
}

Moreover your function will output nothing if at least two arguments have the maximum value.

The function can look for example the following way.

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;
    } 
    else if ( !( c < d ) ) 
    {
        return c;
    } 
    else 
    {
       return d;
    }
}

CodePudding user response:

The first answer here is the best one, according to me. But unfortunately I can't upvote it just yet.

Anyway, if you are writing a function to return the largest number among four numbers, it's a good idea to first declare a variable to store the maximum value, and initialize it to 0 (alternatively, you can also initialize it to the first number given to you).

int max_of_four(int a, int b, int c, int d)
{
  max = 0; //this will store the largest value eventually
  if(a > max)
    max = a;
  if(b > max) //max is equal to "a" right now
    max = b;   
  /* 
    if b is greater than the value stored in max, then replace max with b.
    else, leave max as it is and proceed to the next line of code.
  */
  if(c > max) //max has the largest number among a & b right now
    max = c;
  if(d > max) //max has the largest number among a, b, and c
    max = d;

  return(max); //the largest value among a,b,c,d will be returned
}

The others have already pointed out your coding/logical errors, but here's a gist:

  • Your function is returning 0 irrespective of what you write in the function, above return 0;
  • Your function is unnecessarily printing some numbers when it should actually return a value. You're essentially assigning the return value to another variable, ans, right?
  • Related