How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the C++ programming language
How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the C++ 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 C++ programming language
This code is a C++ program that takes 11 numbers as input and applies a mathematical function to each number. The function is defined by the expression sqrt( abs( n ) ) + 5 * pow( n , 3 )
, where n
is the input number. The program then outputs the result of applying the function to each number, along with a message indicating whether the result is "too large" or not.
Here is a breakdown of the code:
-
The program includes several standard libraries, including
<iostream>
,<cmath>
,<vector>
,<algorithm>
, and<iomanip>
. These libraries provide the functionality needed to read input from the console, perform mathematical operations, store data in vectors, apply transformations to collections of data, and format output. -
The
main
function is the entry point of the program. It declares two vectors,input
andresults
, each of which can hold 11double
values. -
The program prompts the user to enter 11 numbers and reads them into the
input
vector using afor
loop. -
The program uses the
std::transform
function to apply the mathematical function to each number in theinput
vector and store the results in theresults
vector. Thestd::transform
function takes three arguments: an input iterator, an output iterator, and a function that defines the transformation to be applied to each element. In this case, the input iterator isinput.begin()
, the output iterator isresults.begin()
, and the function is a lambda expression that defines the mathematical function. -
The program then uses a
for
loop to iterate over theresults
vector in reverse order (from index 10 to 0). For each number in theresults
vector, the program prints a message to the console that includes the input number, the result of applying the mathematical function to the input number, and a message indicating whether the result is "too large" or not. The program uses thestd::setw
function to format the output so that the input numbers and results are aligned in columns. -
The program returns 0 to indicate successful execution.
Source code in the cpp programming language
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iomanip>
int main( ) {
std::vector<double> input( 11 ) , results( 11 ) ;
std::cout << "Please enter 11 numbers!\n" ;
for ( int i = 0 ; i < input.size( ) ; i++ )
std::cin >> input[i];
std::transform( input.begin( ) , input.end( ) , results.begin( ) ,
[ ]( double n )-> double { return sqrt( abs( n ) ) + 5 * pow( n , 3 ) ; } ) ;
for ( int i = 10 ; i > -1 ; i-- ) {
std::cout << "f( " << std::setw( 3 ) << input[ i ] << " ) : " ;
if ( results[ i ] > 400 )
std::cout << "too large!" ;
else
std::cout << results[ i ] << " !" ;
std::cout << std::endl ;
}
return 0 ;
}
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the J programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the Lua programming language
You may also check:How to resolve the algorithm The Name Game step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Ring programming language