CHAPTER 1
INTRODUCTION:
Life involves solving various problems, both simple and complex.
In programming, problems often relate to mathematical challenges that require careful planning to solve.
Programmers must develop clear steps to instruct a computer to solve a problem. The computer only follows
these instructions without thinking for itself.
OUTCOMES OF THE CHAPTER:
Understand data structures and how they are organized hierarchically.
Know what variables are, their types, and how they are used in expressions.
Understand the distinction between variables and constants.
Learn basic arithmetic operations, including how to set up expressions and equations.
DATA HIERARCHY:
Data refers to facts like numbers, words, or measurements. It is organized in a hierarchy from small units (characters)
to larger ones (data warehouses).
Character: The smallest unit of data, like a letter or number.
Field: A collection of characters (e.g., a name or a number).
Record: A collection of fields (e.g., a student’s record).
File: A collection of related records (e.g., employee files).
Table: A structure with rows and columns where each row represents data for a specific entity.
Database: A collection of related files or tables.
Data Warehouse: The largest data structure, used for processing and analysis.
VARIABLES:
A variable is a location in memory where a value is stored, which can change during program execution.
Programmers use descriptive names for variables, and certain naming rules must be followed (e.g., starting
with a letter, no special characters, etc.).
TYPES OF VARIABLES
Variables can be classified into two main types:
NUMERIC VARIABLES:
Integers: Whole numbers, either positive or negative (e.g., 15, 2).
Real Numbers: Numbers with decimal points (e.g., 12.47, 987.123).
NONNUMERIC VARIABLES:
Character: A single letter, number, or symbol enclosed in quotes (e.g., "A", "8", "").
String: A sequence of characters (e.g., "John Doe").
Boolean: Represents true or false values, often used in logical tests (e.g., Is the user a member? Yes/No).
THE VALUE OF A VARIABLE:
A variable may start without an initial value, but values are assigned during program execution.
The previous value of a variable is replaced when a new value is assigned, just like updating a scoreboard in a
football game.
CONSTANTS:
A constant is similar to a variable, but its value doesn’t change during program execution.
Examples include fixed values like 24 hours in a day or the value of pi (3.14159).
USING VARIABLES IN EXPRESSIONS AND ASSIGNMENTS:
The assignment operator (`=`) is used to assign values to variables.
, Syntax: `VariableName = Value` or `VariableName = Expression`.
Example: `totalStudents = numMales + numFemales`.
ARITHMETIC EXPRESSIONS:
Computer programs often involve arithmetic, such as calculating averages or amounts.
Key words in problem statements indicate arithmetic operations, such as "add," "subtract," "multiply," and
"divide."
Example: The total amount can be expressed as "sum of prices," indicating an addition operation.
ARITHMETIC OPERATORS:
Operators tell the computer what operation to perform on operands (numbers).
Examples of operators:
`+` for addition
`` for subtraction
`` for multiplication
`/` for division
`mod` for finding the remainder after division
The order of precedence determines which operations are performed first in an expression (e.g.,
exponentiation before multiplication).
SETTING UP ARITHMETIC EQUATIONS
An arithmetic equation is also called an assignment statement, where values are assigned to a variable on the
left side of the equation.
For example: `X = A^3 2B + (2(C + D)) / 2` shows how mathematical equations are written for computers.
Computers don’t understand equations as humans do, so they must be written in a computer readable
format, ensuring that variables and constants are correctly placed.
EXAMPLES USING VARIABLES AND CONSTANTS
Several examples are provided to illustrate the application of arithmetic equations in programming:
EXAMPLE 1: CALCULATING MONEY EARNED IN A WORKWEEK.
Kevin works 8 hours a day at R11 per hour and spends R12 on transport daily. His total income after 5 days would
be calculated using:
`money = (hours * payrate - transport) * noDays`.
EXAMPLE 2: PACKING ITEMS INTO PACKETS.
Thandi has 257 items and packs them into groups of 14. To find how many full packets and leftover items she has,
use:
`fullPackets = items \ noInPacket` and `numLeft = items mod noInPacket`.
EXAMPLE 3: CALCULATING JOSEPH'S NET INCOME.
Joseph works 8 hours a day at R5.75 per hour, but spends R3.60 on bus fare daily. His net income per day is
calculated by:
`netIncome = rate * noHours - busFare`.
EXAMPLE 4: CALCULATING THE TOTAL COST AFTER APPLYING A DISCOUNT.
Bonita buys items at R15.85 each and gets a 7.5% discount. The final amount she has to pay can be calculated by:
`amountDue = noOfItems * priceOfItem * 0.925`.
CHAPTER 2
PROBLEM SOLVING:
Before attempting to solve a problem, it’s crucial to read the problem statement carefully to fully understand it.
, The steps to solving any problem include:
1. Reading the problem carefully.
2. Understanding what it entails.
3. Writing down the steps to solve it (i.e., creating an algorithm).
Example: To describe how to get to class from waking up in the morning, you might break down the steps like:
Wake up → Get out of bed → Wash → Get dressed → Eat → Grab your bag → Go to class.
UNDERSTANDING THE PROBLEM:
To solve a problem, eliminate unnecessary information from the problem statement and focus only on the relevant
parts.
Example:
"The Fresh Greengrocer sells apples at 85 cents each, pears at 65 cents, and oranges at 90 cents each. Tumi
has R15 and wants to buy 10 apples and 5 pears."
Relevant information: The prices of apples and pears, the number of apples and pears Tumi wants. You don’t
need the price of oranges or how much money Tumi has for calculating the total.
DATA PROCESSING:
An algorithm follows three key phases:
1. Input: The data you have.
2. Processing: The steps you take to solve the problem.
3. Output: The result of processing the data.
Example: If a student works parttime and needs to calculate their pay:
Input: Number of hours worked, pay rate.
Processing: Multiply the hours by the rate to get the pay.
Output: Display the total pay.
THE PROBLEM SOLVING APPROACH:
To effectively solve a problem, follow these structured steps:
ANALYZE THE PROBLEM:
Carefully read the problem multiple times to ensure understanding.
If necessary, ask for additional information to fill in any missing data before developing a solution.
IDENTIFY ALTERNATIVE SOLUTIONS:
There are often multiple ways to solve a problem, and you should explore different options.
Example: To travel from Johannesburg to Cape Town, you can drive, fly, or take a train. Similarly, different
algorithms may solve the same problem.
SELECT THE MOST EFFECTIVE SOLUTION:
Choose the best solution by considering factors like cost and efficiency.
LIST ALL STEPS (ALGORITHM):
Present the solution steps in an unambiguous way (pseudocode). These steps should be applicable in any
programming language.
EVALUATE THE ALGORITHM FOR ACCURACY:
Test your algorithm (called desk checking) by walking through it with different data sets to ensure it
produces the correct results.
PSEUDOCODE:
Pseudocode is a method of writing down an algorithm in a simple, structured form using plain English.
It’s not a programming language, but it helps plan the logic for programming.