introduction to computer programming

30
Introduction to Computer Programming Lesson 2

Upload: cain-rose

Post on 01-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Computer Programming. Lesson 2. How People Solve Problems. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Introduction to Computer Programming

Lesson 2

How People Solve Problems

• 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

How Computers Solve Problems

• 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

Algorithms (source : wikipedia)

• In mathematics, computing, linguistics and related disciplines, an algorithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminatee in a defined end-state.

• Algorithms are essential to the way computers process information, because a computer program is essentially an algorithm that tells the computer what specific steps to perform (in what specific order) in order to carry out a specified task, such as calculating employees’ paychecks or printing students’ report cards. Thus, an algorithm can be considered to be any sequence of operations which can be performed by a Turing-Complete system.

Problem Solving

• 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

Expressing the Algorithms

• A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily

• There are two standards in programming:– NATURAL LANGUAGE– PSEUDOCODE – FLOWCHARTS– PROGRAMMING LANGUAGE

Expressing The Algorithms

• Algorithms can be expressed in many kinds of notation, including natural languages, pseudocode, flowcharts, and programming languages. Natural language expressions of algorithms tend to be verbose and ambiguous, and are rarely used for complex or technical algorithms. Pseudocode and flowcharts are structured ways to express algorithms that avoid many of the ambiguities common in natural language statements, while remaining independent of a particular implementation language. Programming languages are primarily intended for expressing algorithms in a form that can be executed by a complete, but are often used as a way to define or document algorithms.

Natural Language

• "...prose to describe an algorithm, ignoring the implementation details. At this level we do not need to mention how the machine manages its tape or head“

• In the philosophy of language, a natural language (or ordinary language) is a language that is spoken, written, or signed (visually or tactilely) by humans for general-purpose communication, as distinguished from such constructs as computer-programming languages or the "languages" used in the study of formal logic, especially mathematical logic.

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

• Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does

Pseudocode (wikipedia)

• 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.

Flowcharts

• A Flowchart is a Visual Representation of an algorithm

• 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

Selection

• Selection is the decision-making construct

• It is used to make yes/no or true/false decisions logically

• Selection can be thought of as “if something is true take this action, otherwise take that action”

The Three Computer Programming Constructs

• Any problem, regardless of how complex, can be broken down into three basic CONSTRUCTS

– SEQUENCE

– SELECTION

– ITERATION

Sequence

• Sequence is the most basic of the constructs

• It is simply performing one step after another

• Each step is followed in a specific sequence, hence the name

• Sequence can be thought of as “do this, then do this, then do this”

Iteration

• Iteration comes from the word “reiterate”, which means to repeat

• Iteration is a looping construct

• Iteration is a combination of decision and sequence and can repeat steps

• Iteration can be thought of as “while something is true, do this, otherwise stop”

Visualizing The Constructs

• By using Flowchart symbols, we can visualize the three basic computer constructs

• Every algorithm ever created is made up of only these three constructs

Basic Flowchart Symbols

ProcessTerminator Input/Output

Decision

Connector Data Flow

The Basic Flowchart Symbols

• The terminator shows where a specific piece of an algorithm beings and ends

• A process is a basic action or actions on data

• A decision is a logical test with two possible data paths resulting from one

The Basic Flowchart Symbols

• Input/Output gets data or displays information to the user of the algorithm

• A connector connects two or more data flows into one

• A data flow shows the direction of data’s movement through the algorithm

Sequence

Selection

TF

Iteration

TF

Example :

• You have problem when staying at home. Your lamp doesn’t work and you need the light. Express the algorithm to solve the problems !

Algorithms :Algorithms :

1.1. Lamp doesn’t workLamp doesn’t work

2.2. Is lamp plugged in ?Is lamp plugged in ?

3.3. If No plug in lamp and If No plug in lamp and finishfinish

4.4. If yes, Check the bulb if it If yes, Check the bulb if it is burned out ?is burned out ?

5.5. If yes replace bulbIf yes replace bulb

6.6. Buy new lampBuy new lamp

Example :

One of the simplest algorithms is to find the largest number in an (unsorted) list of numbers. The solution necessarily requires looking at every number in the list, but only once at each. From this follows a simple algorithm, which can be stated in a high-level description English prose, as:

– Natural language:Natural language:

Assume the first item is largest.

Look at each of the remaining items in the list and if it is larger than the largest item so far, make a note of it.

The last noted item is the largest in the list when the process is complete.

– (Quasi-) Formal description:(Quasi-) Formal description: Written in prose but much closer to the high-level language

of a computer program, the following is the more formal coding of the algorithm in pseudo code :

Algorithm LargestNumber Input: A non-empty list of numbers L. Output: The largest number in the list L. largest ← L0 for each item in the list L≥1, do if the item > largest, then largest ← the item return largest

• "←" is a loose shorthand for "changes to". For instance, "largest ← item" means that the value of largest changes to the value of item.

• "return" terminates the algorithm and outputs the value that follows.

Make the Flowchart for the example above !!!

Assignment !

• Make an algorithms in high level language, pseudo code and flowchart for

1. Computing factorial N (N!) !

2. Finding roots of aX2 + bX + c = 0

3. Bisection Method to find a root of the equation.