Title: ICOM 4015 Advanced Programming
1ICOM 4015 Advanced Programming
- Lecture 0
- Review of Programming I
- Reading LNN Chapters 1-3,5-6
2Review of Programming I Lecture Outline
- Course Information
- C Basics
- Writing modular programs
- Separate Compilation
- Definitions and Summary of Concepts
3Course Information
- Favor leer el prontuario HOY!
- Asistencia requerida
- Correo electronico requerido
- asumimos mensaje recibido en 48 horas
- Laboratorios requeridos
- Evaluacion
- 36 Examen Final
- 34 Examenes parciales (3)
- 20 Programas
- 10 Laboratorio
- Website del curso
- http//www.ece.uprm.edu/bvelez/courses/Fall2001/I
com4015/icom4015.htm
4Roots of 2x2 3x 4A monolithic program(A
typical INGE3016 approach)
include ltcmathgt include ltiostreamgt int main()
float a 1.0 float b 4.0
float c 4.0 float d b b - 4.0 a
c float root1 (-b sqrt(d)) / (2.0 a)
float root2 (-b - sqrt(d)) / (2.0 a)
cout ltlt root1 ltlt root1 ltlt endl cout ltlt
root2 ltlt root2 ltlt endl return 0
Potential Runtime error
roots.cc
gt gcc roots1.cc -o roots1 gt roots1 root1
2.0 root2 ?? gt
SHELL
monolithic gt low modularity gt low reusability
5Roots of 2x2 3x 4A monolithic program
Suggest improvements to our design
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
6Roots of ax2 bx cModular design using
functions
include ltcmathgt include roots.h // roots(a,
b, c, r1, r2) - returns the number of // real
roots of ax2 bx c. If two roots exists //
they are returned is r1 and r2. If only one
root // exists, it is returned in r1. Otherwise
the value // of r1 and r2 is undetermined. int
roots(float a, float b, float c,
float r1, float r2) float d b b - 4.0
a c if (d lt 0) return 0
r1 (-b sqrt(d)) / (2.0 a) if (d
0) return 1 r2 (-b -
sqrt(d)) / (2.0 a) return 2
roots.cc
7Roots of ax2 bx cModular design using
functions
// rootsUI.cc // Simple text-based interactive
user interface for // finding the roots of
polynomials include ltiostreamgt void
readCoefficients(float a, float b, float c)
cout ltlt "Enter coefficient for cuadratic term
(a) " cin gtgt a cout ltlt "Enter coefficient
for linear term (b) " cin gtgt b cout ltlt
"Enter coefficient for constant term (c) "
cin gtgt c void reportRoots(float a, float b,
float c, int numRoots, float
root1, float root2) cout ltlt "a " ltlt a
ltlt " b " ltlt b ltlt " c " ltlt c ltlt
" " switch (numRoots) case 0 cout
ltlt "No real roots" ltlt endl break
case 1 cout ltlt "One real root "
ltlt root1 ltlt endl break case 2
cout ltlt "Two real roots " ltlt
root1 ltlt " and " ltlt root2
ltlt endl break default cout ltlt
"Error bad number of roots" ltlt endl
rootsUI.cc
8Roots of ax2 bx cModular design using
functions
// rootsUI.h // Header File // Simple text-based
interactive user interface for // finding the
roots of polynomials extern void
readCoefficients(float a, float b, float
c) extern void reportRoots(float a, float b,
float c, int numRoots,
float root1, float root2)
rootsUI.h
// roots(a, b, c, r1, r2) - returns the number of
// real roots of ax2 bx c. If two roots
exists // they are returned is r1 and r2. If only
one root // exists, it is returned in r1.
Otherwise the value // of r1 and r2 is
undetermined. extern int roots(float a, float b,
float c, float r1, float r2)
roots.h
Header files provide linkage information
// rootsMain.cc // Main module for finding the
roots of polynomials include "roots.h" include
"rootsUI.h" int main() float a,b,c
readCoefficients(a, b, c) float root1, root2
int numRoots roots(a, b, c, root1, root2)
reportRoots(a, b, c, numRoots, root1, root2)
rootsMain.cc
Main module glues everything together Keep it
simple!
9Complete Layered Application
MAIN rootsMain.cc
rootsUI.h
roots.h
10Separate Compilation
source files
roots.cc
rootsUI.cc
rootsMain.cc
compile
compile
compile
compile time
object files
roots.o
rootsUI.o
rootsMain.o
compile
link time
executable files
roots
run time
11Layered Software Design
GUI
Text-based Batch Interface
Text-based Interactive Interface
Appl Programming Interface (API)
Library/Component
12An Alternative Use of roots FunctionFinding the
roots of polynomials with integer coefficients in
0, 4)
include ltiostreamgt include roots.h int
main() for (float a 0 a lt 4 a)
for (float b 0 b lt 4 b) for
(float c 0 c lt 4 c) float root1
0.0 float root2 0.0 int
rootNo roots(a, b, c, root1, root2)
switch (rootNo) case 0 cout ltlt No
real roots break
case 1 cout ltlt One real root
ltlt root1 ltlt
endl break case
2 cout ltlt Two real roots
ltlt root1 ltlt and
ltlt root2
ltlt endl break
default cout ltlt Ërror bad number of
roots ltlt endl
rootsMany.cc
13Example 3Factorization of an integer
include ltiostreamgt int factors(int number)
int factors 0 int quotient number //
outer loop computes next quotient while
(quotient gt 1) // inner loop finds next
factor for (int factor 2 factor lt
quotient factor) if (quotient factor
0) cout ltlt Next factor is ltlt
factor ltlt endl factors
break quotient / factor
return factors
A Brute Force Algorithm Why?
Challenge Can we find a faster algorithm?
14Definitions andSummary of Concepts I
- Monolithic code
- Very few reusable components
- Modular code
- Lots of reusable pieces or modules
- Modules can be code segments, functions, files,
classes, etc. - Self-documenting code
- Easier to read, understand and maintain
- Techniques include
- meaningful indentation/alignment
- meaningful semantic naming
- concise comments when needed
- simplicity of expressions
- Abstraction Hides irrelevant detail
- Via naming (e.g. named constants)
- Via parameterization (e.g. functions)
- Tool for controlling complexity of software
- Hard-coding
- The opposite of parameterization
- Parameter values explicitly specified in code
15Definitions andSummary of Concepts II
- Scope rules
- Local declarations take precedence over global
ones - Less need to worry about name collisions
- Facilitate modularity
- Separate compilation (C version)
- A program can consists of several separate file
modules - Compiler links the modules together
- Modules import declarations of other modules via
include of header files containing declarations - File modules provide another unit of modularity
- Library
- Reusable object file providing definitions of
general purpose constants, variables, functions,
types, classes and other objects.
16Definitions andSummary of Concepts III
- Compile-time error
- Detected by compiler
- Examples
- Syntax error (missing )
- Undefined identifiers
- Runtime error
- Undetectable (in general) by compiler
- Examples
- Array bounds violation
- Division by zero
- Square root of a negative
- Software development cycle
- Gather/Specify requirements
- Design
- Code
- Test
- Maintain