find the area of a trapezoid in swift

To find the area of a trapezoid in Swift, you can use the formula:

main.swift
let area = 0.5 * (top + bottom) * height
41 chars
2 lines

where top and bottom are the lengths of the two parallel sides, and height is the distance between them.

Here's an example function that takes in the top, bottom, and height as input parameters and returns the area:

main.swift
func areaOfTrapezoid(top: Double, bottom: Double, height: Double) -> Double {
    let area = 0.5 * (top + bottom) * height
    return area
}
141 chars
5 lines

You can then call this function with the appropriate values to calculate the area, like this:

main.swift
let myTrapezoidArea = areaOfTrapezoid(top: 8, bottom: 6, height: 4)
print("The area of my trapezoid is \(myTrapezoidArea).")
125 chars
3 lines

This would output:

main.swift
The area of my trapezoid is 28.0.
34 chars
2 lines

gistlibby LogSnag