Title: Todays Agenda
1Todays Agenda
- Implementation Issues
- Modularity and Reuse
- Interfaces and Implementations (Review) ?
- Functions as Modules ?
- Use of Types (for Re-use)
2Types for Re-use 1
- Types classify (data) values
- So, each type is a set of values
- e.g. int m -2n-1 lt m lt 2n-1
- e.g. char c c is an ASCII value
- int and char are basic (primitive) types
- Arrays are structured (non-primitive) types
3Types for Re-use 2
- Users can define types
- Specify a (new) name
- Specify a set of values (by using other types)
- e.g. Celsius int
- e.g. WeeklyTemps Celsius7
4Types for Re-use 3
- C Syntax for user-defined types
- typedef int Celsius
- typedef Celsius WeeklyTemps7
- typdef int Matrix100100
- Matrix M ? Type declaration for Matrix type!
- Mi contains an int
5Types for Re-use 4
- Consider example Ordered Search
- / Pre-condition
- N is the size of A.
- A is sorted.
- Post-condition
- return P if AP x return 1
otherwise. - /
- extern int OrderedSearch(int x, int A, int N)
- // What if I wanted to search for a char. ?
6Types for Re-use 5
- Pre-conditions and Post-condition do not insist
on int type. - Algorithm for Binary Search do not require values
to be of int type. - Only the interface declaration and the function
definition refer to int type. - Even the implementation will work with
appropriate comparisons ( and order)
7Types for Re-use 6
- Can we re-write the code?
- What is the ideal requirement?
- Any type should be acceptable as long there
- is an order function and an appropriate
operation available.
8Types for Re-use 7
- Ideal Implementation
- / Pre-condition
- N is the size of A A is sorted
- Element type is orderable
-
- /
- extern int
- OrderedSearch(Element x, Element A, int N)
9Types for Re-use 8
- How do we obtain the ideal implementation?
- Define the specific type to be used
- / file element.h /
- typedef int Element
- Use the definition by including the header file
- / file OrderedSearch.h /
- include element.h
10Types for Re-use 9
- / file OrderedSearch.h /
- include element.h
- / Pre-condition
- N is the size of A A is sorted.
- Element type is orderable
- Post-condition
- return P if AP x return 1
otherwise. - /
- extern int
- OrderedSearch(Element x, Element A, int
N)
11Types for Re-use 10
- / file order.h /
- include element.h
- / Check whether u and v are in order.
- Post-condition return 1 if u and v are in
order - 0
otherwise - /
- extern int ordered(Element u, Element v)
12Types for Re-use 11
- /file intOrderND.c /
- include element.h
- / Check whether (int)u and (int)v are in order
/ - / Specialized Pre-condition
- u must be of type int
- v must be of type int
- /
- int ordered(Element u, Element v) return u lt v
- // Generate linkable gcc c intOrderND.c
13Types for Re-use 12
- / file bsearch.c /
- include order.h
-
- int OrderedSearch(Element x, Element A, int N)
-
- while (low lt high)
-
- else if (ordered(Amid, x))
-
-
-
14Types for Re-use 13
- Compile and Run
- Step1 Compile bsearch.c to create bsearch.o
- gcc c bsearch.c
- Step2 Create exectutable for bsearch application
- gcc o bsearchexe main.c bsearch.o
intOrderND.o - Step3 Execute the program
- ./bsearch_nd
15Personal Process for Programming
- Design ?
- Development
- Implementation Issues Modularity and Re-use
- Testing
16Modularity and Re-use (in C)
- Interfaces and Implementations
- Files in C
- Functions for Re-use
- Types for Re-use
17Types for Re-use (Review)
- User-defined Types in C
- Interfaces and User-defined Types
- Implementations and User-defined Types
- Re-use
- Disadvantage(s)
18Types for Re-use - Definition
- / file element.h /
- typedef int Element
19Types for Re-use - Interfaces
- / file osearch.h /
- include element.h
/ Pre-condition N is the size of A
A is sorted by an order defined on
Element. Post-condition
return P if AP x return 1 otherwise. /
extern int OrderedSearch(Element x,
Element A, int N)
20Types for Re-use - Interfaces
- / file order.h /
- include element.h
/ Check whether u and v are in order.
Post-condition return 1 if u and j are in
order 0
otherwise /
extern int ordered(Element u, Element v)
21Types for Re-use - Implementations
include order.h int OrderedSearch(Element x,
Element A, int N)
while (low lt high) else if
(ordered(Amid, x))
// Generate linkable gcc c bsearch.c
22Types for Re-use - Implementations
/file intOrderND.c /
include element.h
/ Check whether (int)u and (int)v are in order
/ / Specialized Pre-condition u must be of
type int v must be of type int /
int ordered(Element u, Element v) return u lt v
// Generate linkable gcc c intOrderND.c
23Types for Re-use - Usage
/ file main.c /
include OrderedSearch.h
int main()
Element x Element A100 int N int pos
// read x, A and N.
pos OrderedSearch(x, A, N)
24Types for Re-use Usage
- int temp, j
- // read x
- scanf(d, temp) x (Element) temp
// read N scanf(d, N)
// read A for (j0 j lt N j)
scanf(d, temp) Aj (Element) temp
25Types for Re-use - Execution
- Compile
- gcc main.c bsearch.o intOrderND.o o bsnd
- Execute
- ./bsnd
26Types for Re-use - Re-definition
- / file element.h /
- typedef char Element
27Types for Re-use - Re-Implementation
- / file charOrderLex.c /
- int ordered(Element c, Element d)
- char i (char) c
- char j (char) d
- if (isalpha(i) isalpha(j))
-
- else if
-
28Types for Reuse - Reuse
char temp, j // read x scanf(c, temp) x
(Element) temp
// read N scanf(d, N)
// read A for (j0 j lt N j) scanf(
c, temp) Aj (Element) temp
29Types for Re-use - Execution
- Compile
- gcc main.c bsearch.o charOrderLex.o o bstc
- Execute
- ./bstc
30Types for Re-use - Disadvantages
- Need to use same .h file for types i.e.
multiple copies of other files needed. - Type conversions needed (from Element to char and
vice-versa) - Approach complex for non-primitive types
- (say string instead of int or char)