Uploaded by dsquared911

Problem Solving Handout

advertisement
JENNINGS SECONDARY SCHOOL
INFORMATION TECHNOLOGY DEPARTMENT
HANDOUT
UNIT – PROBLEM SOLVING
LESSON
1.
2.
3.
4.
5.
6.
7.
8.
9.
evaluate alternative solutions to problems and use the most efficient solution
explain the difference between constants and variables
select and use appropriate data types to declare various variables
identify output, input and processing statements with 100% accuracy
outline the structure of a pseudocode
identify flowchart symbols used to represent the three components of a problem
convert algorithm to basic pseudocode(structured-algorithm)
apply aspects of problem solving with computing to solve everyday problems
show willingness to develop algorithms by asking questions
PROBLEM SOLVING CONTENT
TASK 1: Inform a new teacher of a way he/she can get to Subway on Redcliff Street Restaurant from Golden Grove Primary
School . Propose alternative solutions to the problem and determine the most efficient one.
TASK 2: Consider the algorithm below that reads four numbers, calculated and printed the sum. Can you develop an
alternative solution to the problem? Make your proposal.
SOLUTION 1
1.
2.
3.
4.
5.
6.
7.
8.
Start
Get the first number
Get the second number
Get the third number
Get the fourth number
Add the four numbers
Print the total
Stop
HINT: In finding alternative solutions, ask yourself:


could I have used less steps and still solve the problem
could I have used a different equation and get the same result
Oral Question/Answer: In what ways do you see alternative solutions being proposed and used to solve or attempt to
solve problems at Mount Alvernia as it relates to:
1. Students’ lunch schedule
2. Tastee providing lunch within a specific time.
REPRESENTING AGLORITHMS
Two ways of representing algorithm: Pseudocode (Structured Algorithm) and Flowchart
 Pseudocode is a method of describing computer algorithms using a combination of natural language and some aspects
programming language.
 Unlike pseudocode, flowcharts describe the program flow using symbols that represent each component of the problem.
COMPONENT OF THE
PROBLEM
Input
PSEDUOCODE
Keywords: INPUT, READ
Read Num1, Num2
Process
Result = Num1 + Num2
Output
Keywords: PRINT, DISPLAY
PRINT Result
ALGORITHMIC STRUCTURE
Header:
Declaration:
FLOWCHART SYMBOLS
Num1
Num2
Result = Num1 +Num2
Result
FLOWCHART
Algorithm’s name or title
A brief description of the algorithm
Declaration of constants
Declaration of variables
BEGIN
Initializing variables
Prompt/output statement
Input statements
…..
……
Processing statement
Output statement with result
END
START
INPUT
PROCESS
THE INPUT
OUTPUT
RESULT
END
CONSTANT
A constant is a value that remains the same throughout the execution of the program repeatedly.
Data that remains constant: PI, minutes in an hour, etc.
CONCEPT OF VARIABLES
A variable is a symbolic name assigned to a memory location to store a particular value. NOTE: Use little or no literal or values in your
codes. Create variables for storage, doing so will allocate space in memory and variables are stored for later use, for printing or use in
subsequent calculations. Variables MUST be declared before they are used otherwise, there will be no location
allocated to store values entered by user, or what the program processes.


Suitable names to identify the variable to store the first number – NUM1, A or even X
Suitable names to identify the variable to store the second number – NUM2, A, or even Y

