SECTION 2
STAGES OF PROGRAM DEVELOPMENT  PROBLEM DEFINITION  ANALYSIS  DESIGN A SOLUTION USING AN ALGORITHM  WRITE A COMPUTER PROGRAM  TEST AND DEBUG THE PROGRAM  DOCUMENT THE PROGRAM Problem- Solving Phase Implementation Phase
PROBLEM SOLVING PHASE: STEP 1: Define the problem STEP 2: Find the solution to the problem STEP 3: Evaluate alternative solutions STEP 4: Represent the most efficient solution as an algorithm STEP 5: Test the algorithm for correctness
STAGE 1: DEFINING THE PROBLEM Stating what the problem is and what the end result should be. A problem should be decomposed into 3 key components: 1. INPUT 2. PROCESS 3. OUTPUT
DEFINING THE PROBLEM INPUT (What is given) OUTPUT (The expected output) PROCESS (Tasks that must be performed)
Example:  Write a program that enters a student name and date of birth and calculate the child’s age and write the name and age of the child.
PROBLEM DEFINITION:  Required to find the age of a child  DEFINING DIAGRAM INPUT PROCESS OUTPUT 1. Student Name Calculate the child’s age 1. Student Name 2. Student DOB From the child’s DOB 2. Student Age
Questions 1. Write a program that is required to read three (3) numbers, calculate and print their total. 2. Give three integers representing the age of three boys respectively, write a program to find their average and also determine the age of the oldest boy. 3. Write a program to enter the base and height of a triangle and find and print the area. 4. Write a program to read the temperature in degrees Celsius and convert it to degrees Celsius and Fahrenheit.
PROBLEM DEFINITIONS: 1. Required to find the Total of three numbers. 2. Required to find the average age of 3 boys and the age of the oldest boy. 3. Required to find the Area of a triangle. 4. Required to find the Fahrenheit equivalent of a temperature in degrees Celsius.
DEFINING DIAGRAM: QUES. 1 INPUT PROCESS OUTPUT NUM1 GET NUM1, NUM2, NUM3 TOTAL/SUM NUM2 CALCULATE TOTAL USING NUM1, NUM2,NUM3 NUM3 DISPLAY TOTAL
QUES. 2 INPUT PROCESS OUTPUT AGE1 1. Read AGE1, AGE2, AGE3 1.AVERAGE AGE AGE2 2. CALCULATE AVERAGE AGE USING AGE1, AGE2, AGE3. 2. AGE OF OLDEST BOY AGE 3 3. FIND AGE OF OLDEST BOY FROM AGE1, AGE2, AGE3 4. DISPLAY AVERAGE, AGE OF OLDEST BOY
QUES. 3 INPUT PROCESS OUTPUT BASE READ BASE, HEIGHT AREA OF TRIANGLE HEIGHT CALCULATE AREA USING BASE AND HEIGHT DISPLAY AREA
QUES. 4 INPUT PROCESS OUPUT DEGREES CELCIUS READ DEGREES CELCIUS DEGREES FAHRENHEIT CALCULATE DEGREES FAHRENHEIT USING DEGREES CELCIUS DISPLAY DEGREES FAHRENHEIT
ALGORITHMS  An algorithm is a series of steps to be followed to perform a task. An algorithm should be: 1. Precise 2. Unambiguous 3. Finite (i.e. terminate after a given number of steps) 4. Instructions must be in Logical order
Questions 1. Identify which of the options gives the steps in correct sequence: (a) Get on the bus, get dressed for school, get off the bus at school (b) Get dressed for school, get off the bus at school, get on the bus (c) Get off the bus at school, get on the bus, get dressed for school (d) Get dressed for school, get on the bus, get off the bus at school 2. Consider the following algorithms & identify which one accurately gives the area of a square: (a) Get length and width Area = length x width (b) Get side measurement of square Area = side x side (c) Get area of square Area =Area x 2 (d) Get side measurement of square Area = side x 4
3. Consider the following algorithm and choose the correct step to make it complete: Purchase some ice-cream and toppings Get ice-cream scoop Use scoop to place two scoops of ice-cream on cone Cover with sprinkles Add a cherry to the top (a) Eat cake (b) Go to the cinema (c) Purchase cone (d) Enjoy the movie
VARIABLES  A Variable is a location in memory where data of different types are stored. When you declare a variable the computer sets aside a location in memory to store data. num1 num2 sum E.g. var num1, num2, sum: integer;
 A variable can be given a value or a variable can be set to a specific value. E.g. num1:= 1 Or the user inputs the value for num1 (1, 2, 3, 5, 90, etc.)
 A variable can hold ONLY one value at a time. If a new value is entered, the old one is lost. e.g. 5 is currently being stored in the num1 memory location with the label, num1 (variable). If you enter a new value, say 15 for the variable num1, the value 5 is replaced by the value num1 15. 5 15
