You are on page 1of 21

Data Types & Data

Structures
CAPE Unit 1
Module 2 - Problem Solving
Objectives
By the end of the lesson students should be
able to:
Differentiate Data types and Data Structures
State examples of Data types and Data Structures
Utilize arrays in the design of problem solutions in
Pseudocode
What need is there for Data
Types or Data Structures?
In the design of a program a programmer must
clearly define the type of data to be collected ,
defined or generated. These data types can either
be elementary data types or data structures.
Elementary Data Types
An elementary data type is one containing a single
value that can be treated as a unit. These elementary
data types can be consisted only of a set of values and a
set of operations that can be performed on these
values.
The most common elementary data types are:
1.Integers
2.Real
3.Character
4.Boolean
Integers
This elementary data type is used to create variables
that will store whole numbers, whether positive,
negative or zero.

For Example
 5
 0
 -76
Real
This elementary data type is used to create variables
that will store values, whether positive or negative
which may include decimal numbers. These are
sometimes referred to as floating point numbers.

For Example
19.2
27
- 15.6
Character
This elementary data type is used to create variables
that will store characters seen on the keyboard
including special characters

For Example
 $
 H
 J
 8
Boolean
This elementary data type is used to represent a
control flag or switch that may hold one of two possible
values, true or false. An alternative to using Boolean
variables is to use an Integer variable and assign
numbers such as 0 for false and 1 for true.
Boolean Variable example
A program reads whether or not a patient has a tootache and tells
him/her to visit the dentist.

START
DECLARE toothAche AS INTEGER
WRITE “Do you have a toothache, enter 1 for true or 0 for false”
READ toothAche
IF (toothache = 1) THEN
WRITE “visit nearest dentist”
ELSE
WRITE “teeth in good health”
END IF
STOP
Boolean Variable example
This is an alternative to the previous slide.

START
DECLARE toothAche AS BOOLEAN
WRITE “Do you have a toothache, enter 1 for true or 0 for false”
READ toothAche
IF (toothache = true) THEN
WRITE “visit nearest dentist”
ELSE
WRITE “teeth in good health”
END IF
STOP
Boolean Variable example
This is an alternative to the previous slide.

START
DECLARE toothAche AS BOOLEAN
WRITE “Do you have a toothache, enter true or false”
READ toothAche
IF (toothache )THEN
WRITE “visit nearest dentist”
ELSE
WRITE “teeth in good health”
END IF
STOP
Data Structures
A data structure is a structure that is made up of other
data items. The data items that it contains are its
components and may be elementary data types or
other data structures. Every type of data structure is
governed by its own set of rules.
Some common Data Structures are:
 Arrays
 Strings
 Records
 Files
Exploration
What is an Array
How is an array declared
What Control structure is extremely import in
Initializing/Reading from and Writing to arrays
Using code snippets show
Array
An array is a homogenous collection of data
elements or variables. Meaning the elements are all of
the same data type and size and stored in a consecutive
fashion in memory i.e. one after the other.
String
A collection of characters that can be fixed or
variable. A string is sometimes referred to as a
Character Array.
More on Arrays
An Array is referenced by a single variable.
Individual elements in the array can be accessed via
its index or subscript
This index is the position of the element in the list,
starting from 0 for the first element
The last element in an Array of size n is located at
index number n-1
Array elements are always stored consecutively, so
the element at index 1 is beside index 0, etc
Declaring an Array
Indicates how
many Integer
variables the
DECLARE testScores (4) AS INTEGER array can hold

Initializing an Array
testScores{0} Initializing all elements to 0
(1st element) testScores (0)4
testScores (1)7 Initializing the elements of an
array individually
testScores (2)-2
(last element) testScores (3)79
Initializing an array with a loop
FOR index0 TO 3 STEP 1
testScores(index)0
END FOR
Writing values to an Array with a
loop
DECLARE testscores (4), index as INTEGER

FOR index0 TO 3 STEP 1


WRITE “Please enter a number”
READ testScores(index)
END FOR
Reading values from an Array
with a loop
FOR index0 TO 3 STEP 1
WRITE “Number ”, index +1, “ is: ”, testScores(index)
END FOR
Practice Questions
Using an Array:
1. Write a Pseudocode algorithm to read and store 10 test
scores, find the average of those test scores and print
it.
2. Write a Pseudocode algorithm to read and store a list of
100 integers and then print out all the negative
elements in the array.
3. Write a Pseudocode algorithm to read and store a list of
70 integers and then print out all the even numbers and
their sum an the all the odd numbers and their sum.

You might also like