Computer Science department. Lecture 1: Introduction to computers & problem solving

CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 1: Introduction to computers & problem solving Lecture Contents 2 Course Info. ...
Author: Willa McKinney
0 downloads 2 Views 2MB Size
CS110D: PROGRAMMING LANGUAGE I Computer Science department

Lecture 1: Introduction to computers & problem solving

Lecture Contents 2

Course Info. Problem Solving Techniques Pseudocode Algorithm Flow charts

Examples Elements of a Computer system. Evolution of programming languages The code Life Cycle dr. Amal Khalifa,Fall14

Course info 3

Books and references Course plan Assessment methods and grading Labs and practical assignments Practical exam

Books and references 4

[1] JavaTM Programming: From Problem Analysis to Program Design (Introduction to Programming) 5th Edition

[1]

https://www.amazon.com/JavaTMProgramming-Problem-AnalysisIntroduction/dp/111153053X

[2] Deitel P.J., Deitel H.M. - Java. How to Program, 9th Edition http://staff.cs.psu.ac.th/iew/cs344481/Java%20How%20to%20Program%209t h%20Edition.pdf

[2]

How People Solve Problems 5

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 People do this quickly and often in a complex way

Dr. Amal Khalifa, 2014

How Computers Solve Problems 6

Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step at a time Complex “Human” Algorithms must be broken down into simple step-by-step instructions BEFORE they can be translated into computer code

Dr. Amal Khalifa, 2014

Algorithms 7

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 car 1. 2. 3. 4.

Move right foot to gas pedal Apply pressure to gas pedal with right foot If speed is too high, apply less pressure. If speed is too low, apply more pressure.

Dr. Amal Khalifa, 2014

Problem Solving 8

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 Good Problem Solving Skills are based on knowledge, experience and logic Good Programmers NEVER make assumptions

Dr. Amal Khalifa, 2014

9

Dr. Amal Khalifa, 2014

Problem Solving Approach 10

In the programming environment, the problemsolving 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. 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. Dr. Amal Khalifa, 2014

11

Problem analysis–coding–execution cycle

Dr. Amal Khalifa, 2014

Expressing the Algorithms 12

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 FLOWCHARTS

Dr. Amal Khalifa, 2014

Pseudo Code 13

“Pseudo” means “pretend” or “false” Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like 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 or language-specific syntax. The programming language is augmented with natural language descriptions of the details, where convenient.

Dr. Amal Khalifa, 2014

Example 15

Write an algorithm to find the area and the perimeter of a rectangle

dr. Amal Khalifa,Fall14

16

Pseudo Code Example design an algorithm to find the perimeter and area of a rectangle

Dr. Amal Khalifa, 2014

17

That’s all for Today!! Text book [1] chapter 1 (pages 13-16)

Dr. Amal Khalifa, 2014

Flowcharts 18

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 Flowcharts aid in breaking down a problem into simple steps

Dr. Amal Khalifa, 2014

Flowchart Symbols 19

Dr. Amal Khalifa, 2014

flowcharting 20

Dr. Amal Khalifa, 2014

Example 21

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

Pseudocode 22

Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM)

Dr. Amal Khalifa, 2014

Algorithm 23

Flowchart

Step 1: Input Lft Step 2: Lcm ← Lft x 30 Step 3: Print Lcm

START

Input Lft

Lcm ← Lft x 30

Print Lcm

STOP

Dr. Amal Khalifa, 2014

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 Print A

Dr. Amal Khalifa, 2014

Example 2 26

Algorithm Step 1: Input r Step 2: A ← Pi x r x r Step 3: Print A

START

Input r

A ← Pi x r x r

Print A

STOP

Dr. Amal Khalifa, 2014

Example 3 27

Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation ax 2 + bx + c = 0

Hint: the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a where, d = sqrt ( b 2 − 4ac )

Dr. Amal Khalifa, 2014

Example 3 START

28

Algorithm: Step 1: Input the coefficients (a, b, c) of the quadratic equation Step 2: d ← sqrt ( b × b − 4 × a × c ) Step 3: x1 ← (–b + d) / (2 x a) Step 4: x2 ← (–b – d) / (2 x a) Step 5: Print x1, x2