1)Write a program that is required to read three (3) numbers, calculate and print their total. 2)Write a program to enter girls’ heights. Calculate the average height of the girls. • Underline the variables in your program! 3)Identify all the variables in the program AreaOfSquare.
1. Write an algorithm to find the sum of two numbers and display the sum. 2. Write an algorithm to enter the name of an item, Cost Price of an item, the number of items purchased; calculate the Total Cost of items and display the name of the item and total cost of the items. 3. Write an algorithm to enter a student’s name, three marks; calculate the average mark. Display the student’s name and average mark.
Program AreaOfSquare; var s, a: integer; Begin write(‘Enter length of side:’); read (s); {store length in s} a:= s*s; {calculate area; store in a} writeln; {print a blank line} writeln(‘Area of square is’, a); End.
VARIABLE NAMES/Identifiers The variable name can: • Start with a letter or underscore • Be a combination of letters, digits and underscore • The length of the variable name cannot exceed 31 characters • No spaces are allowed in the variable names
Exercise:  Identify the valid and invalid variable names: R _XYZ Alpha;beta Net Pay Maxamt Root1 2hottohandle
DATA TYPES  Integer  Real (floating point)  String  Boolean (data which can be true or false)  Character (data consisting of a single character)
Examples of data types:  Integer:- 3, -59, 0, 1987  Real:- 3.142, -5.0, 1.16  Boolean:- True or False  Character- ‘k’, ‘8’  String:- ‘Hi there’
ALGORITHMIC STRUCTURE  Header- Algorithm’s name or title  Declaration- A brief description of algorithm and variables used.  Body- sequence of steps  Terminator- end statement
PSEUDOCODE  An outline of a program, written in a form that can easily be converted into real programming statements.
CONTROL STRUCTURES  SEQUENCE  SELECTION  REPETITION (LOOP)
SEQUENTIAL STRUCTURE  INPUT STATEMENTS. For example, A. Get num1, num2 B. Read price • OUTPUT STATEMENTS. For example, A. Print Total_Cost B. Display Average • Statements with arithmetic operations: A. Sum = num1 + num2 • Statements that assign values to variables: A. Count = 0 B. Max = 20
SELECTION STRUCTURE  IF-THEN-ELSE A. If (A>B) then Display A B. If (age>= 50) Print “Old” Else Print “Young”
REPETITION: LOOP A. While (price <> 0) do read price total = total + price End while B. Repeat 10 times Print “I am good looking” End Repeat
FLOWCHARTS  Algorithms can also be expressed as flowcharts. A flowchart is a pictorial representation of an algorithm.  A parallelogram- represents the input & output operation.  A rectangle- used to represent processing/assignment statements.  A diamond- represents a decision (if-then-else).  An elliptical shape- represents terminal indicators.  Directional arrows- indicate the flow of logic in the program.
INPUT/OUTPUT PROCESSING/ASSIGNMENT DECISION START/STOP FLOW OF LOGIC
SEQUENCE
DECISION
 FOR LOOP REPITITION
WHILE LOOP
REPEAT UNTIL
http://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_flow.htm
ARRAYS  An array is a special variable where we reserve a series of locations in memory to store data items of a certain data type. These related data items are of the same data type.
DECLARING AN ARRAY  Example: var MARKS: Array[1..35] of integer; Reserved word Variable Name Group of 35 Data type MARKS is the name of the entire array. In order to access one data item the subscript is used. MARKS 1 2 3 4 5
RESOURCES
EXERCISES

