Algorithm/Pseudo code help for swapping/comparing values
-
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
-
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
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.
-
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
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; endifPatrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
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; endifPatrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
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
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
-
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
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