Suitable names to identify the variable to store the result – SUM, RESULT, ANSWER
A variable must not begin with a number or special characters such as %, #, $...etc
Data that changes: Price, salary, hours worked, etc
DATA TYPES
Data is defined as facts or figures, or information that's stored in or used by a computer. Data can be stored in the form of
text, numbers or characters. A data type is an attribute that specifies the type of data that the variable can hold. Some basic
data types used in programming.
Data Type
Description
Example
Variable Type
Integer
Whole numbers
-1, 0, 2, 39
Age, Quantity, etc
Real
Numbers with decimal 0.1, 3.4, 5
String
More than one character “Bob”,etc
Name, Address, etc
when the variable will store a word
Char
For a character
Gender, Letter
when the variable is likely to store a
letter or symbol
%, a, b,etc
Use
when the variable isn’t likely to have
decimal points
Price, Salary, Score, etc when the variable may have decimal
points
DECLARING VARIABLES
Pseudocode Syntax of Format is: Declare variable as: <variable name> as <data type>
Examples
Declare variable salary as real
Declare variable hours_worked as real
Declare variable firstname as string
Declare variable age as integer
Declare variable gender as char
TASK 3: This activity will help you to declare variables using appropriate data type
a) Write pseudocode statements to declare:
a)
b)
c)
d)
e)
two integer variables called num1 and num2 and give a sample data for each
a variable called average to store the average grade of a student after its being calculated
a variable called firstname to store the first name of a student.
a variable called age of a student
a variable called grade to store the letter grade of a student
b) For each variable, give ONE sample data that the variable may store. Record your result in a tabular format for
part (a) and (b). See sample table below:
Activity: Declaring variables using pseudocode statements
Task
a
b
Variable Declaration
Sample Data to use in
the program
c
d
e
OUTPUTTING INFORMATION USING PSEUDOCODE
The keywords OUTPUT, PRINT or DISPLAY may be used to output instructions and/ results to the screen. With this
statement, the screen will be blank.
Syntax to instruct/prompt user to enter information into the computer program is:
A "string literal" is a sequence of characters enclosed in double quotation marks (" ");
Example:
PRINT “String literal”
PRINT “Enter two numbers, separate by a space”
Syntax to output value stored in a variable is:
PRINT variable_name
E.g. PRINT sum
Syntax to output value in a variable with a string literal is: PRINT “String literal”, variable_name
E.g PRINT “The total is: ” , sum
TASK 4:
PAIR ACTIVITY: Write pseudocode statements or instructions to:
a) “Today is Monday”
b) print the value stored in the variable Salary
c) print the value stored in the variable sum with the string literal “The total is =”
INPUTTING DATA
Pseudocode syntax to enable input into variables is:
READ variable identifier
E.g. READ name
READ age
READ name, age
The input statement allows the user to enter a value, which corresponds with the instruction given on screen. READ name,
is storing a name in the variable location ‘name’.
TASK 5: This activity will help you write instructions to allow the program to accept values
Write pseudocode statements or instructions to:
a)
b)
c)
d)
input a number
read the name of a student
read the name and age of student in two separate statements (two different steps or lines)
read the name and age of a student in one statement (in one step or line)
PROCESSING STATEMENT are statements that perform calculation
Pseudocode syntax to perform calculation is:
variable_name assignment operator expression/equation
Psueudocode operator is the backward arrow or the equal sign. Preferably, use the arrow for pseudocode and the equal
sign for Pascal codes.
E.g. total  a + b+ c
or
count  3
average  total/count
Arithmetic Operators:
total = a + b + c
count = 3 //initializing the variable count to 3. This means the value 3 is in count
average = total/count
+ (addition) ; - (subtraction); * (multiplication); / (division)
TASK 6: This activity will help you write instructions to allow the program to perform calculations and storing
the result in a variable.
Write pseudocode statements or instructions to:
a)
b)
c)
d)
calculate the area of a circle
determine the circumference
calculate the salary of a worker
calculate the sum of two numbers
TASK 7: Represent the algorithm below using a full pseudocode and flowchart. Lessen the number of prompt
statements to improve efficiency.
1.
2.
3.
4.
5.
6.
7.
8.
Start
Get the first number
Get the second number
Get the third number
Get the fourth number
Add the four numbers
Print the total
Stop
HOMEWORK
1. Develop a 1) pseudocode and 2) flowchart to represent the algorithm below:
A customer pays a monthly fee of $37.50 for the telephone service and $3.50 per minute for long
distance calls. Input the number of minutes for a long distance call. Calculate and print the total bill.
2. Complete question 1 in programming workbook.
Download