0% found this document useful (0 votes)
68 views37 pages

01 AlgorithmicThinking

This document provides an introduction to programming and computer science concepts. It discusses what computer science is, the need for representation and algorithms to solve problems, examples of different types of algorithms, and how algorithms and computers work well together due to computers' ability to quickly and reliably execute instructions. It also briefly touches on binary systems, computer architecture, software, programming languages, syntax, semantics, translation, errors, and using programming to solve computational problems.
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)
68 views37 pages

01 AlgorithmicThinking

This document provides an introduction to programming and computer science concepts. It discusses what computer science is, the need for representation and algorithms to solve problems, examples of different types of algorithms, and how algorithms and computers work well together due to computers' ability to quickly and reliably execute instructions. It also briefly touches on binary systems, computer architecture, software, programming languages, syntax, semantics, translation, errors, and using programming to solve computational problems.
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/ 37

Programming Intro

What is Computer Science?


• Computer science is fundamentally about
computational problem solving
For instance: How lock-in patients can
communicate?
What we need to solve problems?
1. Representation. Captures all relevant aspects
of a problem.

Representation
What we need to solve problems?
2. Algorithm. Recipe to solve the problem.
Algorithms
An algorithm is a finite number of clearly described, unambiguous “doable” steps
that can be systematically followed to produce a desired result for given input in a
finite amount of time.
Different kind of algorithms

Vincent Formula. A fast algorithm to calculate the Euclid division algorithm


distance between two latitude/longitude points on
an ellipsoid

Diagnostic Algorithms Day of the week


Cool algorithms
Algorithms and Computers
• Computers can execute instructions very
quickly and reliably without error, algorithms
and computers are a perfect match.
Algorithm features
• Input. There are zero or more quantities that are
externally provided.
• Output. At least one output is produced.
• Definiteness. Each instruction is clear and
unambiguous.
• Finiteness. If we trace out the instruction of the
algorithm, them for all cases, the algorithm terminates
after a finite number of steps.
• Effectiveness. Every instruction must be basic enough
to be carried out, in principle, by a person using only
pencil and paper. Each operation must be feasible.
Examples of algorithms
• Write an algorithm to add two numbers
entered by user.

Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Examples of algorithms
• Write an algorithm to find the largest among three
different numbers entered by user.
Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Examples of algorithms
• Write an algorithm to find all roots of a quadratic
equation ax2+bx+c=0.
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D←b2-4ac
Step 4: If D≥0
r1←(-b+√D)/2a
r2←(-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp←b/2a
ip←√(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Examples of algorithms
• Write an algorithm to find all roots of a quadratic
equation ax2+bx+c=0.
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D←b2-4ac
Step 4: If D≥0
r1←(-b+√D)/2a
r2←(-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp←b/2a
ip←√(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Examples of algorithms
• Write an algorithm to find the factorial of a
number entered by user.
Step 1: Start
Step 2: Declare variables n,factorial and i.
Step 3: Initialize variables
factorial←1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
5.1: factorial←factorial*i
5.2: i←i+1
Step 6: Display factorial
Step 7: Stop
Examples of algorithms
• Write an algorithm to find the factorial of a
number entered by user.
Step 1: Start
Step 2: Declare variables n,factorial and i.
Step 3: Initialize variables
factorial←1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
5.1: factorial←factorial*i
5.2: i←i+1
Step 6: Display factorial
Step 7: Stop
Examples of algorithms
• Write an algorithm to find the Fibonacci
series till term≤1000.
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term←0 second_term←1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term≤1000
5.1: temp←second_term
5.2: second_term←second_term+first term
5.3: first_term←temp
5.4: Display second_term
Step 6: Stop
Analysis of algorithms
The theoretical study of computer-program
performance and resource usage.

What’s more important than performance?


• modularity
• correctness
• maintainability
• functionality
• robustness
• user-friendliness
• programmer time
• simplicity
• extensibility
• reliability
Analysis of algorithms
Why study algorithms and performance?

• Algorithms help us to understand scalability.

• Performance often draws the line between what is feasible


and what is impossible.

• Algorithmic mathematics provides a language for talking


about program behavior.

• The lessons of program performance generalize to other


computing resources.

• Speed is fun!
Algorithms is about communication
Flow chart is a pictorial representation; algorithm is done through step by step
direction.
Algorithms and programs

Algorithm is not the


computer code. Algorithm Program is a set of
are just the instructions computer instructions
which gives clear idea to written in a programming
you idea to write the language
computer code.
Computer Hardware
• All information within a digital computer
system is represented using only two digits, 0
and 1, called binary representation.
Binary System

The term bit stands for binary digit. A byte is a group of bits operated on as a single
unit in a computer system, usually consisting of eight bits.
Computer Architecture
Operative system
Computer Software
Computer software is a set of program
instructions, including related data and
documentation, that can be executed by
computer.

The first program


How do we write computer
instructions?
Step 1: Start
Step 2: Declare variables
first_term,second_term and temp.
Step 3: Initialize variables first_term←0
second_term←1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until ?
second_term≤1000
5.1: temp←second_term
5.2: second_term←second_term+first
term
5.3: first_term←temp
5.4: Display second_term
Step 6: Stop
Programming language
Languages or algorithms
Syntaxis, Semantic and Translation
• The syntax of a language is a set of characters
and the acceptable sequences of those
characters.
Syntaxis, Semantic and Translation
• The semantics of a language is the meaning
associated with each syntactically correct
sequence of characters.
Program translation

A compiler is a translator program that translates programs directly into machine code
to be executed by the CPU. An interpreter executes program instructions in place of
(“running on top of”) the CPU.
Errors
• Syntax error: violation of the programming
language rules

• Semantic errors: errors in the meaning


Computational Solving Problem
An example
Let’s start to program!!
• Create a .py file with your program
• Excecute in python…
Limits of computational solving
problem

Traveling salesman problem: Find the shortest route of travel for a salesman needing
to visit a given set of cities
Any algorithm that correctly solves a given problem must solve the problem
in a reasonable amount of time, otherwise it is of limited practical use.

You might also like