Identify skills and personality traits of successful problem solving. Apply standard problem-solving techniques to aid in problem solving. Apply problem-solving techniques to programming activities. Generate potential solutions to problems. Formulate and successfully communicate the solutions to problems.
10%: Homework and labs 30%: 2 quizzes and one exam 20%: 2 quizzes and one exam 40%: Final exam
Types of Problems Problem Solving Steps Problem Solving Strategies Algorithm Design Techniques Review Flowcharts Review C Language Basics (variables, data types, expressions, I/O, etc.). Decisions (if statement and switch statement)
Problems with Algorithmic Solutions Problems that can be solved with a series of actions Balancing checkbook, baking a cake Alphabetizing 10,000 names Problems with Heuristic Solutions Problem solution requiring reasoning based on knowledge and experience, and a process of trial and error Solutions cannot be reached through a direct set of steps How to buy the best stock or expand the company Difficult to evaluate the solution
People are better at solving heuristic problems Artificial intelligence is the field of computer science that deals with heuristic problems Computers are better at solving complicated calculus problems or alphabetizing 10,000 names This class deals with algorithmic solutions
Identify or Define the problem Analyze the problem (inputs, outputs, formulas, constants) Design the Solution (flowchart, algorithm, pseudo-code) Identify alternative ways to solve the problem Select the best way to solve the problem from the list of alternative solutions List instructions that enable you to solve the problem using selected solution Implement (program code) Evaluate
1. Problem: Your boss wants you to convert a list of miles to kilometers. Since you like programming, so you decide to write a program to do the job. 2. Analysis We need to get miles as input We need to output kilometers We know 1 mile = 1.609 kilometers 3. Design 1. Get distance in miles 2. Convert to kilometers 3. Display kilometers 9
10
Abstraction Brainstorming Divide and conquer Trial-and-error We need to THINK!
We need to read it till we understand every detail We need to dissect the problem into its component parts (e.g. problems and sub-problems) We need to remove any ambiguity, extra information We need to determine our knowns and our unknowns We need to be aware of any assumptions we are making.
A car has a fuel capacity of 12 gallons and a fuel consumption of 45 miles per gallon. Gas costs 2.56 a gallon. How far can the car travel on 16.64 worth of petrol?). What are the knowns and the unknowns? Where, if anywhere, is there ambiguity? What are the component parts of the problem? What information is extraneous to the problem solution? What assumptions have you made about the problem?
Brute-Force: Try all possibilities Divide and Conquer: Requires a way of breaking the problem into sub-problems, of solving the trivial cases and of combining sub-problems to the original problem (Merge Sort). Sub-problems are independent. Greedy: Find the local optimal step at each stage. Not necessarily globally optimal. Traveling salesman problem.
Function Header Function Body 24
An identifier must consist only of letters, digits, and underscores. An identifier cannot begin with a digit. A C reserved word cannot be used as an identifier. A standard identifier should not be redefined. C compilers are case sensitive. (Rate, rate and RATE are viewed as different identifiers) Valid identifiers: letter1, inches, KM_PER_MILE Invalid identifiers: 1letter, Happy*trout,return 2 5
26
SYNTAX printf( format string, print list ) ; Printf(format string); Examples : Place holder printf( That equals %f kilometers. \n, kms); printf( enter the distance in miles> ); printf( Hello, World?\n ); Escape sequence 27
28
SYNTAX scanf( format string, input list ) ; Examples : Place holder scanf( %lf, &miles); Ampersand 29
30
Write a program to ask the user for the width and length of a piece of land and then tell him how many orange trees he can grow on it. Given that each orange tree requires 4 m 2. 31
Programming Examples #include <stdio.h> # define one_tree_space 4 int main() { int length,width, area, no_of_tree; printf( Enter length of the land> ); scanf( %d, &length); printf( Enter width of the land> ); scanf( %d, &width); area = length * width; no_of_tree = area / one_tree_space; printf( The available number of trees is %d trees\n, no_of_tree); return(0); } 32
the result of the division operator depends on the type of its operands if one or both operands has a floating point type, the result is a floating point type. Otherwise, the result is an integer type Examples 11 / 4 has value 2 11.0 / 4.0 11 / 4.0 has value has value 2.75 2.75 15 / 3 has value 5 16 / 3 has value 5 33
the modulus operator % can only be used with integer type operands and always has an integer type result its result is the integer type remainder of an integer division EXAMPLE 3 % 5 = 3 5 % 3 = 2 4 % 5 = 4 5 % 4 = 1 5 % 5 = 0 6 % 5 = 1 7 % 5 = 2 8 % 5 = 3 15 % 6 = 3 15 % 5 = 0 34
Most conditions that we use to perform comparisons will have one of these forms: variable relational-operator variable e.g. a < b variable relational-operator constant e.g. a > 3 variable equality-operator variable e.g. a = = b variable equality-operator constant e.g. a!= 10 35
Operator Meaning Type < less than relational > greater than relational <= less than or equal to relational >= greater than or equal to relational == equal to equality!= not equal to equality 36
logical expressions - expressions that use conditional statements and logical operators. && (and) A && B is true if and only if both A and B are true (or) A B is true if either A or B are true! (not)!(condition) is true if condition is false, and false if condition is true This is called the logical complement or negation Example (salary < 1000) (dependents > 5) (temperature > 30.0) && (humidity > 90)!(temperature > 90.0) 37
if ( Expression ) StatementA else StatementB NOTE: StatementA and StatementB each can be a single statement, a null statement, or a block. 3 8
Write a program to calculate the total price of a certain purchase. There is a discount and shipping cost: The discount rate is 25% and the shipping is 10.00 if purchase is over 100.00. Otherwise, The discount rate is 15% and the shipping is 5.00 pounds. 3 9
if ( purchase > 100.00 ) { discountrate =.25 ; shipcost = 10.00 ; } else { discountrate =.15 ; shipcost = 5.00 ; } totalbill = purchase * (1.0 - discountrate) + shipcost ;
Used to select one of several alternatives BASED on the value of a single variable. This variable may be an int or a char but NOT a float ( or double). 4 1
char grade ; printf( Enter your letter grade: ); scanf( %c, &grade); switch ( grade ) { case A : printf( Excellent Job ); break; case B : printf ( Very Good ); break; case C : printf( Not bad ); break; case F : printf( Failing ); break; default : printf( Wrong Input ); } 4 2
Write a program to ask the user for the brightness of a light bulb (in Watts), and print out the expected lifetime: Brightness Lifetime in hours 25 2500 40, 60 1000 75, 100 750 otherwise 0
int bright ; printf( Enter the bulb brightness: ); scanf( %d, &bright); switch ( bright ) { case 25 : printf( Expected Lifetime is 2500 hours ); break; case 40 : case 60 : printf ( Expected Lifetime is 1000 hours ); break; case 75 : case 100 : printf( Expected Lifetime is 750 hours ); break; default : printf( Wrong Input ); }