E215   10 1201
Rev. 11 1005
Lab JL01-27 Hypotenuse
20 points
ELT 215
Back to Main Page

Write a program that will do the following:
  • Switches 7 through 4 represent the length of one small side of a right triangle.
  • Switches 3 through 0 represent the length of the other small side of the triangle.
  • The display shows the length of the hypotenuse.

Example Program
In the text box below this one, there is an example program that is similar to the program you'll be writing.

Procedure
  1. Right-click here and download the template file.
  2. Load the template file into MPLAB and save it with the name JL01-27hypotenuse.c
  3. Below the end of the main program, create a function called Hypot that will accept two floating-point numbers and return the length of the hypotenuse as a floating-point number.
    • Don't forget to create a function prototype just below #pragma code (near the top of the template file).
    • Inside the function, use the pythagorean theorem to calculate the hypotenuse from the two numbers sent to the function.
    • To take the square root, use the sqrt( ) function (one of C's standard functions). This function returns the square root of whatever number is inside the parentheses.
    • Because you're using the sqrt( ) function, you need to include the math.h header file. 
  4. In the main program, declare two unsigned char variables called x and y, and one floating-point variable called h.
  5. Within the main program primary loop, put the number contained in switches 3 through 0 into x.
    • Use the bitwise AND operation to mask off switches 7 through 4 and allow switches 3 through 0 to come through.
    • Put the result into x.
  6. Put the number contained in switches 7 through 4 into y.
    • Use the bitwise AND operation to mask off switches 3 through 0 and allow switches 7 through 4 to come through.
    • Shift the result 4 places to the left and put the shifted number into y.
  7. Send x and y to the Hypot( ) function and put the number returned into h.
  8. Display h with two decimal places.









Example Program



 
Back to Main Page