Matua Doc

Solution: Swift programming revision for Level 2

Sample answer (Swift)

This is one complete working solution that matches the requirements in the revision task. It uses functions, a loop, and selection to process the room and furniture data.

Full program

This section will not display correctly in presentation mode. Please press Esc to return to web page view.

func readNumber(prompt: String) -> Double {
    print(prompt)
    var returnValue = -1.0
    guard let userInput = readLine(), let number = Double(userInput), number > 0 else {
        print("Invalid number.")
        return readNumber(prompt: prompt)
    }
    returnValue = number
    return returnValue
}

@main
struct SwiftPlayground {
    static func main() {
        let maximumItemVolume = 2.0

        let roomLength = readNumber(prompt: "Enter the room length: ")
        let roomWidth = readNumber(prompt: "Enter the room width: ")
        let roomHeight = readNumber(prompt: "Enter the room height: ")

        let roomArea = roomLength * roomWidth
        let roomVolume = roomArea * roomHeight

        print("Room area: \(roomArea) m²")
        print("Room volume: \(roomVolume) m³")

        let furnitureVolumes = [1.2, 0.8, 2.5, 0.6, 1.0]

        var totalFurnitureVolume = 0.0

        furnitureVolumes.enumerated().forEach { index, volume in
            print("Item \(index + 1): \(volume) m³")
            if volume > maximumItemVolume {
                print("Oversized item detected.")
            }
            totalFurnitureVolume = totalFurnitureVolume + volume
        }

        let usableVolume = roomVolume - totalFurnitureVolume
        print("Usable volume: \(usableVolume) m³")

    }
}

Breakdown: Input function

func readNumber(prompt: String) -> Double {
    print(prompt)
    var returnValue = -1.0
    guard let userInput = readLine(), let number = Double(userInput), number > 0 else {
        print("Invalid number.")
        return readNumber(prompt: prompt)
    }
    returnValue = number
    return returnValue
}

The function prints a prompt, reads a line, converts it to a Double, and checks it is positive. If the input is invalid, it calls itself again to keep asking until a valid value is entered.

Breakdown: Room calculations

let roomLength = readNumber(prompt: "Enter the room length: ")
let roomWidth = readNumber(prompt: "Enter the room width: ")
let roomHeight = readNumber(prompt: "Enter the room height: ")

let roomArea = roomLength * roomWidth
let roomVolume = roomArea * roomHeight

print("Room area: \(roomArea) m²")
print("Room volume: \(roomVolume) m³")

The room dimensions are read using the helper function, then area and volume are calculated. The values are printed immediately so the user can see the computed results.

Breakdown: Furniture loop + oversized check

let furnitureVolumes = [1.2, 0.8, 2.5, 0.6, 1.0]
var totalFurnitureVolume = 0.0

furnitureVolumes.enumerated().forEach { index, volume in
    print("Item \(index + 1): \(volume) m³")
    if volume > maximumItemVolume {
        print("Oversized item detected.")
    }
    totalFurnitureVolume = totalFurnitureVolume + volume
}

enumerated() provides an index for numbering each item. The if statement flags any item larger than the allowed maximum volume.

Breakdown: Usable volume

let usableVolume = roomVolume - totalFurnitureVolume
print("Usable volume: \(usableVolume) m³")

The usable space is calculated by subtracting the total furniture volume from the room volume. This final value is printed as the result of the program.