170 likes | 293 Vues
This document covers crucial concepts in coding, specifically focusing on functional decomposition, pointers vs. arrays, and unit testing. It outlines essential practices such as declaring functions, debugging techniques, and compiling multiple modules. The advantages of functional decomposition are emphasized, illustrating its role in code reuse and easier debugging. The importance of creating effective test vectors to compare expected and actual outputs is also discussed. This guide aims to enhance understanding and skills in programming for both beginners and advanced learners.
E N D
Test on Monday!! • Email me BEFORE the weekend if you have questions. • Project 2 will be going out next Wednesday. • We’ll review it in class next week. Announcements
Grades are posted using secret numbers • Grading was based on: • Writing all 3 functions • Correctly using the variables specified in function headers • Declaring necessary local variables • Correctly copying code segments Quizzes
Reasons for functional decomposition: • Divide and conquer! • Easier to debug since the individual pieces can be tested. • Goals when decomposing: • Enable code reuse • Hierarchical decomposition- put functions in functions Functional Decomposition
Pre-declaration of a function: • Takes place in a header file or above “main” • Ex. intfunction_name(int,int); • Calling the function inside of another function: • Omit the variable types and insert the variable to be passed in. • Ex. a=function_name(b,c); • Writing a function: • Can be written in a separate file or underneath main. • Include return and input variable types and names. • Ex. intfunction_name(int first, int second) { //Function code return 0; } Functional Decomposition
Compiling multiple modules: • The files should be linked by a common header • Ex. #include “backgammon.h” • Recommendation: don’t #include other .c files! It makes compiling problematic. • Ex. gccbackgammon.hbackgammon.ccheck.c Compiling tricks
Debugging: • Use gcc –g file_name.c • Making a binary: • Use gcc –c file_name.c • Compiling and renaming: • Use gcc –o new_file_name.outold_file_name.c Compiling Tricks
Program Input Expected Compare Actual Test Vectors
Create test vectors that test one aspect or the code’s full functionality. • Use “diff” to compare the actual output to the expected output: • Ex: gccprogram.outprogram.c program.out < test1.in > test.out diff test.out test1.expected.out program.out < test2.in > ! test.out diff test.out test2.expected.out Test Vectors
Driver function Single function Input Expected Compare Actual Unit Tests
Benefits of unit test vectors: • Supports functional decomposition • Allows for incremental test and build • In theory, they can be exhaustive. Unit Tests
Initializing Arrays: • Arrays can be fully initialized once in C (this needs to happen when the array is first declared) • Example • Both pointers an arrays are used to access memory spaces. • The name of an array is a pointer to the first memory space occupied by the array. • Major difference: the address a pointer references can be changed. Pointers vs. Arrays
Initializing: int number, *pointer, **point_2_pointer; • Assigning values: number=5; pointer=&number; point_2_pointer=&pointer; printf(“%i”,**point_2_pointer); printf(“%p”,*point_2_pointer); Brief Pointer review
Math operations with pointers are all based on the size of the variable the pointer is used for. • Example Pointer Arithmetic
Creating a function with a pointer as an input: intpointer_processing(intstep,char *array, int *value) { //Function’s code return 0;} • Creating a function with a pointer as an output: char* pointer_sending(int step, char *array, int *value) { char *array_point; //Function’s code return array_point; } Pointers with functions
Know conversion to and from binary, octal and hexadecimal • Understand the benefits and principles of functional decomposition • Know the differences between pointers and arrays • Practice writing large pieces of code on paper under a time constraint. Tips