326 Introduction to C++ Programming and Graphics
- In C++, all variables must be declared. Variable declaration is not nec-
essary inMatlab; some declarations are necessary inFortran 77.
InFortran 77, all variables beginning with the lettersI,J,K,L,M,N
(ori,j,k,l,m,n) are integers, while all other variables are real, regis-
tered in single precision. These defaults can be changed with appropriate
data type declarations.
For example, the statement:
Implicit Double Precision (a-h,o-z)
declares that all variables whose names begin with a–h and o–z (or A–H
and O–Z) are registered in double precision.
- In C++ andFortran 77, variables are not necessarily initialized to zero.
InMatlab, all variables must be given initial values. - InMatlab, a variable can change from integer to real, and vice versa, in
the course of a calculation. Not being bothered with variable types is an
extremely appealing feature ofMatlab. The penalty possible confusion
and a prolonged CPU time.
Arrays
In C++, array indices can be zero or positive. InMatlab, array indices
can only be positive. These extremely annoying restrictions can be bypassed
by shifting the indices.
InFortran 77, array indices can have any positive or negative value.
In C++, the lower limit of an array index is 0. Thus, a vectorvwith
20 slots begins atv(0) and ends atv(19). Similarly, the indices of the 10× 5
matrixA[10][5] begin ati, j= 0 and end ati=9,j=4.
InMatlab, the lower limit of an array index is 1.
InFortran 77, the default lower limit of an array index is 1. However,
this can be reset by stating, for example,
double precision A(-30:200)
Dimension B(-4:14,-10:29)
The vectorAbegins atA(-30)and ends atA(200). The first index of the
matrixBbegins at -4 and ends at 14; the second index begins at -10 and ends
at 29.