# lec06-cs110 computational engineering

Post on 22-May-2015

43 views

Embed Size (px)

DESCRIPTION

A keynote on Problem Solving using ComputersTRANSCRIPT

- 1. CS110: Models of Computing Lecture 6 V. Kamakoti 14th January 2008

2. Todays Lecture Contents Solving Quadratic Equations Given ax2 + bx + c = 0 The roots are (-b + SQRT(b2 - 4ac))/2a (-b - SQRT(b2 - 4ac))/2a New things Check if a is 0 Check if b is 0 Check if discriminant is < 0 Calculate SQRT 3. Calculate SQRT Input a double precision float and get a double precision float as output The function sqrt is in like how printf is in There is another function sqrtl also - find out what it does? Use Manual pages in Unix Type the command man sqrt and you shall get the details. 4. The Program #include #include main() { double a,b,c,DISC,R1,R2; printf(Enter a ?n); scanf(%f,&a); //Similarly for b and c 5. The Program if (a == 0) { printf(The equation is not Quadratic n); if (b == 0) printf(No equation at alln); else printf(Root is %fn, -c/b); } else 6. Program { DISC = b*b - 4*a*c; //Operator Precedence if (DISC < 0) { printf(No real solution existsn); } else { //Paranthesis to handle precedence R1 = (-b + sqrt(DISC))/(2*a)); R2 = (-b - sqrt(DISC))/(2*a)); //Print the roots } } } 7. Polynomial Multiplication Input: Coefficients of two polynomials P1 and P2 of degree 5 Output: Coefficients of the polynomial, which is the product of P1 and P2. Do not forget: There are Twelve Inputs and Eleven Outputs 8. Polynomial multiplication (a1x + a0 ) (b1x + b0 ) a1b1x2 + (a0b1 +a1b0 ) x + a0b0 The coefficient of xk is sum of aibjs such that i +j=k In the program you have A0, A1, A5 and B0, B1,, B5 for the coefficients of the input polynomials and C0, C1, ., C10 for the coefficients of the output polynomial. 9. For your problem C0 = A0 * B0; C1 = A0*B1 + B1*A0 . C8 = A3*B5 + A4*B4 + A5*B3 C9 = A4*B5 + B4*A5 C10 = A5 * B5 10. The problem Suppose there are n = 2k persons, each with a certain item of information. In each step, each person can communicate with another person and share all the information he or she knows (including information learned in previous steps). A person cannot communicate with more than one person in any step. Design a communication (gossip) pattern such that after log2n (= k) steps, everyone knows everything. 11. Solution Numbering problem Number N people using integers 0 to N-1. N = 2k Group pairs of two - Number each 0,1 Group pairs of these and prefix 0 to numbers in one pair and prefix 1 to numbers in the other pair. Repeat till only one group exist 12. Solution00000000 13. Solution - Step 101010Groups of two101 14. Solution - Step 20001101100Groups of four011011 15. Solution - Step 3000001010011100Groups of eight101110111 16. Gossip - Step 101010Groups of two101 17. Gossip - Step 20001101100Groups of four011011 18. Gossip - Step 3000001010011100101110Groups of eight - k-bits for n = 2k111 19. Solution at a glance - Step 1000001010011100101110111 20. Solution at a glance - Step 2000001010011100101110111 21. Solution at a glance - Step 3000001010011100101110111 22. Creative Question - 3 You are given a BLACK BOX - nothing is known what is inside it - but it does the following for you Give a sequence of n real numbers and an integer k, the box will output yes or no indicating whether there is a subset of the numbers whose sum is exactly k. It will not tell you the subset. Use this black box to find the subset whose sum is k, if it exists. You are allowed to use the black box exactly n times, where n is the length of the sequence. 23. Example (1.3,.7,2.5,1.7) and k=3YesThe problem is using the above box exactly four times you Should find out the subset that adds to 3, namely (1.3,1.7) 24. Example (1.3,.7,2.5,1.7) and k=3YesAll elements distinct Remove an element and ask if it solves Yes: element not in subset No: element is in the subset 25. Example (1.5,2,1.5,1.5) and k=5YesAll elements not distinct 26. Thank You