130 likes | 257 Views
CIS 338: Problem Solving. Dr. Ralph D. Westfall April, 2011. Programming Problems. need to translate a problem into language a computer can understand translate from specifications written, verbal translate into an idea in your head what are we really trying to do here?
E N D
CIS 338: Problem Solving Dr. Ralph D. Westfall April, 2011
Programming Problems • need to translate a problem into language a computer can understand • translate 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/fields • network 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 problem • for loops: • load 20 names in an array into a ListBox • calculate GPA • multiply corresponding items in 2 parallel arrays (units and grade points) • divide total grade points by total units
Exercise – 2: If Structures • display "low mileage car" when mileage is less than 50,000 • display "low mileage" when less than 50,000, "OK mileage" for others • 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 Calories 0 BRAN FLAKES 90 1 ALFALFA SEEDS 10 2 ANGEL FOOD CAKE 125 3 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 spider • data: 3 parallel 20-item arrays with species, color and unhappiness of person bitten (0-9, 9 is unhappiest) • problems (data): • count red spiders with ratings of 2 or less • average unhappiness for each color • also see Discussion Board Midterm topic