#returning wrong output

20 messages · Page 1 of 1 (latest)

unkempt island
#

hey I am a beginner can anybody tell mw what is wrong with this set of code....

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;

int max_of_four(int a, int b, int c, int d){
int max;
int max1;
int max2;
if (a>b){
int max1 = a;
}
else {
int max1 = b;
}

if (c>d){
    int max2 = c;
}
 else {
 int max2 = d;
 }
 
if (max1>max2){
    int max = max1;
}
 else {
 int max = max2;
 }
return max;

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

int main() {
int a, b, c, d;
cin>>a>>b>>c>>d;
int ans = max_of_four(a, b, c, d);
cout<<ans;

return 0;

}

chilly nestBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

vivid axle
#

;compile

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;

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

int main() {
    int a, b, c, d;
    int ans = max_of_four(4, 3, 2, 1);
    cout<<ans;
    
    return 0;
}```
vale ivyBOT
#
Program Output
4
vivid axle
#

ah

unkempt island
#

yeah bro

#

program is returning 0 every time don't know why

vivid axle
#

oh

#

i see

#

you are redeclaring the variables

#

in the scope of the if statement s

unkempt island
#

ohh thanks for the help bro

#

i aam really starting programming

vivid axle
#

you may want to use an iterative approach to this problem

#

have you learned that?

unkempt island
#

yes buddy

#

!solved

chilly nestBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] returning wrong output