CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2014

    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?
    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
       // 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)
    i have no idea how to get the second highest number and add it to max

  2. #2
    Join Date
    Mar 2014

    Re: Need help with easy problem

    ignore max2 i was testing somtehing

  3. #3
    Join Date
    Jul 2005

    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003

    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.

  5. #5
    Join Date
    Jul 2013

    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,


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


    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.