0

If a, b and c are the lengths of the sides of a triangle, then the area is given by $\sqrt(s(s − a)(s − b)(s − c))$, where s = $\frac{1}{2}$( a + b + c). a) Write a function called triangle which takes as input a, b and c and returns the area of a triangle.

If tried to put this into r-studio so far:

--

area_triangle <- function (a,b,c) {area <- sqrt[s(s − a)(s − b)(s − c)] s=.5*(a+b+c) return(area)}

area_triangle(1,3,5)

--

but it keeps giving errors. if someone can please exlpain.

  • 1
    Did you try to use $s$ before you defined $s$? – JMoravitz Jan 27 '22 at 18:24
  • Try writting first s <- (a+b+c)/2, then area <- sqrt(s*(s-a)*(s-b)*(s-b)), and finally return(area). – azif00 Jan 27 '22 at 18:25
  • "it keeps giving errors" If you look at the errors, do they have any information about what sort of error they were? It very likely told you what you did wrong already. – JMoravitz Jan 27 '22 at 18:26
  • @azif00 I tried what you wrote, but now it gave me this message "Warning message: In sqrt(s * (s - a) * (s - b) * (s - c)) : NaNs produced" – William Smith Jan 28 '22 at 14:43

0 Answers0