Cs 240 computer programming 1
This presentation is the property of its rightful owner.
Sponsored Links
1 / 39

CS 240 Computer Programming 1 PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on
  • Presentation posted in: General

CS 240 Computer Programming 1. Flowcharts. Algorithm. An informal definition of an algorithm is:. a step-by-step method for solving a problem or doing a task. Algorithm. A step-by-step problem-solving procedure.

Download Presentation

CS 240 Computer Programming 1

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 240 computer programming 1

CS 240Computer Programming 1

Flowcharts


Algorithm

Algorithm

An informal definition of an algorithm is:

a step-by-step method for solving a problem or doing a task.


Algorithm1

Algorithm

A step-by-step problem-solving procedure

An algorithm is a sequence of unambiguous instructions for solving a problem.

The number of steps of an algorithm will be countable and finite.

It is a sequence of instructions (or set of instructions) to make a program more readable; a process used to answer a question.


How to understand the problem

How to understand the problem?

Define the problem

Analyze the problem

Develop an algorithm/method of solution

Write a computer program corresponding to the algorithm

Test and debug the program

Document the program (how it works and how to use it)


Tools

Tools

There are two commonly used tools to help to document program logic (the algorithm)

Flowcharts

Pseudo code


Flowchart

Flowchart

The production flowchart is a visual representation of the sequence of the program. It shows what comes first, second, third, etc

Definition

A flowchart indicates:

The steps to be taken in order to solve a problem.

The order or the sequence of these steps.


Flowchart rules

Flowchart Rules

1. Use only one start and one stop per flowchart, --that is, one way in and one way out of the flowchart.

2. The logic flow of the solution is displayed from top to bottom and from left to right.

3. Use the appropriate symbol for each type of operation.

4. Use arrows when moving to another part of the flowchart rather than lines.

5. Do not leave dead-ends--that is, a part of a question unanswered.


Symbols

Symbols


1 simple sequential flowchart

1.Simple sequential Flowchart

Example 1

Construct a flow chart that prints "Hello, World"?


1 simple sequential flowchart1

1.Simple sequential Flowchart

Algorithm

Step 1- Start

Step 2- Print "Hello, World"

Step 3- Stop


1 simple sequential flowchart2

1.Simple sequential Flowchart

Flowchart

Start

Print

“Hello, World”

Stop


1 simple sequential flowchart3

1.Simple sequential Flowchart

Example 2

Construct a flow chart that finds the sum of two numbers.


1 simple sequential flowchart4

1.Simple sequential Flowchart

Algorithm

Variables

Step 1- Start

A: First Number

Step 2- Read A

B: Second Number

Step 3- Read B

C: Sum (A+B)

Step 4- Calculate C = A+B

Step 5- Print C

Step 6- Stop


1 simple sequential flowchart5

1.Simple sequential Flowchart

Start

Flowchart

Read A

Read B

C= A+B

Print C

Stop


1 simple sequential flowchart6

1.Simple sequential Flowchart

Example 3

Construct a flow chart that finds the sum, average and product of three numbers.


1 simple sequential flowchart7

1.Simple sequential Flowchart

Algorithm

Variables

Step 1- Start

X: First Number

Step 2- Read X, Y, Z

Y: Second Number

Step 3- CalculateS = X+Y+Z

Z: Third Number

Step 4- Calculate A = S/3

S: Sum (X+Y+Z)

Step 5- Calculate P = X*Y*Z

A: Average (S/3)

Step 6- Print S, A, P

P: Product (X*Y*Z)

Step 7- Stop


1 simple sequential flowchart8

1.Simple sequential Flowchart

Start

Flowchart

Read X,Y,Z

S= X+Y+Z

A=S/3

P=X*Y*Z

Print S,A,P

Stop


1 simple sequential flowchart9

1.Simple sequential Flowchart

Example 4

Construct a flow chart that finds the difference and the division of two numbers and display the result


1 simple sequential flowchart10

1.Simple sequential Flowchart

Algorithm

Variables

Step 1- Start

N1 : First Number

Step 2- Read N1, N2

N2 : Second Number

