how do i get the larger 2 out of 3 integers?

Pages: 12
does anyone know how to write a simple function that will take 3 ints and find the sum of the higher 2?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
this is what i got so far
int findsum(int a,int b,int c)// will find the highest int and return it to our main program
        {
    int max,max2;// this sets our local variable max
  // next we will find the larger of our first 2 variables
   if( a>=b)
    {    max=a;
       
}   else
        max=b;
        
   // next we will take our local variable which holds max from the first two
   //variables and compare it to our third to get the larger
   if (max<c)
        max=c;


i have no idea how to get the second highest number and add it to max
you can ignore max2 that was me figuring things out
there is actually a very simple feature of c++. the terenary operator (?:) called so because im pretty sure its the only one that actually interacts with three arguments
I am not aware of that function
what would i do in this situation to find out the 2nd highest integer and add it to max?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

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

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

int main()
{
    std::cout << max(1, 2, 3) << '\n' ;
    std::cout << max(3, 2, 1) << '\n' ;
    std::cout << max(3, 1, 2) << '\n' ;
    std::cout << max(1, 3, 2) << '\n' ;
    std::cout << max(2, 1, 3) << '\n' ;
    std::cout << max(2, 3, 1) << '\n' ;
    std::cout << max(-1, 1000, 9) << '\n' ;
    std::cout << max(-90, -80, -70) << '\n' ;
}


http://ideone.com/jG4J5f
cire thank you for your help but I can only use one function i see that you are using two in that solution can you help me with just one function?
#include <iostream>
using namespace std;
int SumOfMax(int,int,int);
void main()
{
int a,b,c;
cout<<"enter three values:";
cin>>a>>b>>c;

int sum = SumOfMax(a,b,c);
cout<<"sum of max two numbers :" ;
cout<< sum <<endl;

}

int SumOfMax(int x, int y, int z)
{
int max1 =0 , max2 =0 ;
if(x>y && x>z)
{
max1=x;
if (y>z)
{
max2=y ;
}
else
{
max2=z ;
}
}
else if(y>z && y>x)
{
max1=y;
if (x>z)
{
max2 = x;
}
else
{
max2 = z;
}
}
else
{
max1 = z;
if (x>y)
{
max2 = x;
}
else
{
max2 = y;
}
}
return max1+max2 ;


}
cire thank you for your help but I can only use one function i see that you are using two in that solution can you help me with just one function?


Simply do what is done in max(int, int) inside the function you want instead of calling it:

1
2
3
4
5
int max(int a, int b, int c)
{
    int biggest = a > b ? a : b ;
    return biggest > c ? biggest : c ;
}


And, please, don't crosspost to different forums.
Last edited on
naveen this works! but its a little too advanced for my class the professor will be confused on how i learned double brackets
}
}
like that
is there a simpler way?
naveens is very messy and can be implemented much better. i dont see why your professor would be confused because there is no such thing as double brackets. for example:
1
2
3
4
5
6
7
void somefunc()
{
    if(bool(1))
    {
        return;
    }
}
the thing is she wants us to write it out with if / else statements i believe
we're only learning about int functions right now its a beginning c++ class
naveens is confusing because i cant make sense of it

there are 2 ifs followed by 2 elses
we havent learned that yet we only learned
if
followed immeditaly by else
its called nested if statements.
we didnt learn nested ifs yet
so is there a simpler way to do it without nested ifs?
naveen2525 did not use code tags and that makes the code more difficult to read.

The ternary operator int result = (cond) ? 7 : 42; does same as if..else:
1
2
3
4
5
6
7
8
9
int result = 0;
if (cond)
  {
    result = 7;
  }
else
  {
    result = 42;
  }
this doesnt help me at all... how does that relate to my problem?
int findsum(int a,int b,int c)// will find the highest int and return it to our main program
{
int max,;// this sets our local variable max
// next we will find the larger of our first 2 variables
if( a>=b)
{ max=a;

} else
max=b;

// next we will take our local variable which holds max from the first two
//variables and compare it to our third to get the larger
if (max<c)
max=c;


i need a way to figure out the 2nd largest variable like this in this type of code not the advanced stuff
#include <iostream>
using namespace std;
int SumOfMax(int,int,int);
void main()
{
int a,b,c;
cout<<"enter three values:";
cin>>a>>b>>c;

int sum = SumOfMax(a,b,c);
cout<<"sum of max two numbers :" ;
cout<< sum <<endl;

}

int SumOfMax(int x, int y, int z)
{
int max1 =0 , max2 =0 ;

max1 = x > y ? x : y ;

if (max1==x)
{
max2 = y>z ? y : z;
}
else
{
max2 = x>z?x:z;
}
return max1 + max2 ;
}
Pages: 12