Need help with easy problem

Im stuck on a logical problem and cant figure it out im sure it is very easy to most of you. Im trying to figure out the larger 2 out of 3 integers when i call them into a function from main program so far i have

does anyone know how to write a simple function that will take 3 ints and find the sum of the higher 2?

Code:

`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

Re: Need help with easy problem

ignore max2 i was testing somtehing

Re: Need help with easy problem

Quote:

does anyone know how to write a simple function that will take 3 ints and find the sum of the higher 2?

You can write a function to find the highest 2 values in a series of values by keeping track of the highest and second highest while iterating over all values.

However, since you already know the function takes 3 values, there is an easier solution. How can you find the value that is **not** one of the largest two? Given that, how can you compute the sum of the two largest values?

Re: Need help with easy problem

The key to solving a problem like this is to figure out the steps before you write any code. Just using plain English and a pencil and paper, jot down the steps you'd follow if you weren't trying to write a program. Once you understand the algorithm, then you can translate it to code.

Re: Need help with easy problem

Say you have a function max that takes two ints and returns the biggest. As you've probably found out you can find the biggest of three ints like this,

max(max(a,b),c)

Now you instead want to find the biggest of the three sums: a+b, a+c and b+c. Reusing the above solution gives,

max(max(a+b,a+c),b+c)

The advantage of this approach is that if the first solution is correct the second will too. This shows the benefits of a functional approach to programming.

But it's not the fastest approach. You can decide which sum will be the biggest by just making two comparisions of a, b and c.