Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.

Similar presentations


Presentation on theme: "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02."— Presentation transcript:

1 COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02

2 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language During development of an algorithm, the language gradually progresses from English towards a programming language notation. An intermediate notation called pseudo-code is commonly used to express algorithms.

3 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Algorithmic Structure Every algorithm should have the following sections, in the stated order: Header : Algorithm’s name or title. Declaration : A brief description of algorithm and variables. i.e. a statement of the purpose. Body : Sequence of steps Terminator : End statement

4 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language How to write Pseudocode An algorithm can be written in pseudocode using six (6) basic computer operations: A computer can receive information. A computer can output (print) information. A computer can perform arithmetic operation A computer can assign a value to a piece of data: A computer can compare two (2) pieces of information and select one of two alternative actions. A computer can repeat a group of actions.

5 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language A computer can receive information. Typical pseudocode instructions to receive information are: Read name Get name Read number1, number2 A computer can output (print) information. Typical pseudocode instructions are: Print name Write "The average is", ave

6 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language A computer can perform arithmetic operation Typical pseudocode instructions: Add number to total OR Total = Total + Number Ave = sum/total A computer can assign a value to a piece of data: e.g. to assign/give data an initial value: Initialize total to zero Set count to 0 To assign a computed value: Total = Price + Tax

7 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language A computer can compare two (2) pieces of information and select one of two alternative actions. Typical pseudocode e.g. If number < 0 then add 1 to neg_number else add one to positive number end-if

8 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language A computer can repeat a group of actions. Typical pseudocode e.g. Repeat until total = 50 read number write number add 1 to total end-repeat OR while total < = 50 do: read number write number add 1 to total end-while

9 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Now, let’s review the plan and write out the algorithm for the average problem in the specified format: Header : Algorithm’s name or title. Declaration : A brief description of algorithm and variables. i.e. a statement of the purpose. Body : Sequence of steps Terminator : End statement

10 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Algorithm Average This algorithm reads a list of numbers and computes their average. Let: SUM be the total of the numbers read COUNTER be the number of items in the list AVE be the average of all the numbers Begin Set SUM to 0, Set COUNTER to 0.

11 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language While there is data do: Read number COUNTER = COUNTER + 1 SUM = SUM + number end-while if COUNTER = 0 then AVE = 0 else AVE = SUM/ COUNTER Write “Average = ” AVE Stop.

12 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language A Systematic Approach to Defining a Problem Defining the problem is the first step towards a problem solution!!!! A systematic approach to problem definition, leads to a good understanding of the problem. A tested method for defining any given problem: Divide problem into three (3) separate components: (a) Input or source data provided (b) Output or end result required (c) Processing - a list of what actions are to be performed

13 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language EXAMPLE 1: A program is required to read three (3) numbers, add them and print their total. Solution The Defining Diagram: INPUT PROCESSING OUTPUT Read : Num1, Num2, Num3 Total = Num1 + Num2 + Num3 Print : Total

14 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Pseudocode for EXAMPLE 1 Algorithm Add _Numbers Begin Read Num1, Num2, Num3 Total = Num1 + Num2 + Num3 Print Total Stop

15 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Top-Down Design Approach (Modularization) Methodology involves: ● breaking a problem into a set of sub-problems; ● breaking each sub-problem into a set of tasks; ● breaking each task into a set of actions.

16 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Example 1 Problem: Add 23 and 35 Refinement: No further refinement is required. Example 2 Problem:Turn on a light bulb Refinement: Sub-problem 1: Locate switch Sub-problem 2: Depress switch Note each sub-problem in this case required only one task which also required only one action

17 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language EXAMPLE 3 Problem: Given a list of test scores, find the highest and lowest score and the average score. Sub-problem 1: Read test scores Sub-problem 2: Find highest score Sub-problem 3: Find lowest score Sub-problem 1 can be considered as one action and therefore needs no further refinement. Sub-problems 2 and 3 can be further divided into a group of actions. This is left as an exercise for you.

18 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Advantages of the Top-Down Design Method It is easier to comprehend the solution of a smaller and less complicated problem than to grasp the solution of a large and complex problem. It is easier to test segments of solutions, rather than the entire solution at once. This method allows one to test the solution of each sub-problem separately until the entire solution has been tested. It is often possible to simplify the logical steps of each sub-problem, so that when taken as a whole, the entire solution has less complex logic and hence easier to develop.

19 COMP6015 - An Introduction to Computer Programming : University of the West Indies The Algorithmic Language Advantages of the Top-Down Design Method A simplified solution takes less time to develop and will be more readable. The program will be easier to maintain.

20 COMP6015 - An Introduction to Computer Programming : University of the West Indies Summary Steps to Problem-Solving: Imperative that the problem be defined PROPERLY! Level 1 - Abstraction: Clarify the nature of the problem Decide how to find a solution to the problem Judge the appropriateness of a potential solution to the problem Evaluate the implementation of a solution to the problem

21 COMP6015 - An Introduction to Computer Programming : University of the West Indies Summary Steps to Problem-Solving: Level 2 - Implementation Level Can you "see" what the problem needs in order to be solved? Do you know of a technique which might be useful in solving the problem? Do you know of a technique which might be useful in solving the problem if you could use it in a more abstract, or sophisticated way?

22 COMP6015 - An Introduction to Computer Programming : University of the West Indies Summary CHARACTERISTICS of a Good Algorithm: The Right Problem Has Been Solved The Implementation is free of Errors The Algorithm is Well Documented The solution is Robust The Solution Is Maintainable You can make your solution maintainable by: ● dividing the solution into modules ● making sure that the algorithm is well-documented ● using symbolic constants

23 COMP6015 - An Introduction to Computer Programming : University of the West Indies Summary Elements of Programming Style: ● Choice of Identifiers ● Comments ● Indentation ● Effective Use of White Spaces

24 COMP6015 - An Introduction to Computer Programming : University of the West Indies EXERCISE The Electricity Company requires a program which computes charges for subscribers’ electricity consumption. The company maintains a file on all its subscribers – domestic and commercial. Each record in the file consists of the subscriber’s name, account number, account type, address and the number of kilowatt-hours consumed. For commercial accounts, the rate charged is 25¢ per KWH. For domestic accounts, the rates charged are as follows: 10¢ per KWH on the first 100KWH 25¢ per KWH on the next 200KWH 55¢ per KWH on the remaining KWHs in excess of 300 KWH e.g. The charge for 416 KWH would be: (100 x 10¢) + (200 x 25¢) + (116 x 55¢) Design an algorithm that will read all the records in the file and produce a report for subscribers as follows: Subscribers Name Account No. KWH Used Amt Due. xxxxxxx xxxxxxx xxx-xxx-xxxx xxx 999.99


Download ppt "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02."

Similar presentations


Ads by Google