Step 3- CalculateD = N1-N2

D: Difference

Step 4- Calculate V = N1/N2

V: Division

Step 5- Print D,V

Step 6- Stop


1 simple sequential flowchart11

1.Simple sequential Flowchart

Start

Flowchart

Read N1, N2

D= N1 –N2

V=N1/N2

Print D,V

Stop


1 simple sequential flowchart12

1.Simple sequential Flowchart

Example 5

Exercise

Construct a flow chart that finds the circle area and circumference of a circle where R (radius) is given


1 simple sequential flowchart13

1.Simple sequential Flowchart

Algorithm

Variables

Step 1- Start

R : Radius

Step 2- Read R

PI: PI = 3.14

Step 3- CalculateA = PI*(R)2

A: Area

Step 4- Calculate C = 2*PI*R

C: Circumference

Step 5- Print R, A, C

Step 6- Stop


2 branched flowcharts

2. Branched Flowcharts

Example 1

Construct a flow chart for the following function

F(x) = {

XX>=0

-X X<0


2 branched flowcharts1

2. Branched Flowcharts

Algorithm

Variables

Step 1- Start

X : Number

Step 2- Read X

F: function of X

Step 3- if X >=0 then F =X

Step 4- if X <0 then F =-X

Step 5- Print F

Step 6- Stop


2 branched flowcharts2

2. Branched Flowcharts

Start

Flowchart

Read X

YES

NO

X>=0

F=-X

F=X

Print F

Stop


2 branched flowcharts3

2. Branched Flowcharts

Example 2

Trace the following flowchart and write the output of it.

1. When X = 20

2. When X = -10


2 branched flowcharts4

2. Branched Flowcharts

Start

Flowchart

>0

0>

Read X

=0

W=SIN(X)+5

X?

W=2*X-1

W=X+1

Print X,W

Stop


2 branched flowcharts5

2. Branched Flowcharts

Result

When X=-10

When X=20

X= -10

W= -21

X= 20

W= 21


2 branched flowcharts6

2. Branched Flowcharts

Example 3

Exercise

Draw a flowchart that shows the traffic light processing


2 branched flowcharts7

2. Branched Flowcharts

Algorithm

Variables

Step 1- Start

C : Traffic light color

Step 2- Read C

Step 3- make a Decision (what is c)

Step 4- if C is RED then Print STOP

Step 5- if C is YELLOW then Print WAIT

Step 6- if C is GREEN then Print PASS

Step 7- Stop


3 loop flowcharts

3. Loop Flowcharts

Example 1

Trace the following flowchart and write the output of it.


3 loop flowcharts1

3. Loop Flowcharts

Start

Flowchart

N=1

Print N

F

T

While N<=7

Stop

N=N+3


3 loop flowcharts2

3. Loop Flowcharts

Result


3 loop flowcharts3

3. Loop Flowcharts

Example 2

Trace the following flowchart and write the output of it.


3 loop flowcharts4

3. Loop Flowcharts

Start

Flowchart

i=0

S=0

F

T

While i<10

Read X

A=S/10

Print A

S= X + S

Increment i

Stop


3 loop flowcharts5

3. Loop Flowcharts

Start

Flowchart

i=0

Sum=0

F

T

While i<10

Read X

avg=Sum/10

Print avg

Sum= X + Sum

Increment i

Stop


3 loop flowcharts6

3. Loop Flowcharts

Result

Avg=50/10 =5


Problem write algorithm and flowchart to find solution of quadratic equation

Problem: Write Algorithm and Flowchart to find solution of Quadratic equation


Problem write algorithm and flowchart to find solution of quadratic equation1

START

Read

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)

STOP

Problem: Write Algorithm and Flowchart to find solution of Quadratic equation

  • Algorithm:

  • Step 1: Start

  • Step 2: Read a, b, c

  • Step 3: dsqrt ( )

  • Step 4: x1  (–b + d) / (2 x a)

  • Step 5: x2  (–b – d) / (2 x a)

  • Step 6: Print x1, x2

  • Step 7: Stop

Print X1, X2


  • Login