venkman@bebop:~$ bc <----- Start up the bc calculator program on cowboy.cns.uni.edu computer. define f (n) { ------------------ if (n <= 1) return (1); return (n * f(n - 1)); } f(3) <----- Use the function f() with the value 3, 6 to calculate 3 factorial. f(5) <-- f(5) outputs 120 120 f is the program, 5 is the input, 120 is result define p(n, r) { a = 1; for (i = n; i >= (n - r + 1); i--) a = a * i; return (a); } p(10,3) 720 p(6,3) 120 define c(n, r) { return ( p(n, r) / f(r) ); } c(10, 3) 120 c(6, 3) 20 c(10, 2) 45 ^d Ctrl+d (Hold down the Control key, then type letter d) (Ctrl+d is signals the bc calculator you are done) ---------------------------------------------------------------- In the bc calculator: operators are: + for addition, - for subtract, * for multiply, / for division and ^ for exponentiation Examples: 2*2*2*2*2*2*2*2 256 2^8 256 scale=2 (26*25*24*23*22*21) / (26^6) .53 There are three special variables, scale, ibase, and obase: scale defines how some operations use digits after the decimal point. The default value of scale is 0. scale=0 (10*9*8*7)/(10^4) 0 scale=3 (10*9*8*7)/(10^4) .504 ibase and obase define the conversion base for input and output numbers. The default for both input and output is base 10. obase=2 <---- Change the output base to binary, base 2 15 1111 255 11111111 <---- 255 in base 10 is 11111111 in binary base 2 512 1000000000 <---- 512 is 2 ^ 9, 2 to the 9th power obase=16 15 F <---- 16 digits in base 16 hexadecimal 255 Use A for 10, B for 11, C for 12, ..., F for 15 FF The letters A through F are used for the highest 512 six digits of the sixteen digits. 200 obase=10 <---- Output base (obase) is set to decimal, base 10 ibase=2 <---- Input base (ibase) is set to base 2 11111110 254 10000 16 100001 33 ---------------------------------------------------------------- for loops in the bc calculator ---------------------------------------------------------------- for ( [expression1] ; [expression2] ; [expression3] ) statement The for statement controls repeated execution of the statement. Expression1 is evaluated before the loop. Expression2 is evaluated before each execu­ tion of the statement. If it is non-zero, the statement is evaluated. If it is zero, the loop is terminated. After each execution of the statement, expression3 is evaluated before the reevaluation of expression2. define p(n, r) { a = 1; for (i = n; i >= (n - r + 1); i--) a = a * i; return (a); } expression1 is --> i = n; expression2 is --> i >= (n - r + 1); expression3 is --> i--; Here is an example of using variables in the bc calculator: rad = 3 pi = 3.14159 area = pi * rad ^ 2 area 28.27431 define a (r) { <--- Define a function named a() return ( 3.14159 * r ^ 2 ); to calculate area of circle. } a(3) 28.27431 a(10) 314.15900 -------------------------------------------------------------- Exercise: Using only the *, / and possibily the ^ operators, write down the expression that could be typed into the bc calculator to calculate how many years it would take to solve the following problem by brute force on a computer: There are 20 different pilots available to be assigned to 20 different flights. The flights differ as to type of aircraft and what time of day and day they occur on and what destination city or base and originating city or base they have. The pilots each have expressed their preferences from highest to lowest. Each airline or some management team for the airline or airlines has listed which pilot is most preferred, 2nd most preferred, down to least preferred. How many ways can the flights be arranged and pilots assigned or matched to them? Suppose a computer is capable of examining 1,000,000 of the possible flight assignments each second. It can check 1 million arrangements per second and keep track of the best scoring arrangement. How many years would it take for this computer to examine all of the flight assignments and when done display the best possible matching of pilots to flights? --------------------------------------------------------------------- Alternative bc definitions of factorial function: define f(n) { a = 1; for (i = 1; i <= n; i++) a = a * i; return (a); } define f(n) { a = 1; for (i = n; i > 1; i--) a = a * i; return (a); }