200 likes | 370 Views
EEE 243B Applied Computer Programming. Structured Problem Solving Lecture 5 Dr. McGaughey. Problem Solving Process. ‘C’ is a structured language that lends itself to a structured problem solving process A 6 step problem solving methodology will be presented
E N D
EEE 243BApplied Computer Programming Structured Problem Solving Lecture 5 Dr. McGaughey
Problem Solving Process • ‘C’ is a structured language that lends itself to a structured problem solving process • A 6 step problem solving methodology will be presented • Can be used for engineering or computer science problems • Assumes a computer will be used to solve the problem
Structured Design • State problem clearly (requirements) • Describe input and output information • Work problem by hand (calculator) • Simple example • Develop a solution (algorithm) • Convert the algorithm and code program • Test program with variety of data • Exceptions and boundary conditions
Example Problem Problem: Calculate the distance between two points in a plane Solution 1. Problem Statement Given 2 points in a plane compute the straight line (Euclidean distance) between them
2 . Input Output • Describe in detail the values given to solve the problem (input) and identify the values to be computed (output) • Describe the type, range and units of these values E.g. positive integers, Real, [-3,-2,-1,0,1,2,3] • Input points • (x1,y1) and (x2,y2) all values are real numbers • Distance output D – real number
3. Hand Example • Work the problem by hand (or calculator) using a simple set of data. • You may need to consult reference material • Find appropriate equations or algorithms
3. Hand Example for distance • Consider two points • p1 = (1,5) and p2 = (4,7)
3. Hand Example Cont • You may wish to do further examples using non-integer points and negative points p1 = (-1.5,5.275) and p2 = (5.25,-3.775) p1 = (0,5) and p2 = (-3,0)
4. Algorithm Development • An algorithm is a step-by-step outline of the solution to a problem • This often involves decomposing a problem into smaller problems • structured design • Algorithms may exist to solve your problem so do your research
4. Algorithm for distance problem • Decomposition of problem • Give or input (x,y) values for both points • Compute length of each side • Compute the length of the hypotenuse (distance) • Output (or display) the distance
5. Convert the algorithm to code • If the decomposition outline (algorithm design) is complete is should give you all the information to develop code • inputs and their types • outputs and their types • procedure of calculating output • type and format of output • Next implement the ‘C’ Code
5. Coding the distance problem int main(void) { /* declare and initialize variables */ double x1=1, y1 = 5, x2=4, y2=7; double side1, side2, distance; /* Compute the lengths of the sides */ side1 = x2-x1; side2 = y2-y1; /*Compute the distance */ distance = sqrt(side1*side1 + side2*side2); /* Print Distance */ printf("The distance between (%5.2f,%5.2f) and (%5.2f,%5.2f) is %5.2f\n", x1,y1,x2,y2,distance); /* Exit Program */ return(0); }
5. Notes on code • Header documentation is required • The math and standard i/o libraries have been used (no need to reinvent all the wheels) • To change points this code needs recompiled • could request user input
6. Testing • ‘C’ code that compiles still can have logical, formatting, and other errors • Run the ‘C’ code on your test cases to ensure you get the correct results. • It is likely impossible to exhaustively test your program • Design of appropriate test sets including boundary conditions and exceptions is important and complex
6. Testing of Distance Code • For our 3 test cases:
Now its your turn!!! • The Sa(x) = sin(x)/x function will be used in signal processing, circuit response, filtering, etc in EE and CE • Problem: Write ‘C’ code to compute the values of the Sa(x) function for at least 2 oscillations of the function (on each side of 0) where ‘x’ is an input value. Write the x value and the output values into a text file with one pair per line. The text file should have two floating point value in exponential notation, separated by a comma, per line. Load the text file into Matlab to plot
Design Methodology • Use the design methodology presented and design code to implement the Sa(x) function • Note: There is a special condition to be computed separately
A little help …. • Here is a graph of the Sa(x) function http://en.wikipedia.org/wiki/Sinc_function
Conclusion • Groups will present their design • Questions?
Quiz Time Which of the following is NOT part of the design methodology? • Problem statement • Testing • Celebratory beer • Input and output You are to calculate the current in a resistor (R=V/I). Give a example of tests you should perform on this code.