Problem solving and design

  • 1.
  • 2.
    STAGES OF PROGRAMDEVELOPMENT  PROBLEM DEFINITION  ANALYSIS  DESIGN A SOLUTION USING AN ALGORITHM  WRITE A COMPUTER PROGRAM  TEST AND DEBUG THE PROGRAM  DOCUMENT THE PROGRAM Problem- Solving Phase Implementation Phase
  • 3.
    PROBLEM SOLVING PHASE: STEP1: Define the problem STEP 2: Find the solution to the problem STEP 3: Evaluate alternative solutions STEP 4: Represent the most efficient solution as an algorithm STEP 5: Test the algorithm for correctness
  • 4.
    STAGE 1: DEFININGTHE PROBLEM Stating what the problem is and what the end result should be. A problem should be decomposed into 3 key components: 1. INPUT 2. PROCESS 3. OUTPUT
  • 5.
    DEFINING THE PROBLEM INPUT (Whatis given) OUTPUT (The expected output) PROCESS (Tasks that must be performed)
  • 6.
    Example:  Write aprogram that enters a student name and date of birth and calculate the child’s age and write the name and age of the child.
  • 7.
    PROBLEM DEFINITION:  Requiredto find the age of a child  DEFINING DIAGRAM INPUT PROCESS OUTPUT 1. Student Name Calculate the child’s age 1. Student Name 2. Student DOB From the child’s DOB 2. Student Age
  • 8.
    Questions 1. Write aprogram that is required to read three (3) numbers, calculate and print their total. 2. Give three integers representing the age of three boys respectively, write a program to find their average and also determine the age of the oldest boy. 3. Write a program to enter the base and height of a triangle and find and print the area. 4. Write a program to read the temperature in degrees Celsius and convert it to degrees Celsius and Fahrenheit.
  • 9.
    PROBLEM DEFINITIONS: 1. Requiredto find the Total of three numbers. 2. Required to find the average age of 3 boys and the age of the oldest boy. 3. Required to find the Area of a triangle. 4. Required to find the Fahrenheit equivalent of a temperature in degrees Celsius.
  • 10.
    DEFINING DIAGRAM: QUES.1 INPUT PROCESS OUTPUT NUM1 GET NUM1, NUM2, NUM3 TOTAL/SUM NUM2 CALCULATE TOTAL USING NUM1, NUM2,NUM3 NUM3 DISPLAY TOTAL
  • 11.
    QUES. 2 INPUT PROCESSOUTPUT AGE1 1. Read AGE1, AGE2, AGE3 1.AVERAGE AGE AGE2 2. CALCULATE AVERAGE AGE USING AGE1, AGE2, AGE3. 2. AGE OF OLDEST BOY AGE 3 3. FIND AGE OF OLDEST BOY FROM AGE1, AGE2, AGE3 4. DISPLAY AVERAGE, AGE OF OLDEST BOY
  • 12.
    QUES. 3 INPUT PROCESSOUTPUT BASE READ BASE, HEIGHT AREA OF TRIANGLE HEIGHT CALCULATE AREA USING BASE AND HEIGHT DISPLAY AREA
  • 13.
    QUES. 4 INPUT PROCESSOUPUT DEGREES CELCIUS READ DEGREES CELCIUS DEGREES FAHRENHEIT CALCULATE DEGREES FAHRENHEIT USING DEGREES CELCIUS DISPLAY DEGREES FAHRENHEIT
  • 14.
    ALGORITHMS  An algorithmis a series of steps to be followed to perform a task. An algorithm should be: 1. Precise 2. Unambiguous 3. Finite (i.e. terminate after a given number of steps) 4. Instructions must be in Logical order
  • 15.
    Questions 1. Identify whichof the options gives the steps in correct sequence: (a) Get on the bus, get dressed for school, get off the bus at school (b) Get dressed for school, get off the bus at school, get on the bus (c) Get off the bus at school, get on the bus, get dressed for school (d) Get dressed for school, get on the bus, get off the bus at school 2. Consider the following algorithms & identify which one accurately gives the area of a square: (a) Get length and width Area = length x width (b) Get side measurement of square Area = side x side (c) Get area of square Area =Area x 2 (d) Get side measurement of square Area = side x 4
  • 16.
    3. Consider thefollowing algorithm and choose the correct step to make it complete: Purchase some ice-cream and toppings Get ice-cream scoop Use scoop to place two scoops of ice-cream on cone Cover with sprinkles Add a cherry to the top (a) Eat cake (b) Go to the cinema (c) Purchase cone (d) Enjoy the movie
  • 17.
    VARIABLES  A Variableis a location in memory where data of different types are stored. When you declare a variable the computer sets aside a location in memory to store data. num1 num2 sum E.g. var num1, num2, sum: integer;
  • 18.
     A variablecan be given a value or a variable can be set to a specific value. E.g. num1:= 1 Or the user inputs the value for num1 (1, 2, 3, 5, 90, etc.)
  • 19.
     A variablecan hold ONLY one value at a time. If a new value is entered, the old one is lost. e.g. 5 is currently being stored in the num1 memory location with the label, num1 (variable). If you enter a new value, say 15 for the variable num1, the value 5 is replaced by the value num1 15. 5 15
  • 20.
    1)Write a programthat is required to read three (3) numbers, calculate and print their total. 2)Write a program to enter girls’ heights. Calculate the average height of the girls. • Underline the variables in your program! 3)Identify all the variables in the program AreaOfSquare.
  • 21.
    1. Write analgorithm to find the sum of two numbers and display the sum. 2. Write an algorithm to enter the name of an item, Cost Price of an item, the number of items purchased; calculate the Total Cost of items and display the name of the item and total cost of the items. 3. Write an algorithm to enter a student’s name, three marks; calculate the average mark. Display the student’s name and average mark.
  • 22.
    Program AreaOfSquare; var s,a: integer; Begin write(‘Enter length of side:’); read (s); {store length in s} a:= s*s; {calculate area; store in a} writeln; {print a blank line} writeln(‘Area of square is’, a); End.
  • 23.
    VARIABLE NAMES/Identifiers The variablename can: • Start with a letter or underscore • Be a combination of letters, digits and underscore • The length of the variable name cannot exceed 31 characters • No spaces are allowed in the variable names
  • 24.
    Exercise:  Identify thevalid and invalid variable names: R _XYZ Alpha;beta Net Pay Maxamt Root1 2hottohandle
  • 25.
    DATA TYPES  Integer Real (floating point)  String  Boolean (data which can be true or false)  Character (data consisting of a single character)
  • 26.
    Examples of datatypes:  Integer:- 3, -59, 0, 1987  Real:- 3.142, -5.0, 1.16  Boolean:- True or False  Character- ‘k’, ‘8’  String:- ‘Hi there’
  • 27.
    ALGORITHMIC STRUCTURE  Header-Algorithm’s name or title  Declaration- A brief description of algorithm and variables used.  Body- sequence of steps  Terminator- end statement
  • 28.
    PSEUDOCODE  An outlineof a program, written in a form that can easily be converted into real programming statements.
  • 29.
    CONTROL STRUCTURES  SEQUENCE SELECTION  REPETITION (LOOP)
  • 30.
    SEQUENTIAL STRUCTURE  INPUTSTATEMENTS. For example, A. Get num1, num2 B. Read price • OUTPUT STATEMENTS. For example, A. Print Total_Cost B. Display Average • Statements with arithmetic operations: A. Sum = num1 + num2 • Statements that assign values to variables: A. Count = 0 B. Max = 20
  • 31.
    SELECTION STRUCTURE  IF-THEN-ELSE A.If (A>B) then Display A B. If (age>= 50) Print “Old” Else Print “Young”
  • 32.
    REPETITION: LOOP A. While(price <> 0) do read price total = total + price End while B. Repeat 10 times Print “I am good looking” End Repeat
  • 33.
    FLOWCHARTS  Algorithms canalso be expressed as flowcharts. A flowchart is a pictorial representation of an algorithm.  A parallelogram- represents the input & output operation.  A rectangle- used to represent processing/assignment statements.  A diamond- represents a decision (if-then-else).  An elliptical shape- represents terminal indicators.  Directional arrows- indicate the flow of logic in the program.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
    ARRAYS  An arrayis a special variable where we reserve a series of locations in memory to store data items of a certain data type. These related data items are of the same data type.
  • 42.
    DECLARING AN ARRAY Example: var MARKS: Array[1..35] of integer; Reserved word Variable Name Group of 35 Data type MARKS is the name of the entire array. In order to access one data item the subscript is used. MARKS 1 2 3 4 5
  • 43.
  • 44.