DATA TYPES AND OPERATIONS
GET 261
Generally all software is divided into two, namely:
1) DATA
2) PROGRAM
Data: is information processed or stored by a computer. This information may be in the
form of text documents, images, audio clips, software programs, or other types of data.
Computer data may be processed by the computer's Central Processing Unit and is stored
in files and folders on the computer's hard disk.
PROGRAM: These are collections of instructions for manipulating data. Variables are
used to carry out instruction in a program.
Data Type: is a data storage format that can contain a specific type or range of values.
When computer programs store data in variables, each variable must be assigned a
specific data type. A data type in a programming language can also be said to be a set
of data with values having predefined characteristics.
Some programming languages require the programmer to define the data type of a variable
before assigning it a value. Other languages can automatically assign a variable's data type
when the initial data is entered into the variable.
For example:
If the variable "var1" is created with the value "1.25," the variable would be
created as a floating point data type.
If the variable is set to "Hello world!," the variable would be assigned a string data
type.
Most programming languages allow each variable to store a single data type.
Therefore, if the variable's data type has already been set to an integer, assigning
string data to the variable may cause the data to be converted to an integer format
For example: Adding two whole numbers 10 & 20, which can be done simply as follows −
10 + 20
Let's take another problem where we want to add two decimal numbers 10.50 & 20.50, which
will be written as follows −
10.50 + 20.50
The two examples are straightforward. Now let's take another example where we want to
record student information in a file. Here we would like to record the following information:
−
A typical student record in a file will look like this:
Student Name: Sara Ali
Sex: Female
Age: 13 years
Section: f
Student Class: 5th
The first example dealt with whole numbers, the second example added two decimal numbers,
whereas the third example is dealing with a mix of different data. For instance:
Student name "Zara Ali" is a sequence of characters which is also called a string.
•Student class "6th" has been represented by a mix of whole number and a string of two
characters. Such a mix is called alphanumeric.
•Student section has been represented by a single character which is 'J'.
•Student age has been represented by a whole number which is 13.
•Student sex has been represented by a single character which is 'F'.
This way, we realized that in our day-to-day life, we deal with different types of data
such as strings, characters, whole numbers (integers), and decimal numbers (floating point
numbers).
Similarly, when we write a computer program to process different types of data, we
need to specify its type clearly; otherwise the computer does not understand how different
operations can be performed on that given data.
It is good to know that different programming languages use different keywords to
specify different data types. Following are the list of basic data type
Basic Data Types
1) Integer
2) Floating point
3) Real
4) string
5) Complex
6) Logical
7) Character
INTEGER DATA TYPE
Integer is a datum of integral data type, it is a data type which represents
some finite subset of the mathematical data. Integral data types may be of different sizes
and may or may not be allowed to contain negative values.
Integers are commonly represented in a computer as a group of binary digits
(bits). The size of the grouping varies so the set of integer sizes available varies between
different types of computers.
An integer is a whole number (not a fraction) that can be positive, negative, or
zero. Therefore, the numbers 10, 0, -25, and 5,148 are all integers. Integers cannot have
decimal places.
Integers are the most commonly used data type in computer programming. For
example, whenever a number is being incremented, such as within a "for loop" or "while
loop," an integer is used. Integers are also used to determine an item's location within an
array.
When two integers are added, subtracted, or multiplied, the result is also an integer.
NOTE: when one integer is divided into another, the result may be an integer or a
fraction. For example, 6 divided by 3 equals 2, which is an integer, but 6 divided by 4
equals 1.5, which contains a fraction. Decimal numbers may either be rounded or
truncated to produce an integer result.
CHARACTER DATA TYPE
The Character data type is a single two-byte (16-bit) Unicode character. If a variable
always stores exactly one character, declare it as Character.
For example:
To store alphanumeric text, such as letters, numbers, spaces, symbols, and punctuation, use
the Character data type.
FLOATING POINT DATA TYPE
As the name implies, floating point numbers are numbers that contain floating decimal
points. For example, the numbers 5.5, 0.001, and -2,345.6789 are floating point numbers.
Numbers that do not have decimal places are called integers.
Computers recognize real numbers that contain fractions as floating point numbers.
When a calculation includes a floating point number, it is called a "floating point
calculation." Older computers used to have a separate floating point unit (FPU) that
handled these calculations, but now the FPU is typically built into the computer's CPU.
STRING DATA TYPE
A string is a data type used in programming, such as an integer and floating point unit, but is
used to represent text rather than numbers. It is comprised of a set of characters that can also
contain spaces and numbers. For example, the word "hamburger" and the phrase "I ate 3
hamburgers" are both strings. Even "12345" could be considered a string, if specified
correctly. Typically, programmers must enclose strings in quotation marks for the data to
recognized as a string and not a number or variable name.
For example: In the comparison:
if (Option1 == Option2) then ...
Option1 and Option2 may be variables containing integers, strings, or
other data. If the values are the same, the test returns a value of true,
otherwise the result is false. In the comparison:
if ("Option1" == "Option2") then ...
Option1 and Option2 are being treated as strings. Therefore the test is
comparing the words "Option1" and "Option2," which would return
false.
REAL DATA TYPE
A 'Real' data type is numerical data which contains decimal numbers. It would be used when
extra detail is required and a whole number would not provide enough information.
Examples of where a 'real' data type is used can be seen below:
•Weight in Kg 12.25, 19.99, 199.99
•Room measurement in metres 14.5
•Temperature (degrees Celsius) 37.5
NOTE: that a 'real' data type cannot store the measurement symbol itself ($,£, %), nor the
type of quantity being measured e.g. meters, Kilograms, Celsius. If you use this data type
then you need to put the unit of measurement after the field name as it was done above.
COMPLEX DATA TYPE
Complex data type can also be referred to as composite data type: A complex variable
or value is usually represented as a pair of floating point numbers. Languages that
support a complex data type usually provide special syntax for building such values,
and extend the basic arithmetic operations ('+', '−', '×', '÷') to act on them. These
operations are usually translated by the compiler into a sequence of floating-
point machine instructions or into library calls.
LOGICAL DATA TYPE
To store data that has only two values true or false, use the Logical data type when you
want an efficient way to represent Boolean expressions or when comparing two or more
logical statements. Logical data is stored as true (.T.) or false (.F.).
Programming language typically support a set of operators constructs, which behave
generally like functions, but which differ syntactically or semantically from usual functions.
Common simple examples include arithmetic (addition with +, comparison with >;) and
logical operations (such as AND or &&).
More involved examples include assignment (usually = or :=), field access in a record
or object (usually .), and the scope resolution operator (often ::). Languages usually define a
set of built-in operators, and in some cases allow user-defined operators.
A programming language provides various arithmetic operators. The following table lists
down a few of the important arithmetic operators available in C programming language.
Assume variable A holds 10 and variable B holds 20.
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% This gives remainder of an integer B % A will give 0
division
Assume variable A holds 10 and variable B holds 20, then a typical C program for this instruction will look like this:
#include <stdio.h>
main() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf( "Value of c = %d\n", c);
c = a - b;
printf( "Value of c = %d\n", c);
c = a * b;
printf( "Value of c = %d\n", c);
c = b / a;
printf( "Value of c = %d\n", c);
c = b % a;
printf( "Value of c = %d\n", c);
}