Input a, b, c

d ← sqrt(b x b – 4 x a x c) x1 ←(–b + d) / (2 x a) X2 ← (–b – d) / (2 x a) Print x1 ,x2

STOP

Elements of a Computer System 29

Hardware

Computer Software

dr. Amal Khalifa,Fall14

Hardware 30

dr. Amal Khalifa,Fall14

Memory Unit 31

Ordered sequence of cells or locations Stores instructions and data in binary Types of memory Read-Only Memory (ROM) Random Access Memory (RAM)

dr. Amal Khalifa,Fall14

32

Binary Data Bit: A binary digit 0 or 1. A sequence of eight bits is called a byte. dr. Amal Khalifa,Fall14

ASCII Code 33

Every letter, number, or special symbol (such as * or {) on your keyboard is encoded as a sequence of bits, each having a unique representation. The most commonly used American Standard Code for Information Interchange (ASCII).

dr. Amal Khalifa,Fall14

Central Processing Unit (CPU) 34

Executes stored instructions Arithmetic/Logic Unit (ALU) Performs arithmetic and logical operations

Control Unit Controls the other components Guarantees instructions are executed in sequence

dr. Amal Khalifa,Fall14

Input and Output Devices 35

Interaction with humans Gathers data (Input) Displays results (Output)

dr. Amal Khalifa,Fall14

Software 36

System programs loads first when you turn on your PC Also called the operating system. The operating system monitors the overall activity of the computer and provides services, such as memory management, input/output activities, and storage management.

Application programs

perform specific tasks Examples : Word processors spreadsheets, and games

Both operating systems and application programs are written in programming languages. dr. Amal Khalifa,Fall14

Are Computers Intelligent? 37

Do we really need to be involved in programming computers? They have beaten world chess champions. They help predict weather patterns. They can perform arithmetic quickly.

So, a computer has an IQ of _____.

dr. Amal Khalifa,Fall14

What is Computer Programming? 38

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. A program: A sequence of steps to be performed by a computer. Expressed in a computer language.

dr. Amal Khalifa,Fall14

Computer Languages 39

A set of Symbols (punctuation), Special words or keywords (vocabulary), And rules (grammar)

used to construct a program.

dr. Amal Khalifa,Fall14

Evolution of Programming Languages 40

Languages differ in Size (or complexity) Readability Expressivity (or writability) "Level" closeness to instructions for the CPU

dr. Amal Khalifa,Fall14

Machine Language 41

Binary-coded instructions Used directly by the CPU Lowest level language Every program step is ultimately a machine language instruction

Address Contents 2034

10010110

2035

11101010

2036

00010010

2037

10101010

2038

10010110

2039

11101010

2040

11111111

2041

01010101

2042

10101101 dr. Amal Khalifa,Fall14

Assembly Language 42

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 language. Sample Program

Mnemonic Instruction ADD 10010011

MUL ADD STO SUB

X,10 X,Y Z,20 X,Z

dr. Amal Khalifa,Fall14

43

Examples of Instructions in Assembly Language and Machine Language

dr. Amal Khalifa,Fall14

High-Level Languages 44

Closer to natural language Each step maps to several machine language instructions Easier to state and solve problems

Compiler: A program that translates a program written in a high-level language into the equivalent machine language. dr. Amal Khalifa,Fall14

Examples of High-Level Languages 45

Language Pascal

Primary Uses Learning to program

C++ FORTRAN PERL

General purpose Scientific programming Web programming, text processing

Java

Web programming, application programming Business

COBOL

dr. Amal Khalifa,Fall14

The java Language 46

source program. Saved in File ClassName.ja va

dr. Amal Khalifa,Fall14

The Code life Cycle!! 47

Edit

compile

run

dr. Amal Khalifa,Fall14

48

Processing a java program

dr. Amal Khalifa,Fall14

IDE 49

The programs that you write in Java are typically developed using an integrated development environment (IDE).

dr. Amal Khalifa,Fall14

50

That’s all for Today!! Text book [1] chapter 1 (pages 1-13)

dr. Amal Khalifa,Fall14