180 likes | 245 Views
Today’s Agenda. Implementation Issues Modularity – Files and Functions Reuse Function Definitions Separate Compilation. Process. 3-step process: Design Develop Test. Implementation Issues. Program Structure Issue: How to manage complexity? Solution Strategy:
E N D
Today’s Agenda Implementation Issues Modularity – Files and Functions Reuse Function Definitions Separate Compilation Sundar B. BITS, Pilani.
Process • 3-step process: • Design • Develop • Test Sundar B. BITS, Pilani.
Implementation Issues • Program Structure • Issue: How to manage complexity? • Solution Strategy: • Divide into modules • Re-use modules • What are modules? • Each module is a file or a function in C • A file module contains declarations or definitions (of data or functions) Sundar B. BITS, Pilani.
Implementation Issues • Separate files for “what” and “how” • “what” is known as interface • In C, an interface is specified by a function declaration • “how” is known as implementation • In C, an implementation is specified by a function definition • Separation allows different implementations for same interface. Sundar B. BITS, Pilani.
Example – Search - Interface /* File search.h */ /* Find the position of x in list A */ /* Pre-condition: N is the size of A. Post-condition: return P if A[P] = x ; return –1 otherwise. */ extern int search(int x, int A[], int N); Sundar B. BITS, Pilani.
Example – Search - Implementation /* File search.c */ int search(int x, int A[], int N) { int pos; return –1; } // Loop Invariant: A[j] != x for j < pos • for (pos = 0; pos < N; pos++) • if (A[pos] == x) return pos; Sundar B. BITS, Pilani.
Example – Search - Usage /* File main.c */ #include “search.h” int main() { int pos; // Code for reading test values for x, N, and A. } pos = search(x, A, N); if (pos>= 0) printf(“%d found in position %d\n”, x, pos); else printf(“%d not found in list \n”, x); return 0;
Example – Search - Execution • Compile the program: gcc –c search.c –o search.o gcc main.c search.o -o searchtest • Run the program: ./searchtest Sundar B. BITS, Pilani.
Example – Search – Alternate Implementation /* File esearch.c */ int search(int x, int A[], int N) { } for ( --N; N >= 0; N--) { if (A[N] == x) return N; } return N; // Exercise: Prove this is correct! // Loop Invariant: A[j] != x for j > N
Example – Search - Execution • Compile the program: gcc –c esearch.c –o esearch.o gcc main.c esearch.o -o searchtest • Run the program: ./searchtest Sundar B. BITS, Pilani.
Example – Ordered Search - Interface /* File osearch.h */ /* Find the position of x in ordered list A */ /* Pre-condition: N is the size of A. A is sorted in non-decreasing order. Post-condition: return P if A[P] = x ; return –1 otherwise. */ extern int osearch(int x, int A[], int N);
Example – Binary Search - Implementation /* File bsearch.c */ int osearch(int x, int A[], int N) { unsigned int low = 0, high = N – 1, mid; while (low <= high) { // Loop Invariant: (x NOT in A) OR (A[low] <= x <= A[high]) mid = (low + high)/2; if (A[mid] == x) return mid; else if (A[mid] < x) low = mid + 1; else if high = mid – 1; } return –1; }
Binary Search – Usage & Execution • Exercise: • Write a main program that uses ordered search. • Compile the program • Run the program • Test for all test cases identified. Sundar B. BITS, Pilani.
Approach – What? • Separate Implementation from Interface • e.g. search.h and search.c • Separate Implementations from each other • e.g. search.c and esearch.c • Separate Usage from Implementation • e.g. search.c and main.c Sundar B. BITS, Pilani.
Approach – How? Declare Interface (e.g. search.h) Implement (e.g. search.c) Use (e.g. main.c) Implement (e.g. esearch.c) Generate linkable (e.g. esearch.o) Generate linkable (e.g. search.o) Generate executable (e.g. searchtest) Generate executable (e.g. searchtest) Execute Execute
Approach – Why? • Reuse of interface • Different implementors can – independently - support single interface. • Reuse of user code • User can choose implementation without modifying code. • Reuse of implementation • Different users can use same implementation. Sundar B. BITS, Pilani.
Approach - Summary • Use of separate files in C for interfaces, implementations, and uses • Increases modularity and re-use via separate compilation. • C refers to program/header files as “compilation units”. Sundar B. BITS, Pilani.
Exercise • Provide a C interface for computing xy when y is a power of 2. • Provide at least two different implementations for the above interface. • Provide a C interface for computing xy for any non-negative integer y, using the interface in 1. • Provide at least 2 different implementations for the above interface in 3. • Write a C program that uses the interface in 3. • Compile the modules separately and link them together such that you get 4 different executables. Sundar B. BITS, Pilani.