Tuesday, July 28, 2009

Write algorithm (not in c++):-Find the smallest number from a set of three given numbers?

Input three numbers.


Declare a variable 'small'(stores the smallest number) and assume that small = number1


if(number2%26lt;small)


small = number2;


if(number3 %26lt; small)


small = number3;


Print - "The smallest number is" small;





I hope you can convert this into a proper C++ program, I am not very good at algorithms.

Write algorithm (not in c++):-Find the smallest number from a set of three given numbers?
let the three numbers are a,b,c.


"small" is avariable to hold smallest value.


if((a%26lt;b) and (a%26lt;c))


small= c;


else


{


if((b%26lt;a)and(b%26lt;c))


small=b;


if((c%26lt;a) and(c%26lt;b))


small=c;


}
Reply:Declare your set of 3 number as A, B and C, and Min as variables





To make sure that one number amongst 3 is the smallest you have to compare it to the other 2


Naming A is the smallest if and only if A%26lt;B and A%26lt;C


so your code should be something like that


If A %26lt; B And A %26lt; C Then


Min =A


ElseIf B %26lt; A And B %26lt; C Then


Min = B


ElseIf C %26lt; B And C %26lt; A Then


Min = C


End If


(in vb syntax)
Reply:You could actually do this in one line of code using a couple ternary operators in Java or C#, or anything that supports ( ? : )
Reply:hmmm smalles 3 digit number uhh how about.....000?
Reply:It's dead easy in Python:





s = [1,12,5] # for example


s.sort()


s[0] # returns 1, the minimum value


s[-1] # returns 12, the maximum


No comments:

Post a Comment