13
CIS 338: Problem Solving Dr. Ralph D. Westfall April, 2011

CIS 338: Problem Solving Dr. Ralph D. Westfall April, 2011

Embed Size (px)

Citation preview

CIS 338: Problem Solving

Dr. Ralph D. WestfallApril, 2011

Programming Problems

need to translate a problem into language a computer can understandtranslate from specifications written, verbal

translate into an idea in your head what are we really trying to do here?

translate into objects and code

Decompose the Problem

identify application tiers and functions User Interface (UI) tier

inputs by/outputs to user, printer, etc.

Data tier (if necessary)files/layouts, database tables/fieldsnetwork connections (if necessary)

Decompose the Problem - 2

Business rules tier control structures – loops, decisions, etc. data structures: arrays, collections, etc. variables – inputs, outputs, temporary

(with meaningful variable names) operations/processing: math (+,-, etc.),

logical (compare), assignment (=), etc.

Translating

write pseudocode list of steps (recipe in human

language) to solve problem pidgin code: mix of different

computer languages

write code translate pseudocode (or pidgin code)

into computer code for one language

Testing

hand check your code go through with pencil and paper

1 line at a time with sample data

look for harmful side effects does code destroy other data or

disrupt a loop?'notes

revise code and test again

Exercise

follow the above steps with a programming problemfor loops:

1. load 20 names in an array into a ListBox

2. calculate GPA multiply corresponding items in 2 parallel

arrays (units and grade points) divide total grade points by total units

Exercise – 2: If Structures

1. display "low mileage car" when mileage is less than 50,000

2. display "low mileage" when less than 50,000, "OK mileage" for others

3. display the following "low" when mileage less than 50,000 "OK": between 50,000 and 99,999 "high": mileage 100,000 or more

Exercise - 3

if and loop together only prints item(s) from a 100-item

array that have less than 100 calories in another array

print the Index before the item to identify it

Index Item Calories0 BRAN FLAKES 90 1 ALFALFA SEEDS 102 ANGEL FOOD CAKE 1253 etc.

Exercise - 4

do Exercise 3 again, using a 2-dimensional array with 100 rows and 2 columns assume that calories are Strings and

either all have three digits or are left padded so that numeric comparisons are accurate

Random Problem Creation

use the Random Phrase Generator to generate adjectives and nouns create a problem that selects subjects

based on values of attributes

partially (all?) random Google searches BananaSLUG takes your word and adds

to it a word—randomly chosen from a selected category—to do a Google search

Creating Random Problems

what are the subjects (nouns) people: athletes, students, politicians, etc. dogs: guard, guide, working, toy, etc.

what are the attributes (adjectives) people: age, height, gender, money, etc. dogs: weight, color, how much hair, etc.

how many subjects have attributes: count, average, more than, etc.

Random Problem Example

words: unhappy spiderdata: 3 parallel 20-item arrays with species, color and unhappiness of person bitten (0-9, 9 is unhappiest)problems (data):1. count red spiders with ratings of 2 or less2. average unhappiness for each color

also see Discussion Board Midterm topic