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
- Right-click
here and download the template file.
- Load the template file into MPLAB and save it with the
name JL01-27hypotenuse.c
- 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.
- In the main program, declare two unsigned char
variables called x and y, and one floating-point variable
called h.
- 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.
- 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.
- Send x and y to the Hypot( ) function and put the number
returned into h.
- Display h
with two decimal places.
|