27
Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Place photo here

Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Embed Size (px)

Citation preview

Page 1: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Chapter 2: General Problem Solving Concepts

Computer Programming

Skills-2

4800153-31435/1436

Department of Computer ScienceFoundation Year ProgramUmm Alqura University, Makkah

Place photo here

Page 2: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

The Objectives and the outcomes

The Objectives:1. To be able to understand the problem.2. To be able to analyze the problem. 3. To be able to produce an algorithm that solves

the problem. The Outcomes:4. Students should be able to understand the

problem. 5. Student should be able to analyze the problem.6. Students should learn how to solve the problem.

Page 3: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Introduction to Programming and Programming language

Programming language is an artificial language that specifies instruction to be executed on a computer.

There are two types of programming languages :• Low level languages• High level languages

Page 4: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Introduction

• Computer Languages:

There are many types of computer languages, which can be categorized into the following four types:

1. Low-Level Languages (1st & 2nd Generation Languages)

2. High-Level Languages (3rd Generation Languages)

3. User-Friendly Languages (4th Generation Languages)

4. Object-Oriented Languages (5th Generation Languages)

Page 5: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

introduction

Main Operations of High Level Language Program

1. Input operations: Like input, read

2. Output operations: Like write, print

3. Arithmetic operations: Like adding, subtracting, multiplying etc....

4. Control transferring operations: Like “GO TO”, conditional, non-conditional, etc....

5. Looping: Like repeat, do while, for, etc...

Page 6: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

introduction

Main Steps in Developing a Program:

1. Analyze the problem

2. Identify the variables involved

3. Design the solution

4. Write the program

5. Enter the program into a computer

6. Compile the program and correct errors

7. Correct the logical errors if any

8. Test and debug program with data

9. Document the program

Page 7: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

An Algorithm:

A sequence of precise instructions, which leads to a solution, is called an algorithm.

Examples:1. Steps (algorithm) to solve a first degree equation.2. Steps (algorithm) to solve a second degree equation using

delta.

In other words, algorithm is a method of representing the step-by-step logical procedure for solving a problem.

Page 8: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

According to D.E. Knuth, an algorithm must possess the following properties:

1. Finiteness: An algorithm must terminated.2. Definiteness: Each step of the algorithm must be precisely

and unambiguously stated.3. Effectiveness: Each step must be effective, 4. Generality: The algorithm must be complete in itself 5. Input/output: Each algorithm must take zero, one or more

quantities as input data produce one or more output values.

Page 9: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example:

Find the algorithm for solving a first degree equation, in the form Ax=B.

Sol.1. Read the values A, B2. Check if A=0 (if yes there is no equation) therefore no

equation to be solved (end processing).3. Check if B=0 (if yes X=0 and end processing).4. Calculate the value of X= B/A5. Print the value of: X

Page 10: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Algorithm Representation

There are many techniques in which we may represent an algorithm when thinking of writing a computer program. The techniques that are to be studied are:

1. Flowchart: The flowchart represents a graphical representation of the steps required for an algorithm or program.

2. Pseudocode: It is an informal high level description of the operating principle of a computer program or other algorithm.

Page 11: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Pseudocode:

It is an informal high level description of the operating principle of a computer program or other algorithm.

Example 1:

1. Set total to zero

2. Set grade counter to one

3. While grade counter is less than or equal to ten1. Input the next grade

2. Add the grade into the total

4. Set the class average to the total divided by ten

5. Print the class average.

Page 12: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example 2:

1. Initialize passes to zero

2. Initialize failures to zero

3. Initialize student to one

4. While student counter is less than or equal to tenA. Input the next exam result

B. If the student passed① Add one to passes

C. Else① Add one to failures

5. Add one to student counter

6. Print the number of passes

7. Print the number of failures

8. If eight or more students passed

9. Print "raise tuition"

Page 13: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:

The flowchart represents a graphical representation of the steps required for an algorithm or program. The flowchart is characterized by:

1) Clarifying the program logic

2) Identifying alternative processing methods

3) Serving as guide for program coding

4) Serving as documentation

There are some principals that should be followed up when putting the Flowchart, such as:

a) Simplicity

b) Organization

c) Planning

Page 14: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:

• General Concepts:• Flow chart must be characterized by:

1. The major element of the project.

2. Elements are clearly labeled.

3. Sequence of elements must be clear.

4. No gap or dead ends.

5. Sequence of elements must be in logical form.

6. Flowchart must be used correctly.

Page 15: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

• General Concept

• Levels of Program Flowchart:1. Simple sequential flowchart: 2. Branched flowchart.3. Simple-loop flowchart.4. Multi-loop flowchart.

Flowchart:

Page 16: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:

Simple sequential flowchart: It is simplest level which contain the sequence of steps without loops or branch and the flowchart comes in straight line from the beginning to the end.

Page 17: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:

Branched flowchart: It is branched flowchart, when there is a condition statement in the program.

Page 18: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:

Simple-loop flowchart: It is a flowchart, which contain iteration or repetitions. It is usually called loop flowcharts. In this type we need to repeat some operation several times using the same set of operation.

Page 19: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:

Multi-loop flowchart: Looping is used to manage a loop to fulfill a repetition under a definite condition. It takes the following special symbol.

Page 20: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Flowchart:Table 2.1 represents the symbols that are used to draw the flow chart.

Page 21: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Examples

Page 22: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example1:Algorithm for reading student name

Page 23: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example2:Algorithm for calculating the area of circle

Page 24: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example 3:Draw flow chart for evaluating the following functions:

• F(x) = x if x>=0• F(x) = -x if x<0

Page 25: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example 3:Draw flow chart for evaluating the following functions:

• F(x) = x if x>=0• F(x) = -x if x<0

Page 26: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example 4:Write an algorithm and draw the flow chart to find the circumference of circle.

Page 27: Chapter 2: General Problem Solving Concepts Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science Foundation Year Program Umm

Example 5:Write an algorithm and draw a flowchart to print 10 integers starting from 1.