Top Banner
Basic Concepts of Programming Mrs. Gonzales
40

Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Jan 19, 2016

Download

Documents

Erika Eaton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Basic Concepts of Programming

Mrs. Gonzales

Page 2: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

PROBLEMS

•A problem is a discrepancy between what is required and what exists.

Page 3: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

SOLUTIONS

Three

Two

One

Page 4: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

ALGORITHM

•An algorithm is a sequence of instructions which, if followed in order, produces a solution to a given problem.

Page 5: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

PROBLEM SOLVING

• This a thought process that requires a critical analysis of the situation (the problem) and careful consideration of possible ways of overcoming the problem.

Page 6: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

PSEUDOCODE

Pseudo code is an algorithm that models or resembles the real programming language of the computer. It cannot be executed by the computer.

Page 7: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

The six main problem-solving steps

Whenever you attempt to solve a problem, there are certain steps you should follow.

1. Define the problem 2. Propose and evaluate solutions3. Determine the most efficient solution.4. Represent the most efficient solution in the form

of an algorithm5. Test and debug the program6. Document the program (i.e. a written explanation

of how the program works and how to use it)

Page 8: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Problem

How do we fry an egg?

Write the steps involved in frying an egg.

Page 9: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Step 1

Defining the problem

In defining the problem there must be no ambiguity. The problem should be clear and concise and have only one meaning.

Page 10: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Step 2

Analyzing the problem

In order to write a program to accomplish a certain task you must analyse the task as a sequence of instructions that can be performed by the computer.

These instructions can be divided into three main parts: Input and Storage instructions, Processing instructions and Output instructions.

Page 11: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

ExampleWrite a program to enter the base and height of a triangle and find and print the area

Input & Storage Processing Output

Page 12: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Questions

• 1. Write a program to enter an individual’s year of birth and the current year and calculate and return the individual’s age.

• 2. Write a program to enter a number, double the number, and output the result.

Page 13: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Input and Storage Instructions

• These instructions allow information or data to be accepted by the computer.

• The input is information that is needed by the computer to solve the problem.

• Within problem statements there are words that indicate what data the computer requires like ‘Enter’, ‘Input’, and ‘Read’.

Page 14: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Processing Instructions

• These instructions manipulate the input data.• They involve:

- Calculations- Repeating instructions- Selecting instructions- Comparing instructions- Commands and Constructs.

Page 15: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Commands

• A command is a word that instructs the computer what must be done to accomplish a specific task.

• Commands are specific to a particular programming language.

• Example: WRITE, PRINT, READ, INPUT, etc.

Page 16: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Constructs

• A construct is a group of instructions that work together with commands to accomplish a specific task.

• An example is the ‘IF-THEN’ construct.

IF a < 10 THENRead NumENDIF

Page 17: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Comparison Statements

• Comparison / selection/ decision statements involve determining whether a condition is true or false in order to perform a set of instructions.

Page 18: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Repeat Statements

• Repeat statements / looping / iterations are used when a problem has to be done a number of times.

WHILE m <> 0 DOPRINT y

FOR x = 1 to 5 DO r = x * 10

Page 19: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Output Instructions

• These instructions allow information to be displayed on the screen.

• Problem statements contain key words that indicate what data should be output to the screen, like –

• ‘PRINT’, ‘OUTPUT’, ‘DISPLAY’, ‘RETURN’, ‘WRITE’.

Page 20: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Step 3

• Developing an Algorithm–This involves the use of :

* Constants* Variables

Page 21: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Constants

• A constant identifies a memory location where a fixed item or data is stored.

• This data does not change but remains the same during the execution of the program.

There are two main types:* Numeric constants* String constants / character strings

Page 22: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Data Types

• The types of data can be divided into three broad categories:

1) Textual Data2) Numerical Data3) Boolean data

Page 23: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Examples of Data TypesTextual Data Numerical Data Boolean Data

This may include a combination of letters, symbols, and numbers.

These are simply numbers.

This is data that must be either true or false.

(1) Character (1)Real (1) TrueExamples: a,b,c,$,^,*,2,9,0

Numbers with a decimal point. Examples: 5.6, -43.7

Examples:The sky is blue – True10 = 6 + 4 – True

(2) String (2) Integer (2) FalseDavis, Shania, ‘Average’

Numbers without a decimal point. Examples: 5, 18, -20

