How to resolve the algorithm Jensen's Device step by step in the C programming language
How to resolve the algorithm Jensen's Device step by step in the C programming language
Table of Contents
Problem Statement
This task is an exercise in call by name. Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report. The following program was proposed to illustrate the technique. It computes the 100th harmonic number: The above exploits call by name to produce the correct answer (5.187...). It depends on the assumption that an expression passed as an actual parameter to a procedure would be re-evaluated in the caller's context every time the corresponding formal parameter's value was required. If the last parameter to sum had been passed by value, and assuming the initial value of i were 1, the result would have been 100 × 1/1 = 100. Moreover, the first parameter to sum, representing the "bound" variable of the summation, must also be passed by name (or at least by reference), otherwise changes to it (made within sum) would not be visible in the caller's context when computing each of the values to be added. (On the other hand, the global variable does not have to use the same identifier, in this case i, as the formal parameter.) Donald Knuth later proposed the Man or Boy Test as a more rigorous exercise.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Jensen's Device step by step in the C programming language
Source Code 1:
This C code calculates the sum of a series of terms, where each term is computed using a function called term()
. It uses a loop to iterate through the terms and accumulate their values.
-
Global Variables:
i
: Declares a global integer variable.
-
sum
Function:- Takes four arguments:
*i
: A pointer to an integer variable that will be used as a loop counter.lo
: The lower bound of the loop.hi
: The upper bound of the loop.term()
: A pointer to a function that computes the term value for each iteration.
- Iterates from
lo
tohi
by incrementing*i
in each iteration. - Accumulates the term values computed by calling
term()
into a temporary variabletemp
. - Returns the sum of all the terms (
temp
) after completing the loop.
- Takes four arguments:
-
term_func
Function:- Computes and returns the term value based on the current value of
i
. In this case, it returns1.0 / i
.
- Computes and returns the term value based on the current value of
-
main
Function:- Calls the
sum
function with arguments:&i
: Address of the global variablei
that will be used as the loop counter.1
: Lower bound of the loop.100
: Upper bound of the loop.term_func
: Function pointer to theterm_func
function.
- Prints the result of the sum, which is the sum of the series
1/1 + 1/2 + ... + 1/100
.
- Calls the
Source Code 2:
This C code is a macro implementation of the sum
function. It provides a more concise way to define the loop and accumulation logic.
-
Macro Definition:
- Defines a macro called
sum
that takes four arguments:i
: The loop counter variable name.lo_byname
: The name of the lower bound variable.hi_byname
: The name of the upper bound variable.term
: The expression that computes the term value for each iteration.
- The macro is expanded in-place when it is used in the code.
- Defines a macro called
-
main
Function:- Calls the
sum
macro with arguments:i
: Loop counter variable name.1
: Lower bound of the loop.100
: Upper bound of the loop.1.0 / i
: Expression to compute the term value for each iteration.
- Prints the result of the sum, which is the sum of the series
1/1 + 1/2 + ... + 1/100
.
- Calls the
Both source codes provide ways to calculate the sum of a series of terms, but the second one uses a macro to simplify the code and make it more concise.
Source code in the c programming language
#include <stdio.h>
int i;
double sum(int *i, int lo, int hi, double (*term)()) {
double temp = 0;
for (*i = lo; *i <= hi; (*i)++)
temp += term();
return temp;
}
double term_func() { return 1.0 / i; }
int main () {
printf("%f\n", sum(&i, 1, 100, term_func));
return 0;
}
#include <stdio.h>
int i;
#define sum(i, lo_byname, hi_byname, term) \
({ \
int lo = lo_byname; \
int hi = hi_byname; \
\
double temp = 0; \
for (i = lo; i <= hi; ++i) \
temp += term; \
temp; \
})
int main () {
printf("%f\n", sum(i, 1, 100, 1.0 / i));
return 0;
}
You may also check:How to resolve the algorithm Leap year step by step in the RPL programming language
You may also check:How to resolve the algorithm Terminal control/Cursor movement step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Sidef programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the Java programming language