Data Types and Structures

Data Types and Structures

Data Types and Structures

The variables and constants used by a program can be of various different types. Data types may differ:

1 In the way they are stored, for example, decimal fractions are usually stored as floating point numbers but integers are not

2 In the operations which can be done on them, for example numbers can be multiplied but strings cannot.

REALS, INTEGERS AND STRINGS

The common data types are:

1 REAL numbers-all ordinary numbers including decimals.

2 INTEGERS-all whole numbers including 0 and the negative numbers. 3 STRINGS-groups of charactes including letters, digits and others.

Uses and advantages

1 (a) Integers are used whenever the variable involved is always a whole number. This is because:

(i) Integers can be stored exactly, whereas real numbers are stored approximately

(ii) Operations on integers can be done more quickly.

(b) In particular integers are useful when counting and as the variable in FOR loops.

2 Real numbers are used:

(a) Where there is a large range of numbers- numbers are either very big or very small.

(b) Where decimals are involved-for instance, where division is done.

3 Strings are used:

(a) For data which is not numeric or is not going to be used in calculations.

(b) For inputting numeric data so that it can be checked that it is in fact a number.

Examples

1 The following is a line of a BASIC program:

200 IF Name$="***" THEN Average=TotalMark / N Here

Name$ is a string variable. We can tell this because it ends in $;is a string constant;

Average is probably REAL and TotalMark and N are probably INTEGERS.

In BASIC many versions do not allow you to declare which variables are REAL and which are INTEGER.

2 The following is a statement from a Pascal program:

IF Name= Boque_value THEN Average: =TotalMark/N;

In Pascal all variables and constants have to be ‘declared’ before they are used. This seems a lot of trouble but at least anyone reading the program can understand it clearly. Thus in the program in this example the following statements appear near the beginning:

CONST

Hoque_Value = ‘***’;

VAR

Name : STRING[10];

Average: REAL;

Total_Mark: INTEGER;

N :INTEGER;

DATA STRUCTURES INCLUDING ARRAYS

A data structure is a set of data items organized together. The data items making up the structure are its elements. The structure is defined by a set of properties linking the elements.

Examples of data structures

1 A file. In a file the items are grouped into records. The records are then organized into a file.

2 A string. This can be thought of as a data structure composed of separate characters.

An array is a data structure in which the elements all have the same name. Each element is distinguished from the others by means of one or more numbers.

These numbers are called subscripts. The subscripts provide the programmer with a means of referring to individual elements.

The number of dimensions an array has is the number of subscripts attached to each element, for example, if each element has two subscripts, then the array has two dimensions.

Declaring an array

Usually the programmer has to declare the size of an array before using it. This is done by giving the range of values which can be taken by each subscript. In some languages this is done in a DIMENSION statement. In BASIC and COMAL a DIM statement is used.

Example of an array in BASIC

100 REM *** Fill the array num with the odd numbers from 1 to 19 *** 110 DIM num(10)

120 FOR subs=1 TO 10 130 num(subs) = 2*subs- 1 140 NEXT subs

Line 110 declares a one-dimensional array, num, with 10 elements. In line 130 subs is a subscript. The FOR … NEXT loop gives subs the values 1,2,3 … up to 10  for FOR … NEXT). The expression 2*subs -1 gives 1 when subs is 1,3 when subs is 2, 5 when subs is 3, etc. Thus the program sets numtl) = 1, num(2) = 3, num(3) = 5, etc.

Advantages of using arrays

1 The same operation can be done on each of a group of variables using only one instruction. The instruction is repeated in a loop using the same name but varying the subscript. (See line 130 in the example above.)

2 In some languages the whole array can be treated as a unit, as if it were one variable.

3 Arrays can be used to store tables, lists, etc.

Leave a comment

Your email address will not be published. Required fields are marked *