Cosc175 - Define Problem/Design Solution/Pseudocode/Trace 1 DEFINE THE PROBLEM.
-
Upload
patience-maxwell -
Category
Documents
-
view
212 -
download
0
Transcript of Cosc175 - Define Problem/Design Solution/Pseudocode/Trace 1 DEFINE THE PROBLEM.
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
1
DEFINE THE PROBLEM
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
2
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
3
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
4
Example 1:
Input
Processing
Output
number1
total
number2
number3
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
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
5
Example 1:
Input
Processing
Output
number1
Read three numbers
total
number2
Add numbers together
number3
Print total number
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
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
6
Example 2:
Input
Processing
Output
maxTemp avgTemp
minTemp
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.
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
7
Example 2:
Input
Processing
Output
maxTemp Prompt for temps
avgTemp
minTemp
Calculate average
Display average
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.
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
8
PLAN THE SOLUTION
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
9
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
10
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
11
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
12
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
13
Pseudocode
1. Write statements in simple English2. 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
14
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 studentNameGet systemDateRead num1,num2Get taxCode
• use one verb followed by one or more nouns
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
15
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
16
Output continued
• Labels– Display "label"
• statement inside quotes is printed as it appears
– Display "x" // if x = 5• Yields x
– Display x• yields 5
– Display "X = " and x• yields: X = 5
cosc175 - Define Problem/Design Solution/Pseudocode/Trace
17
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
18
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