0% found this document useful (0 votes)
10 views15 pages

Lecture 1 2024

Uploaded by

Lucia Motlhagodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Lecture 1 2024

Uploaded by

Lucia Motlhagodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ISS112

Introduction to programming

Lecture 1: Introduction to algorithms


Introduction

• Before writing a program to solve a problem,


have a thorough understanding of the problem
and a carefully planned approach to solving it.
• Understand the types of building blocks
available & employ proven program-
construction techniques.
Algorithms
• An algorithms is a series of ordered steps that are required to carry out
a certain task or solve a problem.
• An algorithm is a procedure for solving a problem in terms of
• the actions to execute and
• the order in which these actions execute
• We use algorithms everywhere in our daily lives, not only in
computing.
o A recipe is an algorithm preparing a particular dish
o A morning routine is an algorithm for efficiently getting ready for
work
o Google search uses algorithms to sort results by relevance
o Social media sites use algorithms to customise content viewing

• Any computing problem can be solved by executing a series of actions


in a specific order.
Algorithms

• The “rise-and-shine algorithm” is a typical morning routine


followed by one manager to get to work
• (1) Get out of bed; (2) exercise; (3) take a shower; (4) get
dressed; (5) eat breakfast; (6) get in the car to go to work.

• Suppose that the same steps are performed in a slightly different


order:
• (1) Get out of bed; (2) take off pajamas; (3) get dressed; (4) take
a shower; (5) eat breakfast; (6) get in the car to go to work.

• Specifying the order in which statements (actions) execute in a


program is called program control.
Problem Solving: Algorithm
Design

Algorithm: A sequence of steps that is:


• unambiguous
• Executable
• terminating
An Algorithm for Solving an
Investment Problem
The problem:
You put P10,000 into a bank account that earns 5 percent
interest per year. How many years does it take for the
account balance to be double the original?
Calculating by hand
An Algorithm for Solving an
Investment Problem - continued
The steps in the algorithm:
• Start with a year value of 0, A column for the interest, and a balance of
P10,000.

• Repeat the following steps while the balance is less than P20,000 (double the
original)

o Add 1 to the year value.


o Compute the interest as balance x 0.05 (i.e., 5 percent interest). Add the
interest to the balance.
• Report the final year value as the answer.
Self Check 1.21

Suppose the interest rate was 20 percent. How


long would it take for the investment to double?

Answer: 4 years:

0 10,000
1 12,000
2 14,400
3 17,280
4 20,736
From Algorithm to Programs
Pseudocode
• Pseudocode is an informal language that helps you
develop algorithms without having to worry about
the strict details of Java language syntax.
• Particularly useful for developing algorithms that
will be converted to structured portions of Java
programs.
• Similar to everyday English.
• Helps you “think out” a program before attempting
to write it in a programming language, such as
Java.
Pseudocode Cont….
• You can type pseudocode conveniently, using any
text-editor program.
• Carefully prepared pseudocode can easily be
converted to a corresponding Java program.
• Pseudocode normally describes only statements
representing the actions that occur
• After you convert a program from pseudocode to
Java and the program is run on a computer.
• e.g., input, output or calculations.
Self Check 1.22

Suppose your cell phone carrier charges you P29.95 for up to 300
minutes of calls, and P0.45 for each additional minute, plus 12.5
percent taxes and fees. Give an algorithm to compute the monthly
charge for a given number of minutes.
Answer:
Is the number of minutes at most 300?
A. If so, the answer is P29.95 + (12.5/100 *29.95) = P33.70
B. If not,
1.Compute the difference: (number of minutes) – 300.

2.Multiply that difference by 0.45.

3. Add P29.95 to get total

5. Calculate and add taxes to the total. That is the answer.


Simple Vs Complex Algorithms

• Linear algorithms – a simple form of an algorithm that uses a linear,


step-by-step process to achieve a particular outcome.
• Branching algorithms - incorporate branching, can take multiple
different actions, operate on multiple potential sequences
o depending on a specific set of conditions and the information
provided
o it can be helpful to use flowcharts to represent the different
pathways and the conditions which trigger them.
• Branching algorithms link to two other important algorithmic
concepts: conditional statements and loops.
Control Statements

• A conditional statement is a step in the algorithm’s sequence that is


dependent on certain requirements being met, and they are therefore one of
the main drivers of a branching algorithm.
• For example, a streaming website might ask you for your age when you
log on.
• An algorithm could then use a conditional statement to decide whether
you’re old enough to access age-restricted content and change what
content you’re shown as a result.
• Loops are an action or group of actions that are repeated by an algorithm
until a conditional statement returns a different result.
• A practical example is boiling a kettle – the kettle keeps boiling water
until the water is boiling hot, then turns off. The conditional statement
required to end a loop and progress the sequence of an algorithm is
known as the exit condition.
Computing Skills 15

You might also like