
March 12th, 2014, 12:56 AM
#1
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

March 12th, 2014, 12:56 AM
#2
Re: Need help with easy problem
ignore max2 i was testing somtehing

March 12th, 2014, 03:01 AM
#3
Re: Need help with easy problem
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?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

March 12th, 2014, 07:12 AM
#4
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.

March 13th, 2014, 01:41 PM
#5
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.
Last edited by razzle; March 15th, 2014 at 04:46 PM.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
