L1VM - array functions
L1VM - array functions
I did add new functions to my math-lib-vect
module:
vmath_min_int, vmath_max_int, vmath_average_int, vmath_sort_int_inc, vmath_sort_int_dec
vmath_min_double, vmath_max_double, vmath_average_double, vmath_sort_double_inc, vmath_sort_double_dec
vmath_array_copy, vmath_array_clear
The vmath_min
and vmath_max
functions return the smallest and biggest number of an array.
The vmath_average
function returns the average number of an array.
The vmath_sort
functions sort an array in increasing or decreasing order.
The vmath_array_copy
function copies an array to a new array of the same variable type.
The vmath_array_clear
function sets an array entries to zero.
Here is a full example:
// array-min-max.l1com
// array with values demo
// shows how to set up an array
// and how to access the elements
//
#include <intr.l1h>
#include <vars.l1h>
(main func)
(set const-int64 1 zero 0)
(set const-int64 1 one 1)
(set int64 1 ind 0)
(set int64 1 f 0)
(set int64 1 num 0)
(set int64 1 realind 0)
// set array data using spaces as elements separator
(set int64 10 array 10 5 8 4 3 2 7 23 45 30)
(set int64 10 array_new)
(set int64 1 maxarray 10)
(set int64 1 maxind 9)
(set const-int64 1 Parray 0)
(set const-int64 1 Parray_new 0)
(set int64 1 min 0)
(set int64 1 max 0)
(set double 1 averaged 0.0)
(set string s minstr "min: ")
(set string s maxstr "max: ")
(set string s sortincstr "sorted increasing:")
(set string s sortdecstr "sorted decreasing:")
(set string s averagestr "average: ")
(set string s array_copystr "array_new copy of array:")
(set string s array_clearstr "array_new cleared:")
(zero :math_vect_init !)
(array Parray pointer)
(array_new Parray_new pointer)
// print array
(for-loop)
(((ind maxarray <) f =) f for)
((ind int64_size *) realind =)
(array [ realind ] num =)
print_i (num)
print_n
((ind one +) ind =)
(next)
// get min number
(Parray zero maxind :vmath_min_int !)
(min stpop)
print_n
print_s (minstr)
print_i (min)
print_n
// get max number
(Parray zero maxind :vmath_max_int !)
(max stpop)
print_s (maxstr)
print_i (max)
print_n
// get average
(Parray zero maxind :vmath_average_int !)
(averaged stpop)
print_s (averagestr)
print_d (averaged)
print_n
print_n
// sort array increasing numbers
(Parray maxind :vmath_sort_int_inc !)
(zero ind =)
print_s (sortincstr)
print_n
// print array
(for-loop)
(((ind maxarray <) f =) f for)
((ind int64_size *) realind =)
(array [ realind ] num =)
print_i (num)
print_n
((ind one +) ind =)
(next)
print_n
// sort array decreasing numbers
(Parray maxind :vmath_sort_int_dec !)
(zero ind =)
print_s (sortdecstr)
print_n
// print array
(for-loop)
(((ind maxarray <) f =) f for)
((ind int64_size *) realind =)
(array [ realind ] num =)
print_i (num)
print_n
((ind one +) ind =)
(next)
// copy array
(Parray Parray_new zero maxind int64_size :vmath_array_copy !)
(zero ind =)
print_n
print_s (array_copystr)
print_n
// print array
(for-loop)
(((ind maxarray <) f =) f for)
((ind int64_size *) realind =)
(array_new [ realind ] num =)
print_i (num)
print_n
((ind one +) ind =)
(next)
// clear array
(Parray_new zero maxind int64_size :vmath_array_clear !)
(zero ind =)
print_n
print_s (array_clearstr)
print_n
// print array
(for-loop)
(((ind maxarray <) f =) f for)
((ind int64_size *) realind =)
(array_new [ realind ] num =)
print_i (num)
print_n
((ind one +) ind =)
(next)
exit (zero)
(funcend)
#include <math-lib-vect.l1h>
Here is the output of the program:
$ l1vm prog/array-min-max -q
10
5
8
4
3
2
7
23
45
30
min: 2
max: 45
average: 13.7000000000
sorted increasing:
2
3
4
5
7
8
10
23
30
45
sorted decreasing:
45
30
23
10
8
7
5
4
3
2
array_new copy of array:
45
30
23
10
8
7
5
4
3
2
array_new cleared:
0
0
0
0
0
0
0
0
0
0