# Assignment 4.1

Post date: Jun 27, 2010 9:20:11 PM

Well this one was simple enough, just a little algebra. It should be noted though that due to the nature of non-floating point integers, this will only work accurately for perfect squares (ie 3 4 5, 5 12 13, etc), otherwise the answer will be rounded to a whole number. For a more accurate answer, simply replace all "int" declarations with "float" declarations.

code:

/***********************************************************************************/

#include "math.h" // include the Math Library

const int a = 3;

const int h = 5;

int b;

void setup() // run once, when the sketch starts

{

Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println("Lets calculate the leg of a right triangle"); //print message (with carriage return, \n)

Serial.print("a = "); //print message without carriage return

Serial.println(a); //print the value of variable a

Serial.print("h = ");

Serial.println(h);

b = sqrt( h*h - a*a ); //a^2 + b^2 = h^2, so b = sqrt of (h^2 - a^2); this assignment sets variable b to that value

Serial.print("b = ");

Serial.println(b);

}

void loop() // we need this to be here even though its empty

{

}

/***********************************************************************************/

Serial monitor output:

Lets calculate the leg of a right triangle

a = 3

h = 5

b = 4