Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Algorithm/Pseudo code help for swapping/comparing values

Algorithm/Pseudo code help for swapping/comparing values

Scheduled Pinned Locked Moved Algorithms
helpalgorithmsquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11777723
    wrote on last edited by
    #1

    My maths is quite poor and I need some help writing an algorithm/pseudo for a simple cost estimator: rules I have three inputs x, y and z. The inputs each have a separate max value that if breached return an error. These max values, X = 20, Y = 15, Z = 30. Now I need a function that compares the in-putted values and if possible swaps the values to keep them under the constraints. Also, Z should always contain the smallest value as long as the swapped value still meets the max value constraints. So for a few examples if the user entered X=15,Y=5,Z=10 the values for y and Z would be swapped to make z the lowest value. With X=5 Y=18 Z=2 X and Y would be swapped with x to fit under max values. With X=10 Y=8 Z=2 nothing would be swapped. X=31 Y=18 Z=10 nothing would be swapped and you would get an error. I have written this codepen so far: http://codepen.io/FredHair/pen/gpvoPR?editors=101[^] It finds the minVal, medVal and maxVal. I just need some help with the next logic steps for swapping the values. The values themself represent a bounding box, orientating an inputted box that would lead to a cost estimate. I have already had a go at writing this into a program but I made numerous logical errors. If anyone can offer any insight into the maths and best way to write this I can then write the code. Any help is greatly appriceted, I hope I have made myslef clear, feel free to look at codepen and ask any questions. Regards, F

    A P 2 Replies Last reply
    0
    • U User 11777723

      My maths is quite poor and I need some help writing an algorithm/pseudo for a simple cost estimator: rules I have three inputs x, y and z. The inputs each have a separate max value that if breached return an error. These max values, X = 20, Y = 15, Z = 30. Now I need a function that compares the in-putted values and if possible swaps the values to keep them under the constraints. Also, Z should always contain the smallest value as long as the swapped value still meets the max value constraints. So for a few examples if the user entered X=15,Y=5,Z=10 the values for y and Z would be swapped to make z the lowest value. With X=5 Y=18 Z=2 X and Y would be swapped with x to fit under max values. With X=10 Y=8 Z=2 nothing would be swapped. X=31 Y=18 Z=10 nothing would be swapped and you would get an error. I have written this codepen so far: http://codepen.io/FredHair/pen/gpvoPR?editors=101[^] It finds the minVal, medVal and maxVal. I just need some help with the next logic steps for swapping the values. The values themself represent a bounding box, orientating an inputted box that would lead to a cost estimate. I have already had a go at writing this into a program but I made numerous logical errors. If anyone can offer any insight into the maths and best way to write this I can then write the code. Any help is greatly appriceted, I hope I have made myslef clear, feel free to look at codepen and ask any questions. Regards, F

      A Offline
      A Offline
      Alan Balkany
      wrote on last edited by
      #2

      Here's a piece of the solution:

      void SwapValues (ref int a, ref int b)
      {
      int temp = a;
      a = b;
      b = temp;
      }

      Now you have to write the "if" statements to determine the calls to SwapValues to get your variables in numeric order.

      1 Reply Last reply
      0
      • U User 11777723

        My maths is quite poor and I need some help writing an algorithm/pseudo for a simple cost estimator: rules I have three inputs x, y and z. The inputs each have a separate max value that if breached return an error. These max values, X = 20, Y = 15, Z = 30. Now I need a function that compares the in-putted values and if possible swaps the values to keep them under the constraints. Also, Z should always contain the smallest value as long as the swapped value still meets the max value constraints. So for a few examples if the user entered X=15,Y=5,Z=10 the values for y and Z would be swapped to make z the lowest value. With X=5 Y=18 Z=2 X and Y would be swapped with x to fit under max values. With X=10 Y=8 Z=2 nothing would be swapped. X=31 Y=18 Z=10 nothing would be swapped and you would get an error. I have written this codepen so far: http://codepen.io/FredHair/pen/gpvoPR?editors=101[^] It finds the minVal, medVal and maxVal. I just need some help with the next logic steps for swapping the values. The values themself represent a bounding box, orientating an inputted box that would lead to a cost estimate. I have already had a go at writing this into a program but I made numerous logical errors. If anyone can offer any insight into the maths and best way to write this I can then write the code. Any help is greatly appriceted, I hope I have made myslef clear, feel free to look at codepen and ask any questions. Regards, F

        P Offline
        P Offline
        Patrice T
        wrote on last edited by
        #3

        This code sort X, Y and Z so that X > Y > Z code between 'then' and 'endif' is swaping values

        if x < y then t= x; x= y; y= t; endif
        if x < z then t= x; x= z; z= t; endif
        if y < z then t= y; y= z; z= t; endif

        Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

        P 1 Reply Last reply
        0
        • P Patrice T

          This code sort X, Y and Z so that X > Y > Z code between 'then' and 'endif' is swaping values

          if x < y then t= x; x= y; y= t; endif
          if x < z then t= x; x= z; z= t; endif
          if y < z then t= y; y= z; z= t; endif

          Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

          P Offline
          P Offline
          Patrice T
          wrote on last edited by
          #4

          I would like to know why my previous message was downvoted ?

          Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

          Richard DeemingR 1 Reply Last reply
          0
          • P Patrice T

            I would like to know why my previous message was downvoted ?

            Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            It's a high level member too - my counter-vote only brought the message back up to a 3. Possibly the same person reported in B&S[^] who's been randomly abuse-voting Lounge messages.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            P 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              It's a high level member too - my counter-vote only brought the message back up to a 3. Possibly the same person reported in B&S[^] who's been randomly abuse-voting Lounge messages.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              P Offline
              P Offline
              Patrice T
              wrote on last edited by
              #6

              Thank you very much. You probably right, and I have been a prey of choice since hi downvoted my answers 5 times in a row. Nota: I am only Silver member, so my upvote will not be as good as yours :)

              Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups