Post on 22-May-2015
description
CS110: Models of Computing
Lecture 6V. Kamakoti
14th January 2008
• 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
Today’s Lecture Contents
Calculate SQRT
• Input a double precision float and get adouble precision float as output
• The function “sqrt” is in <math.h> like how“printf” is in <stdio.h>
• There is another function “sqrtl” also - find outwhat it does?
• Use “Manual pages” in Unix– Type the command “man sqrt” and you shall get
the details.
The Program
#include <stdio.h>#include <math.h>main() { double a,b,c,DISC,R1,R2; printf(“Enter a ?\n”); scanf(“%f”,&a); //Similarly for b and c
The Program
if (a == 0) {printf(“The equation is not Quadratic \n”);
if (b == 0) printf(“No equation at all\n”); else printf(“Root is %f\n”, -c/b); } else
Program { DISC = b*b - 4*a*c; //Operator Precedence if (DISC < 0) { printf(“No real solution exists\n”); } else { //Paranthesis to handle precedence R1 = (-b + sqrt(DISC))/(2*a)); R2 = (-b - sqrt(DISC))/(2*a)); //Print the roots } }}
Polynomial Multiplication
• Input: Coefficients of two polynomialsP1 and P2 of degree 5
• Output: Coefficients of the polynomial,which is the product of P1 and P2.
• Do not forget: There are Twelve Inputsand Eleven Outputs
Polynomial multiplication
• (a1x + a0 ) (b1x + b0 )– a1b1x2 + (a0b1 +a1b0 ) x + a0b0
• The coefficient of xk is sum of aibj’s such that i+ j = k
• In the program you have A0, A1,… A5 andB0, B1,…, B5 for the coefficients of the inputpolynomials and C0, C1, …., C10 for thecoefficients of the output polynomial.
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
The problem
• Suppose there are n = 2k persons, each witha certain item of information. In each step,each person can communicate with anotherperson and share all the information he orshe knows (including information learned inprevious steps). A person cannotcommunicate with more than one person inany step. Design a communication (gossip)pattern such that after log2n (= k) steps,everyone knows everything.
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 tonumbers in the other pair.
– Repeat till only one group exist
Solution
0 0 0 0 0 0 0 0
Solution - Step 1
0 1 0 1 0 1 0 1
Groups of two
Solution - Step 2
00 01 10 11 00 01 10 11
Groups of four
Solution - Step 3
000 001 010 011 100 101 110 111
Groups of eight
Gossip - Step 1
0 1 0 1 0 1 0 1
Groups of two
Gossip - Step 2
00 01 10 11 00 01 10 11
Groups of four
Gossip - Step 3
000 001 010 011 100 101 110 111
Groups of eight - k-bits for n = 2k
Solution at a glance - Step 1
000 001 010 011 100 101 110 111
Solution at a glance - Step 2
000 001 010 011 100 101 110 111
Solution at a glance - Step 3
000 001 010 011 100 101 110 111
Creative Question - 3
• You are given a BLACK BOX - nothing isknown what is inside it - but it does thefollowing for you– Give a sequence of n real numbers and an integer
k, the box will output “yes” or “no” indicatingwhether there is a subset of the numbers whosesum 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.
Example
(1.3,.7,2.5,1.7) and k = 3
Yes
The problem is using the above box exactly four times you
Should find out the subset that adds to 3, namely (1.3,1.7)
Example
(1.3,.7,2.5,1.7) and k = 3
Yes
All elements distinct
Remove an element and ask if it solves
Yes: element not in subset
No: element is in the subset
Example
(1.5,2,1.5,1.5) and k = 5
Yes
All elements not distinct
Thank You