three two one whenever you attempt to solve a problem, there are certain steps you should follow....
TRANSCRIPT
Basic Concepts of Programming
Mrs. Gonzales
PROBLEMS
•A problem is a discrepancy between what is required and what exists.
SOLUTIONS
Three
Two
One
ALGORITHM
•An algorithm is a sequence of instructions which, if followed in order, produces a solution to a given problem.
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.
PSEUDOCODE
Pseudo code is an algorithm that models or resembles the real programming language of the computer. It cannot be executed by the computer.
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)
Problem
How do we fry an egg?
Write the steps involved in frying an egg.
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.
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.
ExampleWrite a program to enter the base and height of a triangle and find and print the area
Input & Storage Processing Output
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.
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’.
Processing Instructions
• These instructions manipulate the input data.• They involve:
- Calculations- Repeating instructions- Selecting instructions- Comparing instructions- Commands and Constructs.
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.
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
Comparison Statements
• Comparison / selection/ decision statements involve determining whether a condition is true or false in order to perform a set of instructions.
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
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’.
Step 3
• Developing an Algorithm–This involves the use of :
* Constants* Variables
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
Data Types
• The types of data can be divided into three broad categories:
1) Textual Data2) Numerical Data3) Boolean data
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
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
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.
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.
Examples of Variables
• Storage Locations
Firstname Height Age MarksVariable Names
Mariah 154 15 85
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
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.
Operators• Operators are symbols used for
performing calculations or making comparisons.
• There are three main types of operators;1) Arithmetic2) Relational3) Logical
Arithmetic Operators
• These are operators used to perform mathematical operations.
Arithmetic Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
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
Logical Operators
• These are used to make comparisons with multiple criteria.
Logical Operator Operation
AND And
OR Or
NOT Not
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
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)
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
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
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.
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
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)