1 / 20

Functional Decomposition

Functional Decomposition. Functional Decomposition. How to use functions to break up your code? How to test your code? How to debug your code?. Idea of Functional Decomposition . Break up of code into logical segments or group of statements

flynn
Télécharger la présentation

Functional Decomposition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional Decomposition

  2. Functional Decomposition • How to use functions to break up your code? • How to test your code? • How to debug your code?

  3. Idea of Functional Decomposition • Break up of code into logical segments or group of statements • Block of code should work to perform a logical task

  4. Benefits of Functional Decomposition • Complexity Hiding • Divide and Conquer • Code Reusage • Easier to Debug • Management of Code

  5. Simple Examples: int max(int x, int y){ if(x > y) return x; else return y; } intisPositive(int x){ if(x > 0) return 1; else return 0; } What about return -1?

  6. Debugging • 1. GDB debugger (Segmentation Fault) gcc –g MyProject.c gdba.out • 2. printfstatements • Output is buffered in C! • fflush(stdout); • printf will only write to screen immediately if you use fflush(stdout)!

  7. What is GDB? • GDB is the gcc debugger. • You can load your program into gdb, and run your code line by line. This allows you to: • Observe the flow of your code • Print variable values at certain points

  8. Getting Started • Compile with the –g flag: • Ex: If you have a program in the file gdb_test.c: gccgdb_test.c –o gdbTest –g • Load the executable into gdb: gdbgdbTest

  9. Inside GDB: Running a Program To run the program, once you’re inside gdb, type “run” If there are no breakpoints, and no reason for the code to stop (like a segmentation fault), it will run through to the end.

  10. Segmentation Faults If your code has a segmentation fault, and you run it with GDB, it will stop when it hits the segmentation fault. Often times when it stops it will state a line number which is the location the segmentation fault occurred. If not, you can enter the command “where” and it will give you several line numbers; the last is the line in the function where the fault happened.

  11. Inserting Breakpoints If there is no reason for the program to stop running inside GDB, it won’t. You can insert a breakpoint at a line or function in your program to make the code “pause” at that point. Then, you can use the “step” command to progress through the code one line at a time.

  12. Inserting Breakpoints breakequal Breaks at a function called equal break12 Breaks at line 12 breakinsert if(x==2) Breaks when x=2 • Deleting breakpoints: • To delete all breakpoints, type delete • To delete a specific breakpoint, type delete followed by a space and the breakpoint # • To list all breakpoints and their numbers, type info breakpoints

  13. Printing Variable Values in GDB p/c x Prints x as a character p/d x Prints as integer p/f x Prints as float whatis x Tells you x’s data type • Print the current value of a variable at a certain point, i.e. • Can choose to print a variable in a different form (i.e. float, int, char)

  14. Debugging using Printf • Use Printf to describe the values of variables at a specific location • Track the state of your program during execution • “Debugging during execution”

  15. What will happen? int []arr = new int[5]; inti; for(i = 0; i < 7; i++){ scanf(“%d”, &arr[i]); printf(“%d”, arr[i]); }

  16. What we want is … int []arr = new int[5]; inti; for(i = 0; i < 7; i++){ scanf(“%d”, &arr[i]); printf(“%d ”, arr[i]); fflush(stdout); }

  17. Project 1 • Board.c • Backgammon.c • Overview of Game Rules • Header Files

  18. Testing with expected input and output You will be supplied test vectors online (vector = test input file, e.g. test1.in, and expected output file, e.g. test1.out) You can input the test input file into your program automatically using ‘<‘. Let the executable be CC: CC < test1.in This will cause some output to appear. To put all of that output in a file, use ‘>’. For example, to redirect the output to a file my.test1.out CC < test1.in > my.test1.out Then, you can use diff to compare your output to the expected output. This is much better than ‘eye-balling’ your results! diff my.test1.out test1.out THE OUTPUT OF YOUR PROGRAM MUST MATCH THE EXPECTED OUTPUT EXACTLY OR ELSE POINTS WILL BE DEDUCTED!

More Related