Defining the Problem

Download Report

Transcript Defining the Problem

DEFINE THE
PROBLEM
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
1
Analysis of the Problem
1. Determine what is given - input
=>nouns, adjectives
2. Determine what is required - output
=>nouns, adjectives
3. Determine the transformations
needed(processing)
-actions needed to produce the required
output
=>verbs, adverbs
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
2
Inputs and Outputs
• Determine data that is input to the problem, data
that will be output from the problem
• Nouns
• We call the input and output data-variables
• We use a particular naming convention
–
–
–
–
No spaces
Meaningful names
Usually begin with lowercase
Examples: roomWidth, numPeople, studentName
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
3
Example 1:
Read three numbers, add them together and print the total.
Read three numbers, add them together and print the total.
Step 1 – define inputs and outputs(hint: look at nouns)
Break up several values into separate variables
Input
Processing
Output
total
number1
number2
number3
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
4
Example 1:
Read three numbers, add them together and print the total.
Read three numbers, add them together and print the total.
Step 2: define list of actions. Hint: Use verbs,these steps
usually involve the input and output defined in step 1
Input
Processing
Output
Read three numbers
total
number1
Add numbers together
number2
Print total number
number3
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
5
Example 2:
Write a program to prompt the operator for the maximum and
minimum temperature readings on a particular day, accept those
readings as integers, and calculate and display on the screen the
average temperature.
Input
maxTemp
Processing
Output
avgTemp
minTemp
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
6
Example 2:
Write a program to prompt the operator for the maximum and
minimum temperature readings on a particular day, accept those
readings as integers, and calculate and display on the screen the
average temperature.
Input
maxTemp
Processing
Output
Prompt for temps
avgTemp
Calculate average
minTemp
Display average
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
7
PLAN THE
SOLUTION
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
8
Plan the Solution
• Outline the Solution
– Major processing steps involved
– Major subtasks
– Major control structures
– Major variables and record structures
– Mainline logic
• Develop the outline into an algorithm
• Test the algorithm for correctness
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
9
Structured Programming
• Top Down Design & Development
• Modularity
• Control structures
– sequence
– selection or IF-THEN-ELSE
– repetition or looping
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
10
Design/Plan the solution
• Algorithm – a step by step procedure for
solving a problem
• Programs - implementations of algorithms
– Set of instructions
• flowcharts
• pseudocode
– test solution for correctness - trace
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
11
Pseudocode
• structured concise English statements
used to state operations
• language independent,no standard
• use words and phrases that are in line
with basic computer operations
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
12
Pseudocode
1. Write statements in simple English
2. Write each instruction on a separate
line.
3. Use keywords and indentation for
control structures.
4. Write instructions from top to bottom,
with only one entry and one exit.
5. Groups of statements may be formed
into modules.
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
13
Pseudocode
1. Input - receiving information
• keywords - Read, Get, Input
• Read - receive information from a record on a
file
• Get - receive information from the keyboard.
Read studentName
Get systemDate
Read num1,num2
Get taxCode
• use one verb followed by one or more nouns
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
14
Pseudocode
2. Output
– putting information out
• Put, Output,Display - screen
• Print - printer
• Write - file
–
–
–
–
–
Print smaller.
Print "Program Completed"
Write customer_record to file
Output totalTax
Display "End of Data"
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
15
Output continued
• Labels
– Display "label"
• statement inside quotes is printed as it appears
– Display "x"
• Yields
// if x = 5
x
– Display x
• yields
5
– Display "X = " and x
• yields:
X=5
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
16
Pseudocode
3. Arithmetic
• use name of the operation or algebraic symbol
• add,subtract,multiply, divide, exponentiation
• + - * / **
• x = algebraic expression
–
–
–
–
count = count + 1
netPay = hourlyPayRate * hoursWorked
salesTax = cost * 0.10
totalPrice = price + salesTax
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
17
Pseudocode
4. Assignment - place information into a storage
location
• Initialize, Set - give variable initial value
• Save,Store - save info for later
• =
//preferred
y = 3 note: does not mean equality
– could involve calculation:
– area = length * width
cosc175 - Define Problem/Design
Solution/Pseudocode/Trace
18