Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

PROGRAMMING AND

ALGORITHMS OVERVIEW
1
▪ Operating System
▪ Application Software
▪ Computer Languages
▪ Executing a Program

2
▪ operating system (OS)
▪ software that controls interaction of user
and computer hardware and that manages
allocation of computer resources
▪ booting a computer
▪ loading the operating system from disk
into memory

3
▪ application
▪ software used for a specific task such as word
processing, accounting, database management,
playing a game, or checking the weather forecast
▪ install
▪ make an application available on a computer by
copying it to the computer’s hard drive

4
▪ Computers are machines that
▪ have the ability of performing a limited set of operations tremendously
fast
▪ do not have the ability of reasoning (cannot think and decide)
▪ Humans have to prepare necessary list of instructions

▪ Computer Program
▪ a set of instructions that tells the computer what to do

▪ Programming
▪ the act of giving instructions to the computer

▪ Programming Language
▪ set of rules, symbols and special words used to construct a program

5
6
▪ machine language
▪ the only language that is directly understood by the
computer, and it does not need to be translated. All
instructions use binary notation and are written as a string
of 1s and 0s.
▪ a program instruction in machine language may look
something like this:
10010101100101001111101010011011100101
▪ this is the only language computer hardware understands
but binary notation is very difficult for humans to
understand. This is where assembly languages come in.

7
▪ assembly language
▪ the first step to improve programming structure and make
machine language more readable by humans.
▪ An assembly language consists of a set of symbols and letters.
▪ A translator is required to translate the assembly language to
machine language. This translator program is called the
'assembler.’
▪ It can be called the second generation language since it no
longer uses 1s and 0s to write instructions, but terms like MOVE,
ADD, SUB and END.

8
▪ assembly language
▪ Many of the earliest computer programs were written in assembly
languages.
▪ Most programmers today don't use assembly languages very
often, but they are still used for applications like operating
systems of electronic devices and technical applications, which
use very precise timing or optimization of computer resources.
▪ While easier than machine code, assembly languages are still
pretty difficult to understand. This is why high-level languages
have been developed.

9
▪ high-level language
▪ programming language that uses English and
mathematical symbols, like +, -, % and many others, in its
instructions.
▪ High-level languages are the languages most often used by
programmers to write programs. Examples of high-level
languages are C++, Fortran, Java and Python.

10
▪ compiler
▪ software that translates a high-level language program into
machine language
▪ source file
▪ file containing a program written in a high-level language;
the input for a compiler
▪ syntax
▪ grammar rules of a programming language

▪ object file
▪ file of machine language instructions that is the output of a
compiler

11
▪ linker
▪ software that combines object files and resolves
cross-references to create an executable
machine language program
▪ integrated development environment (IDE)
▪ software package combining a word processor,
compiler, linker, loader, and tools for finding
errors

12
13
▪ input data
▪ the data values that are scanned by a program

▪ program output
▪ the lines displayed by a program

14
15
1. Specify the problem requirements.
2. Analyze the problem.
3. Design the algorithm to solve the
problem.
4. Implement the algorithm
5. Test and verify the completed program.
6. Maintain and update the program.

16
▪Given a distance in meters and
the speed of a car in miles per
hour (mph), calculate how many
minutes it will take for that car to
travel that distance.

17
▪ Problem
▪ Given a distance in meters and the speed of a car in miles per hour (mph),
calculate how many minutes it will take for that car to travel that distance.

▪ Problem Inputs
▪ distance (in meters)
▪ speed (in mph)

▪ Problem Output
▪ time (in minutes)

▪ Formulas
▪ distance in miles = distance in meters / 1609.344
▪ time in hours = distance in miles / speed in mph
▪ time in minutes = time in hours x 60

18
▪ An algorithm is a finite sequence of clear, executable
instructions that, when followed, will produce the
answer to a given problem.
▪ This can be a simple process, such as adding two
numbers together, or a complex function, such as
adding effects to an image.
▪ For example, in order to sharpen a digital photo, the
algorithm would need to process each pixel in the
image and determine which ones to change and how
much to change them in order to make the image look
sharper

19
▪ An algorithm generally takes some input, carries
out a number of effective steps in a finite
▪ Given a list of numbers, you can easily order them
from largest to smallest with the simple instruction
“Sort these numbers.”
▪ A computer, however, needs more detail to sort
numbers. It must be told to search for the smallest
number, how to find the smallest number, how to
compare numbers together, etc.

