ECE 1620 - Assignment #2 - Due: 28 Jan 2022


General requirements for all assignments


Specific requirements for this assignment

Example: convert miles to kilometers


  1. a2/p1.c - Fahrenheit to Celsius

    Write a function to convert degrees Fahrenheit to degrees Celsius.

    Degrees Celsius is degrees Fahrenheit, minus 32, times 5/9.


  2. a2/p2.c - Surface Area of Sphere

    Write a function to compute the surface area of a sphere of radius r.

    The area is 4πr2.


  3. a2/p3.c - Birthday Collisions

    Write a function to compute the probability of a collision (i.e. duplicate value) when 5 values are selected randomly from a set of n possibilities. The probability of at least one collision is 1 minus the probability of no collisions:

    Test using n=365.


  4. a2/p4.c - Polynomials

    Write a function to evaluate the following rational polynomial (from page 39) using nested form to minimize the number of multiplications:


Notes

Nested form for polynomial evaluation (Horner's Rule), to minimize number of multiplications:

  Math:  p = a x3 + b x2 + c x + d

     C:  p = a*x*x*x + b*x*x + c*x + d;

         p = (a*x*x + b*x + c)*x + d; // factor out one x

         p = ((a*x + b)*x + c)*x + d; // factor out another x -> nested form
nth degree poly, n multiplications
Textbook, pages 39-40:

The evaluation of long expressions should be broken into several statements.

If we try to evaluate the expression in one statement, it becomes too long to be easily read:

  f = (x*x*x - 2*x*x + x - 6.3)/(x*x + 0.05005*x - 3.14);
We could break the statement into two lines:
  f = (x*x*x - 2*x*x + x - 6.3)/
      (x*x + 0.05005*x  - 3.14);
Another solution is to compute the numerator and denominator separately:
  numerator = x*x*x - 2*x*x + x - 6.3;
  denominator = x*x + 0.05005*x - 3.14;
  f = numerator/denominator;
The variables x, numerator, denominator, and f must be floating-point variables (i.e. double) in order to compute the correct value of f.
The textbook solution is not in nested form; but your program a2/p4.c must use nested form.