Transcript
Page 1: Lec06-CS110 Computational Engineering

CS110: Models of Computing

Lecture 6V. Kamakoti

14th January 2008

Page 2: Lec06-CS110 Computational Engineering

• 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

Page 3: Lec06-CS110 Computational Engineering

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.

Page 4: Lec06-CS110 Computational Engineering

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

Page 5: Lec06-CS110 Computational Engineering

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

Page 6: Lec06-CS110 Computational Engineering

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 } }}

Page 7: Lec06-CS110 Computational Engineering

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

Page 8: Lec06-CS110 Computational Engineering

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.

Page 9: Lec06-CS110 Computational Engineering

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

Page 10: Lec06-CS110 Computational Engineering

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.

Page 11: Lec06-CS110 Computational Engineering

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

Page 12: Lec06-CS110 Computational Engineering

Solution

0 0 0 0 0 0 0 0

Page 13: Lec06-CS110 Computational Engineering

Solution - Step 1

0 1 0 1 0 1 0 1

Groups of two

Page 14: Lec06-CS110 Computational Engineering

Solution - Step 2

00 01 10 11 00 01 10 11

Groups of four

Page 15: Lec06-CS110 Computational Engineering

Solution - Step 3

000 001 010 011 100 101 110 111

Groups of eight

Page 16: Lec06-CS110 Computational Engineering

Gossip - Step 1

0 1 0 1 0 1 0 1

Groups of two

Page 17: Lec06-CS110 Computational Engineering

Gossip - Step 2

00 01 10 11 00 01 10 11

Groups of four

Page 18: Lec06-CS110 Computational Engineering

Gossip - Step 3

000 001 010 011 100 101 110 111

Groups of eight - k-bits for n = 2k

Page 19: Lec06-CS110 Computational Engineering

Solution at a glance - Step 1

000 001 010 011 100 101 110 111

Page 20: Lec06-CS110 Computational Engineering

Solution at a glance - Step 2

000 001 010 011 100 101 110 111

Page 21: Lec06-CS110 Computational Engineering

Solution at a glance - Step 3

000 001 010 011 100 101 110 111

Page 22: Lec06-CS110 Computational Engineering

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.

Page 23: Lec06-CS110 Computational Engineering

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)

Page 24: Lec06-CS110 Computational Engineering

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

Page 25: Lec06-CS110 Computational Engineering

Example

(1.5,2,1.5,1.5) and k = 5

Yes

All elements not distinct

Page 26: Lec06-CS110 Computational Engineering

Thank You


Top Related