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

Pages: 12
Mar 12, 2014 at 5:52am
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
Mar 12, 2014 at 5:53am
you can ignore max2 that was me figuring things out
Mar 12, 2014 at 5:55am
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
Mar 12, 2014 at 5:57am
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?
Mar 12, 2014 at 6:00am
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
Mar 12, 2014 at 6:03am
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?
Mar 12, 2014 at 6:11am
#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 ;


}
Mar 12, 2014 at 6:17am
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 Mar 12, 2014 at 6:18am
Mar 12, 2014 at 6:25am
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?
Mar 12, 2014 at 6:30am
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;
    }
}
Mar 12, 2014 at 6:31am
the thing is she wants us to write it out with if / else statements i believe
Mar 12, 2014 at 6:31am
we're only learning about int functions right now its a beginning c++ class
Mar 12, 2014 at 6:32am
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
Mar 12, 2014 at 6:38am
its called nested if statements.
Mar 12, 2014 at 6:39am
we didnt learn nested ifs yet
Mar 12, 2014 at 6:39am
so is there a simpler way to do it without nested ifs?
Mar 12, 2014 at 6:42am
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;
  }
Mar 12, 2014 at 6:45am
this doesnt help me at all... how does that relate to my problem?
Mar 12, 2014 at 6:48am
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
Mar 12, 2014 at 6:55am
#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