Upload
dinhthuy
View
245
Download
5
Embed Size (px)
Citation preview
CS110D: PROGRAMMING CS110D: PROGRAMMING LANGUAGE ILANGUAGE I
Lecture 1: Introduction to computers & problem solving
Computer Science department
Lecture Contents
� Course Info.
� Problem Solving Techniques� Pseudocode
� Algorithm
2
dr. Amal Khalifa,Fall14
� Flow charts
� Examples
� Elements of a Computer system.
� Evolution of programming languages
� The code Life Cycle
Course info
� Books and references
� Course plan
� Assessment methods and grading
� Labs and practical assignments
3
� Labs and practical assignments
� Practical exam
Books and references
[1] JavaTM Programming: From Problem Analysis to Program Design (Introduction to Programming) 5th Edition
� https://www.amazon.com/JavaTM-Programming-Problem-Analysis-
4
[1]
Programming-Problem-Analysis-Introduction/dp/111153053X
[2] Deitel P.J., Deitel H.M. - Java. How to Program, 9th Edition
� http://staff.cs.psu.ac.th/iew/cs344-481/Java%20How%20to%20Program%209th%20Edition.pdf
[2]
How People Solve Problems5
� A Problem exists when what we have (Data) is not the same as what we want (information)
� People create a solution (called an Algorithm) which manipulates Data into Information
Dr. Amal Khalifa, 2014
manipulates Data into Information
� People do this quickly and often in a complex way
How Computers Solve Problems6
� Computers also use Algorithms to solve problems, and change data into information
� Computers can only perform one simple step at a time
Dr. Amal Khalifa, 2014
time
� Complex “Human” Algorithms must be broken down into simple step-by-step instructions BEFORE they can be translated into computer code
Algorithms7
� Algorithm: A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time
� Example: Accelerating in a car1. Move right foot to gas pedal
2. Apply pressure to gas pedal with right foot
Dr. Amal Khalifa, 2014
2. Apply pressure to gas pedal with right foot
3. If speed is too high, apply less pressure.
4. If speed is too low, apply more pressure.
Problem Solving8
� Problem Solving is the ability to understand what you have, what you want, and creating a set of instructions to change what you have into what you want
Dr. Amal Khalifa, 2014
� Good Problem Solving Skills are based on knowledge, experience and logic
� Good Programmers NEVER make assumptions
Problem Solving Approach10
� In the programming environment, the problem-solving process involves the following steps:1. Analyze the problem and outline the problem and its solution requirements.
2. Design an algorithm to solve the problem.
Dr. Amal Khalifa, 2014
2. Design an algorithm to solve the problem.
3. Implement the algorithm in a programming language, such as Java.
4. Verify that the algorithm works.
5. Maintain the program by using and improving it, and modifying it if the problem domain changes.
Expressing the Algorithms12
� A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily
� There are standards in programming:PSEUDOCODE
Dr. Amal Khalifa, 2014
� PSEUDOCODE
� FLOWCHARTS
Pseudo Code
� “Pseudo” means “pretend” or “false”
� Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code
13
computer code
� Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does
Dr. Amal Khalifa, 2014
Pseudocode (wikipedia)14
� Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations
Dr. Amal Khalifa, 2014
but omits detailed subroutines, variable declarations
or language-specific syntax. The programming language is augmented with natural language
descriptions of the details, where convenient.
Example15
� Write an algorithm to find the area and the perimeter of a rectangle
dr. Amal Khalifa,Fall14
design an algorithm to find the perimeter and area of a rectangle
Pseudo Code Example16
Dr. Amal Khalifa, 2014
Text book
[1] chapter 1 (pages 13-16)
That’s all for Today!!17
[1] chapter 1 (pages 13-16)
Dr. Amal Khalifa, 2014
Flowcharts18
� Graphical representations of algorithms
� Tool to translate algorithms into software
� A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data
Dr. Amal Khalifa, 2014
represent actions on data and the flow of data
� Flowcharts aid in breaking down a problem into simple steps
Example21
� Example 1: Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
hint: I feet = 30 cm
Dr. Amal Khalifa, 2014
hint: I feet = 30 cm
Pseudocode22
� Input the length in feet (Lft)
� Calculate the length in cm (Lcm) by multiplying LFT with
30
Dr. Amal Khalifa, 2014
� Print length in cm (LCM)
Algorithm23
� Step 1: Input Lft
� Step 2: Lcm ← Lft x 30
� Step 3: Print Lcm
START
Input
Lft
Flowchart
Dr. Amal Khalifa, 2014
� Step 3: Print Lcm
Lcm ← Lft x 30
Lcm
STOP
Example 2 24
Write an algorithm and draw a flowchart that will read the radius of a circle and calculate its area.
Dr. Amal Khalifa, 2014
Example 2 25
Pseudocode
� Input the radius (r) of a circle
� Calculate the area (A) :
A ← Pi x r x r
Dr. Amal Khalifa, 2014
A ← Pi x r x r
� Print A
Example 226
Algorithm
� Step 1: Input r
� Step 2: A ← Pi x r x r
� Step 3: Print A
START
Input
r
Dr. Amal Khalifa, 2014
� Step 3: Print AA ← Pi x r x r
A
STOP
� Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation
� Hint: the roots are:
Example 327
20ax bx c+ + =
� Hint: the roots are:
x1 = (–b + d)/2a and
x2 = (–b – d)/2a
where, d = sqrt ( )
Dr. Amal Khalifa, 2014
24b ac−
� Algorithm:
Step 1: Input the coefficients (a, b, c) of the
quadratic equation
Example 328
START
Input
a, b, c
d ← sqrt(b x b – 4 x a x c)
quadratic equation
Step 2: d ← sqrt ( )
Step 3: x1 ← (–b + d) / (2 x a)
Step 4: x2 ← (–b – d) / (2 x a)
Step 5: Print x1, x2
4b b a c× − × ×
x1 ,x2
STOP
x1 ←(–b + d) / (2 x a)
X2 ← (–b – d) / (2 x a)
Memory Unit31
� Ordered sequence of cells or locations
� Stores instructions and data in binary
� Types of memory
� Read-Only Memory (ROM)� Read-Only Memory (ROM)
� Random Access Memory (RAM)
dr. Amal Khalifa,Fall14
Bit: A binary digit 0 or 1.
A sequence of eight bits is called a byte.
Binary Data32
dr. Amal Khalifa,Fall14
ASCII Code33
Every letter, number, or special symbol (such as * or {) on your keyboard is encoded as a sequence of bits, sequence of bits, each having a unique representation.
The most commonly used American Standard Code for Information Interchange (ASCII).
dr. Amal Khalifa,Fall14
34
Central Processing Unit (CPU)
� Executes stored instructions
� Arithmetic/Logic Unit (ALU)� Performs arithmetic and logical
operations
Control Unit
dr. Amal Khalifa,Fall14
� Control Unit� Controls the other components
� Guarantees instructions are executed in sequence
Input and Output Devices35
� Interaction with humans
� Gathers data (Input)
� Displays results (Output)
dr. Amal Khalifa,Fall14
Software
� loads first when you turn on your PC
� Also called the operating system.
The operating system monitors
� perform specific tasks
� Examples :
36
System programs Application programs
� The operating system monitors the overall activity of the computer and provides services, such as memory management, input/output activities, and storage management.
Examples :
� Word processors
� spreadsheets, and
� games
dr. Amal Khalifa,Fall14
Both operating systems and application programs are
written in programming languages.
37
Are Computers Intelligent?
� Do we really need to be involved in programming computers?
� They have beaten world chess champions.
� They help predict weather patterns.� They help predict weather patterns.
� They can perform arithmetic quickly.
� So, a computer has an IQ of _____.
dr. Amal Khalifa,Fall14
38
What is Computer Programming?
� Planning or scheduling a sequence of steps for a
computer to follow to perform a task.
� Basically, telling a computer what to do and how to do it.
dr. Amal Khalifa,Fall14
do it.
� A program:
� A sequence of steps to be performed by a computer.
� Expressed in a computer language.
39
Computer Languages
� A set of
� Symbols (punctuation),
� Special words or keywords (vocabulary),
� And rules (grammar)
dr. Amal Khalifa,Fall14
� And rules (grammar)
used to construct a program.
Evolution of Programming Languages40
� Languages differ in
� Size (or complexity)
� Readability
� Expressivity (or writability)
dr. Amal Khalifa,Fall14
� Expressivity (or writability)
� "Level"
� closeness to instructions for the CPU
Machine Language41
� Binary-coded instructions
� Used directly by the CPU
� Lowest level language
� Every program step is ultimately
10010110
11101010
00010010
2034
2035
2036
Address Contents
a machine language instruction00010010
10101010
10010110
11101010
11111111
01010101
10101101
2036
2037
2038
2039
2040
2041
2042
dr. Amal Khalifa,Fall14
Assembly Language42
� Each CPU instruction is labeled with a mnemonic.
� Very-low level language
� Almost 1 to 1 correspondence with machine language
� Assembler: A program that translates a program written in assembly language into an equivalent program in machine
dr. Amal Khalifa,Fall14
assembly language into an equivalent program in machine language.
Mnemonic Instruction
ADD 10010011
MUL X,10ADD X,YSTO Z,20SUB X,Z
Sample Program
44
High-Level Languages
� Closer to natural language
� Each step maps to several machine language instructions
� Easier to state and solve
dr. Amal Khalifa,Fall14
� Easier to state and solve problems
� Compiler: A program that translates a program written in a high-level language into the equivalent machine language.
45
Examples of High-Level Languages
Language Primary Uses
Pascal Learning to program
C++ General purpose
FORTRAN Scientific programming
dr. Amal Khalifa,Fall14
FORTRAN Scientific programming
PERL Web programming, text processing
Java Web programming, application
programming
COBOL Business
IDE49
� The programs that you write in Java are typically developed using an integrated development environment (IDE).
dr. Amal Khalifa,Fall14