Examples:The dog can fly - False10 > 25 – False

Page 24: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Exercise

• State the type of Data that would be use to represent each of the following:

DATA TYPE

The Grade a student got in a course, e.g. A,B,C,D,F

Character

The time Usain Bolt takes to run a 100m race (in seconds)

Real

The number of books in person’s bag Integer

Whether or not a piece of luggage is overweight Boolean

The cost of an item in a store Real

A person’s name String

Page 25: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Variables

• A variable identifies a memory location in which an item or data can be stored. The value of the data can be changed.

• Memory locations are given names that allow the computer to locate the relevant data easily.

• During the execution of the program, instructions may cause the contents in these memory locations to change or vary, hence the term variable.

Page 26: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Types of Variables

Numeric Variables:• These are used to store numbers only.

String Variables:• These are used to store a set, or string,

of characters.

Page 27: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Examples of Variables

• Storage Locations

Firstname Height Age MarksVariable Names

Mariah 154 15 85

Page 28: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Writing Instructions1) Data Input and storage instructions

Commands used to input data are READ or INPUT.

Syntax: READ <Variable name> <Variable name>Example: READ Name, Score

……………………………………………………………………………………………………………………..

2) Prompting instructionsCommands used to prompt the user are PRINT or WRITE.

Syntax: PRINT <String>Example: PRINT “Enter student name”

……………………………………………………………………………………………………………………..

3) Output instructionsCommands used to output data are PRINT or WRITE.Syntax: PRINT <Variable name> <Variable name>Example: PRINT Name, ScoreKenny, 98

Page 29: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Example of Instructions• Write a program to enter the base and height

of a triangle and find and print the area.

Program Area;

Var Base, Height: Integer; Area: Real;BEGINArea := (Base * Height) / 2;PRINT “Enter the Base”;READ Base;PRINT “Enter the Height”;READ Height;PRINT Area;

END.

Page 30: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Operators• Operators are symbols used for

performing calculations or making comparisons.

• There are three main types of operators;1) Arithmetic2) Relational3) Logical

Page 31: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Arithmetic Operators

• These are operators used to perform mathematical operations.

Arithmetic Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

Page 32: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Relational Operators

• These are used to check for comparisons – to compare the value of one variable to another variable or that of a constant.

Relational Operator Operation

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

<> Not equal to

= Equal to

Page 33: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Logical Operators

• These are used to make comparisons with multiple criteria.

Logical Operator Operation

AND And

OR Or

NOT Not

Page 34: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Step 4

• Writing a Computer Program corresponding to the Algorithm.– Generations of Programming Languages:1. First Generation (1GL) – low level language

2. Second Generation Language (2GL) – low level language

3. Third Generation (3GL) – high level language

4. Fourth Generation Language (4GL) – high level language

5. Fifth Generation Language (5GL) – high level language

Page 35: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Pascal

• Pascal is a (3GL) and was designed to be used in teaching programming, and in business programs.

• (Proceed to Tutorial – Lesson 1 and 2)

Page 36: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Step 5

• Testing and Debugging the Program

• This is necessary to ensure that the algorithm or program is doing what it was designed to do, and is doing so correctly.

• There are two types of testing;1. Manual testing / dry running2. Computer testing

Page 37: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Manual Testing / Dry Running

• This involves the technique of tracing through the use of a trace table.– The Algorithm

Count = 0While Count < = 10 DOCount = Count + 2Print CountEndWhile

Page 38: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Trace Table

Count Output

0

2 2

4 4

6 6

8 8

10 10

• Count is set as 0; Count < 10.

• 2 is added to count and 2 is printed. Count < 10.• 2 is added to count and 4 is printed. Count < 10.• 2 is added to count and 6 is printed. Count < 10.• 2 is added to count and 8 is printed. Count < 10.• 2 is added to count and 10 is printed.

Count <= 10 so the loop is exited.

Page 39: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Computer Testing

• Computer testing involves running the program using test data to discover any errors in the program.

• There are three types of errors:– Syntax errors– Logic errors– Runtime errors

Page 40: Three Two One Whenever you attempt to solve a problem, there are certain steps you should follow. 1.Define the problem 2.Propose and evaluate.

Step 6

• Documenting the Program– This involves writing an explanation of how the

program works and how to use it.

• There are two types:1. User documentation / user guides2. Technical documentation

(Proceed to Tutorial – Lesson 3 to 7)