back

Solution to exercise 2.1.3

The key to the solution is to calculate the last element of the sequence and then use operator : (colon) to generate the sequence.

<sxh c> a = input(“Give sequence initial term : ”) d = input(“Give sequence difference : ”) N = input(“Number of terms : ”)

a_last = a + (N-1)*d; % sequence's last term

sequence = a:d:a_last; disp(sequence) </sxh> Download the script: nseq.m