How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Kotlin programming language
How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Kotlin programming language
Table of Contents
Problem Statement
The TPK algorithm is an early example of a programming chrestomathy. It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report The Early Development of Programming Languages. The report traces the early history of work in developing computer languages in the 1940s and 1950s, giving several translations of the algorithm. From the wikipedia entry: The task is to implement the algorithm:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Kotlin programming language
This code is a Kotlin program that takes 11 numbers as input from the user, processes them, and prints the results. Here is a detailed explanation of the code:
-
The program defines a function called
f
that takes a double as input and returns the square root of the absolute value of the input plus 5 times the input cubed. -
The
main
function is the entry point of the program. -
Inside the
main
function, a DoubleArray calledda
is created with a size of 11. This array will be used to store the 11 numbers entered by the user. -
The program prompts the user to enter 11 numbers, and a
while
loop is used to read the input and store it in theda
array. The loop continues until 11 numbers have been entered. -
After the user has entered all 11 numbers, the program prints the sequence of numbers in reverse order.
-
The program then processes the sequence of numbers by iterating through the
da
array and calling thef
function on each element. The result of thef
function is printed to the console. -
If the result of the
f
function is greater than 400.0, the program prints "Overflow!" to the console. Otherwise, the program prints the result of thef
function.
Source code in the kotlin programming language
// version 1.1.2
fun f(x: Double) = Math.sqrt(Math.abs(x)) + 5.0 * x * x * x
fun main(args: Array<String>) {
val da = DoubleArray(11)
println("Please enter 11 numbers:")
var i = 0
while (i < 11) {
print(" ${"%2d".format(i + 1)}: ")
val d = readLine()!!.toDoubleOrNull()
if (d == null)
println("Not a valid number, try again")
else
da[i++] = d
}
println("\nThe sequence you just entered in reverse is:")
da.reverse()
println(da.contentToString())
println("\nProcessing this sequence...")
for (j in 0..10) {
val v = f(da[j])
print(" ${"%2d".format(j + 1)}: ")
if (v > 400.0)
println("Overflow!")
else
println(v)
}
}
You may also check:How to resolve the algorithm Monads/Writer monad step by step in the Java programming language
You may also check:How to resolve the algorithm Hostname step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the AWK programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the ReScript programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Clipper programming language