1 / 18

Today’s Agenda

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:

keziah
Télécharger la présentation

Today’s Agenda

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. Today’s Agenda Implementation Issues Modularity – Files and Functions Reuse Function Definitions Separate Compilation Sundar B. BITS, Pilani.

  2. Process • 3-step process: • Design  • Develop • Test Sundar B. BITS, Pilani.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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;

  8. 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.

  9. 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

  10. 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.

  11. 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);

  12. 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; }

  13. 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.

  14. 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.

  15. 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

  16. 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.

  17. 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.

  18. 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.

More Related