SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

5.8.3 REDIM

FORMAT:


        REDIM array_name (int_expr, int_expr...) ... 
 
             OR 
 
        REDIM array_name [( [int_expr TO] int_expr, 
                      [int_expr TO] int_expr... )] ... 

EXAMPLE:

Example 5-19 REDIM statement

  dim name$(2) 
  input 'How many names': num 
  redim name$(num) 
  for i = 1 to num 
    input 'Enter a name': name$(i) 
  next i 
  do
    print
    for i = 1 to num 
    if  name$(i) = ''  then
      print i; ' '; 'empty slot' 
    else
      print i; ' '; name$(i) 
    end if
    next i 
    print
    input 'How many names': num 
    if  _back or _exit  then exit do
    redim name$(num) 
  loop
  end
 
 
How many names? 3 
Enter a name? Tim 
Enter a name? Sammy 
Enter a name? Fred 
 
1  Tim 
2  Sammy 
3  Fred 
 
How many names? 4 
 
1  Tim 
2  Sammy 
3  Fred 
4  empty slot 
 
How many names? exit 

PURPOSE:

The REDIM statement is used to change the size of an array.

DESCRIPTION:

REDIM redimensions arrays. REDIM can be used only on arrays that have already been dimensioned with the DIM statement. The REDIM statement has the same rules, options and limits as the DIM statement.

Arrays can be dynamically expanded as needed. If you REDIM a single dimension array or the first dimension of a multi-dimensioned array to a larger size, the old values are kept. If you REDIM any array to a smaller size or REDIM two or more dimensions in a multi-dimensioned array to a larger size, the old values are lost.

If your application depends on REDIM initializing all array values, change your code as follows:


        Old Code:       REDIM X(100) 
 
        New Code:       REDIM X(1) 
                        REDIM X(100) 

The REDIM X(1) forces all array values to be initialized by the second REDIM statement.

5.8.4 OPTION BASE

FORMAT:


        OPTION BASE [0 | 1] 

EXAMPLE:

Example 5-20 OPTION BASE statement

  option base 0 
  dim name$(4) 
  for i = 0 to 4 
    input 'Enter a name': name$(i) 
    print i; ' Hello, '; name$(i) 
  next i 
  end
 
 
Enter a name? June 
 0  Hello, June 
Enter a name? Tony 
 1  Hello, Tony 
Enter a name? Sandy 
 2  Hello, Sandy 
Enter a name? Carl 
 3  Hello, Carl 
Enter a name? Liz 
 4  Hello, Liz 

PURPOSE:

OPTION BASE is used to set the default low bound for arrays to suit your needs. You have the option of starting the array with element O or element 1.

DESCRIPTION:

When no low bound is specified for a dimension, the default is 1. The OPTION BASE statement lets you specify a default low bound of 0 or 1. When any following DIM or REDIM statements are executed, SheerPower defaults the low bound to 0 or 1 as specified.


Chapter 6
Built-in Functions

SheerPower has numerous built-in functions. This chapter describes the system and other built-in functions.

6.1 Common Math Functions

The following are common math functions that SheerPower performs:

6.1.1 CEIL(num_expr)

CEIL(x) returns the ceiling of x. The ceiling of x is equal to the smallest integer that is not less than x.

Example 6-1 CEIL function

  print ceil(1.543) 
 
 
2 

6.1.2 DIV0(num_expr1, num_expr2)

The DIV0 function divides num_expr1 by num_expr2. If num_expr2 (divisor) is 0, 0 is returned.

Example 6-2 DIV0 function

  print div0(0.8, 0.000004) 
  print div0(0.8, 0.0) 
  print div0(6, 3) 
  print div0(6, 0) 
  end
 
 
200000 
0 
2 
0 

6.1.3 FP(num_expr)

Given a number, the FP function returns the fractional part of the number. See Section 6.1.6, IP(num_expr).

Example 6-3 FP function

  print fp(238.304) 
  
  
 .304 

6.1.4 INT(num_expr)

INT returns the whole portion of a real number as a real number.

Example 6-4 INT function

  print int(148.8432) 
  
  
148 

6.1.5 INTEGER(num_expr)

INTEGER changes any numeric expression into an integer value and assigns the integer value to the variable specified.

Example 6-5 INTEGER function

  z = integer(4 + (993 * 35)) 
  print z 
  end
  
  
34759 

6.1.6 IP(num_expr)

IP truncates the value of a real number at the decimal point and returns the integer portion. See Section 6.1.3, FP(num_expr).

Example 6-6 IP function

  print ip(1234.56) 
  
  
1234 

6.1.7 MAX(num_expr, num_expr)

MAX(x,y) returns the larger of the two values x and y. See also "MIN function".

Example 6-7 MAX function

  print max(5, 9) 
 
 
9 

6.1.8 MIN(num_expr1, num_expr2)

MIN(x,y) returns the lesser of the values x and y. See also "MAX function".

Example 6-8 MIN function

  x = 43 
  y = 19 
  print min(x, y) 
  
  
19 

6.1.9 MOD(num_expr1, num_expr2)

MOD gives the remainder of one number divided by another.

Example 6-9 MOD function

  print mod(36, 13) 
  
  
10 

6.1.10 REAL(num_expr)

REAL changes any numeric expression into a real or floating-point value and assigns the real value to the variable specified.

Example 6-10 REAL function

  input 'Your age': age% 
  let decimal_age = real(age%) 
  print 'Your number is'; decimal_age 
  end
  
  
Your age? 31 
Your number is 31 

6.1.11 REMAINDER(num_expr1, num_expr2)

REMAINDER(x,y) returns the remainder when X is divided by Y. It differs subtly from MOD. MOD(-4,3) = 2 while REMAINDER(-4,3) = -1.

Example 6-11 REMAINDER function

 
  print remainder(-4,3) 
  
  
-1 

6.1.12 RND

or

RND(num_expr)

RND returns a random number greater than or equal to zero and less than one. If a numeric expression (num_expr) is given, RND returns a whole number between one and the numeric expression.

Example 6-12 RND function

  print rnd
  
  
 .9409720199182629 

6.1.13 ROUND(num_expr [, int_expr])

ROUND rounds a num_expr to the specified number of decimal places (int_expr). The default int_expr is 0.

Example 6-13 ROUND function

  print round(21.83492, 2) 
  
  
21.83 

6.1.14 TRUNCATE(num_expr, int_expr)

This function truncates a real number to a given number of decimal places.

Example 6-14 TRUNCATE function

  print truncate(123.45678, 2) 
  print truncate(123.45678, 4) 
  end
  
  
123.45 
123.4567 


Previous Next Contents Index