simple function assignment

Hello.

I am new to the forum and programming in general. I have read the instructions for posting, but if there is any
deviation from the posting rules please let me know.

My problem with the code is. When I input three values the code should compare the three valeus and output the biggest value. The code only compares the two first values. Big1 and big2. Big3 is left out for some reason I cant see with my current level of programming. I would appreciate any help, pointers or hints.


/* Create function called "biggest" receiving two integer values
as arguments and returns the largest of them.*/


#include <iostream>

using namespace std;

int biggest(int b1, int b2, int b3); //Function

int main()
{
int big1;
int big2;
int big3;
cout<<"Input three values"<<endl; // Input from user
cin>>big1;
cin>>big2;
cin>>big3;
cout<<'\n'<<endl;
cout<<biggest(big1, big2, big3)<<endl; //Funktion call

return 0;
}

int biggest(int b1, int b2, int b3){ //Function
int sum=0;
if(b1>b2 || b1>b3){
sum=b1;
}
else if(b2>b1 || b2>b3){
sum=b2;
}
else if(b3>b1 || b3>b2){
sum=b3;
}

return sum;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int biggest(int b1, int b2, int b3){ //Function
    int sum=0;
    if(b1>b2 || b1>b3){
        sum=b1;
    }
    else if(b2>b1 || b2>b3){
        sum=b2;
    }
    else if(b3>b1 || b3>b2){
        sum=b3;
    }

    return sum;

}
Last edited on
Hey and good morning to you. You should change the if statements from || to &&.
Last edited on
If you still want to use || you shouldn't use else if, but only if because, when you use else if, once one of conditions is true, the program won't check other else ifs.
You do use boolean or in your conditions.

An foo or bar is true if at least one of the two is true. Thus, if the foo is true, then there is no point to evaluate the bar at all, because the whole is true no matter what the bar is. That is lazy evaluation.

Boolean and is similar. Both sides must be true for the and to return true. Therefore, if the left side is false, then the right side is never evaluated because the and is definitely false already.


You, however, do not need or or and.
1. lets assume that b1 is the biggest
2. if b2 is larger than biggest, then biggest is b2
3. if b3 is larger than biggest, then biggest is b3
(This approach is solid even when there are N values, rather than just 3.)


PS. Lets assume that you would have the syntax right for your current logic. What would be the biggest according to it, if b1==42 and b2==42 and b3==42 ?
Thank you both for your answers. Most helpfull!! It works now.

-keskiverto-

b1==42 and b2==42 and b3==42
The biggest according to the"biggest" code is 0.
Last edited on
Indeed. Is that a logical result?
The biggest according to the"biggest" code is 0.

The post above is pointing out that you haven't accounted for the situation where they're all equal. You should at least provide a sensible return value in that case, unless you really want to return 0 as an indication that the numbers are equal. But then how would you indicate the biggest when you have 2 negative numbers and 0?

Re-examine the logic, each step could translate directly into a line of code:
1. lets assume that b1 is the biggest
2. if b2 is larger than biggest, then biggest is b2
3. if b3 is larger than biggest, then biggest is b3

Added a if loop in the main to counter the 42.42.42 logic.



#include <iostream>

using namespace std;

int biggest(int b1, int b2, int b3); //Function

int main()
{
string answer;
int big1;
int big2;
int big3;
cout<<"Input three values: "<<endl; // Input from user
cin>>big1;
cin>>big2;
cin>>big3;
if(big1==big2==big3){
cout<<"Oopss, there is no biggest number. Please type three new numbers"<<endl;
}
else{
cout<<'\n'<<endl;
cout<<biggest(big1, big2, big3)<<endl; //Funktion call
}

return 0;
}
 
if (big1==big2==big3)

Your if statement is bogus. You can;t compare three terms like that,
The first two terms will be comapred. The result of that comparison is either true of false. The true or false result will be compared to big3. Not what you want.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I see what you mean. modifed the code again.

sorry for the wrong format. is this better.

1
2
  if(big1==big2 && big1==big3){
 cout<<"Oopss, there is no biggest number. Please type three new numbers"<<endl;
Topic archived. No new replies allowed.