160 likes | 183 Views
Review of programming concepts in C++ focusing on writing modular programs, separate compilation, and function contracts. Includes a monolithic program example and suggestions for improvements.
E N D
ICOM 4015 Advanced Programming Lecture 0 Review of Programming I Reading: LNN Chapters 1-3,5-6 ICOM 4015 - Lecture 0
Review of Programming I Lecture Outline • Course Information • C++ Basics • Writing modular programs • Separate Compilation • Definitions and Summary of Concepts ICOM 4015 - Lecture 0
Course 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/Icom4015/icom4015.htm ICOM 4015 - Lecture 0
Hard-coded values user interface (UI) mixed up with main computation Roots of 2x2 + 3x + 4A monolithic program(A typical INGE3016 approach) #include <cmath> #include <iostream> 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 << “root1 = ” << root1 << endl; cout << “root2 = ” << root2 << endl; return 0; } Potential Runtime error roots.cc > gcc roots1.cc -o roots1 > roots1 root1 = 2.0 root2 = ?? > SHELL monolithic => low modularity => low reusability ICOM 4015 - Lecture 0
Roots of 2x2 + 3x + 4A monolithic program Suggest improvements to our design: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. ICOM 4015 - Lecture 0
function contract function parameters local declaration Roots of ax2 + bx + cModular design using functions #include <cmath> #include “roots.h” // roots(a, b, c, r1, r2) - returns the number of // real roots of ax^2 + 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 < 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 ICOM 4015 - Lecture 0
Roots of ax2 + bx + cModular design using functions // rootsUI.cc // Simple text-based interactive user interface for // finding the roots of polynomials #include <iostream> void readCoefficients(float& a, float& b, float& c) { cout << "Enter coefficient for cuadratic term (a): "; cin >> a; cout << "Enter coefficient for linear term (b): "; cin >> b; cout << "Enter coefficient for constant term (c): "; cin >> c; } void reportRoots(float a, float b, float c, int numRoots, float root1, float root2) { cout << "a = " << a << " b = " << b << " c = " << c << ":: "; switch (numRoots) { case 0: cout << "No real roots" << endl; break; case 1: cout << "One real root: " << root1 << endl; break; case 2: cout << "Two real roots: " << root1 << " and " << root2 << endl; break; default: cout << "Error: bad number of roots" << endl; } } rootsUI.cc ICOM 4015 - Lecture 0
Roots 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 ax^2 + 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! ICOM 4015 - Lecture 0
API USER INTERFACE rootsUI.cc API POLY ROOTS COMPONENT roots.cc Complete Layered Application MAIN rootsMain.cc roots.h rootsUI.h ICOM 4015 - Lecture 0
Separate 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 ICOM 4015 - Lecture 0
Layered Software Design GUI Text-based Batch Interface Text-based Interactive Interface Appl Programming Interface (API) Library/Component ICOM 4015 - Lecture 0
An Alternative Use of roots FunctionFinding the roots of polynomials with integer coefficients in [0, 4) #include <iostream> #include “roots.h” int main() { for (float a = 0; a < 4; a++) { for (float b = 0; b < 4; b++) { for (float c = 0; c < 4; c++) { float root1 = 0.0; float root2 = 0.0; int rootNo = roots(a, b, c, root1, root2) switch (rootNo) { case 0: cout << “No real roots” break; case 1: cout << “One real root: “ << root1 << endl; break; case 2: cout << “Two real roots: “ << root1 << “ and “ << root2 << endl; break; default: cout << “Ërror: bad number of roots” << endl; } } } } } rootsMany.cc ICOM 4015 - Lecture 0
Example 3Factorization of an integer #include <iostream> int factors(int number) { int factors = 0; int quotient = number; // outer loop computes next quotient while (quotient > 1) { // inner loop finds next factor for (int factor = 2; factor <= quotient; factor++) { if (quotient % factor == 0) { cout << “Next factor is: “ << factor << endl; factors++; break; } } quotient /= factor; } return factors; } A Brute Force Algorithm: Why? Challenge: Can we find a faster algorithm? ICOM 4015 - Lecture 0
Definitions 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 ICOM 4015 - Lecture 0
Definitions 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. ICOM 4015 - Lecture 0
Definitions 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 ICOM 4015 - Lecture 0