How to resolve the algorithm Forward difference step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Forward difference step by step in the Seed7 programming language

Table of Contents

Problem Statement

Provide code that produces a list of numbers which is the   nth  order forward difference, given a non-negative integer (specifying the order) and a list of numbers.

The first-order forward difference of a list of numbers   A   is a new list   B,   where   Bn = An+1 - An. List   B   should have one fewer element as a result. The second-order forward difference of   A   will be: The same as the first-order forward difference of   B. That new list will have two fewer elements than   A   and one less than   B. The goal of this task is to repeat this process up to the desired order. For a more formal description, see the related   Mathworld article.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Forward difference step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const func array integer: forwardDifference (in array integer: data) is func
  result
    var array integer: diffResult is 0 times 0;
  local
    var integer: index is 0;
  begin
    for index range 1 to pred(length(data)) do
      diffResult &:= -data[index] + data[succ(index)];
    end for;
  end func;

const proc: main is func
  local
    var array integer: data is [] (90, 47, 58, 29, 22, 32, 55, 5, 55, 73);
    var integer: level is 0;
    var integer: number is 0;
    var boolean: firstElement is TRUE;
  begin
    for level range 0 to length(data) do
      firstElement := TRUE;
      for number range data do
        if not firstElement then
          write(", ");
        end if;
        firstElement := FALSE;
        write(number);
      end for;
      writeln;
      data := forwardDifference(data);
    end for;
  end func;

  

You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Tau number step by step in the MAD programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Stem-and-leaf plot step by step in the Scala programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the zkl programming language