20
▪ The operation “Sort these numbers” is ambiguous
to a computer because the computer has no basic
operations for sorting.
▪ Basic operations used for writing algorithms are
known as primitive operations or primitives.
▪ When an algorithm is written in computer
primitives, then the algorithm is unambiguous and
the computer can execute it.

21
▪ Each operation in an algorithm must be doable, that is,
the operation must be something that is possible to do.

▪ For computers, many mathematical operations such as


division by zero or finding the square root of a negative
number are also impossible.

▪ These operations are not effectively computable so they


cannot be used in writing algorithms.

22
▪ Unless an algorithm produces some result, we can never be
certain whether our solution is correct.
▪ Have you ever given a command to a computer and discovered
that nothing changed? What was your response?
▪ You probably thought that the computer was malfunctioning
because your command did not produce any type of result.
▪ Without some visible change, you have no way of determining
the effect of your command. The same is true with algorithms.
▪ Only algorithms which produce results can be verified as either
right or wrong

23
▪ Algorithms should be composed of a finite number of operations
and they should complete their execution in a finite amount of
time.

▪ Suppose we wanted to write an algorithm to print all the integers


greater than 1. Our steps might look something like this:
1. Print the number 2.
2. Print the number 3.
3. Print the number 4.

▪ Is it OK ? Any problems ?

24
▪ While our algorithm seems to be pretty clear, we have
two problems.
▪ First, the algorithm must have an infinite number of
steps because there are an infinite number of integers
greater than one.
▪ Second, the algorithm will run forever trying to count
to infinity.
▪ These problems violate our definition that an algorithm
must halt in a finite amount of time.
▪ Every algorithm must reach some operation that tells it
to stop.
25
▪ Most computer programmers spend a large
percentage of their time creating algorithms.
▪ The goal is to create efficient algorithms that do
not waste more computer resources (such as
RAM and CPU time) than necessary.
▪ Poorly written algorithms can cause programs to
run slowly and even crash. Therefore, software
updates are often introduced to improve
performance.

26
Example: The algorithm of the car
problem in narrative description: Given a
distance in meters and the speed of a car in
miles per hour (mph), calculate how many
minutes it will take for that car to travel that
distance.

27
▪ Example: The algorithm of the car problem in narrative description: Given a
distance in meters and the speed of a car in miles per hour (mph), calculate how
many minutes it will take for that car to travel that distance.

▪ Get the data.


▪ Get the distance in meters.
▪ Get the speed of the car in mph.
▪ Perform the computations.
▪ Calculate the time in minutes.
▪ Convert the distance in meters to miles.
▪ distance in miles = distance in meters / 1609.344
▪ Calculate the time in hours.
▪ time in hours = distance in miles / speed in mph
▪ Convert the time in hours to minutes.
▪ time in minutes = time in hours x 60
▪ Display the result(s).
▪ Display the time in minutes.

28
29
30
▪ Stylized, half natural language, half code method

▪ Written in a natural language, but the sentence structure and


indentation look like a programming language.

▪ Do not have not a standard syntax or style. Everyone can


select his/her own syntax and style. The important thing is,
he/she should be consistent, thus use the same words and
symbols for similar operations, and the algorithm should be
clear and easy to be understood by everybody.

31
▪ Assignment name  expression
▪ Conditional selection if condition then action
▪ Repeated execution while condition do activity
▪ Procedure procedure name (generic names)

32
33
▪ 1. Understand the problem.
▪ 2. Get an idea how an algorithmic
procedure might solve the problem.
▪ 3. Formulate the algorithm and represent it
as a program.
▪ 4. Evaluate the program for accuracy and its
potential as a tool for solving other
problems.
34
▪ Person A is charged with the task of determining the
ages of B’s three children.
▪ B tells A that the product of the children’s ages is 36.
▪ A replies that another clue is required.
▪ B tells A the sum of the children’s ages.
▪ A replies that another clue is needed.
▪ B tells A that the oldest child plays the piano.
▪ A tells B the ages of the three children.
▪ How old are the three children?

35
▪ A woman arrives at a hotel. She has no
money, but does have a gold chain
containing 7 links (like the one in the
photo).
▪ She does not know how long she is
going to stay, but hotel manager offers
her to give one link for each night she
remains in the hotel.

36
▪ The hotel owner is happy with this, and
agrees to be helpful by exchanging links
(so if, for example, the woman has given
him three joined links in the past, she can
pay for an extra night by giving him four
joined links and he will give back the
three joined links).
▪ However, the links are tricky to saw and
so the woman wants to make the smallest
number of cuts possible.
▪ What is the smallest number of cuts that
will allow the woman to pay night on
night?

37
38

You might also like