Presentation is loading. Please wait.

Presentation is loading. Please wait.

FALL 2001ICOM 4015 - Lecture 01 ICOM 4015 Advanced Programming Lecture 0 Review of Programming I Reading: LNN Chapters 1-3,5-6.

Similar presentations


Presentation on theme: "FALL 2001ICOM 4015 - Lecture 01 ICOM 4015 Advanced Programming Lecture 0 Review of Programming I Reading: LNN Chapters 1-3,5-6."— Presentation transcript:

1

2 FALL 2001ICOM 4015 - Lecture 01 ICOM 4015 Advanced Programming Lecture 0 Review of Programming I Reading: LNN Chapters 1-3,5-6

3 FALL 2001ICOM 4015 - Lecture 02 Review of Programming I Lecture Outline Course Information C++ Basics Writing modular programs Separate Compilation Definitions and Summary of Concepts

4 FALL 2001ICOM 4015 - Lecture 03 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

5 FALL 2001ICOM 4015 - Lecture 04 Roots of 2x 2 + 3x + 4 A monolithic program (A typical INGE3016 approach) > gcc roots1.cc -o roots1 > roots1 root1 = 2.0 root2 = ?? > Hard-coded values user interface (UI) mixed up with main computation monolithic => low modularity => low reusability Potential Runtime error #include 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; } roots.cc SHELL

6 FALL 2001ICOM 4015 - Lecture 05 Roots of 2x 2 + 3x + 4 A monolithic program 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Suggest improvements to our design:

7 FALL 2001ICOM 4015 - Lecture 06 function contract function parameters local declaration Roots of ax 2 + bx + c Modular design using functions #include #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

8 FALL 2001ICOM 4015 - Lecture 07 Roots of ax 2 + bx + c Modular design using functions // rootsUI.cc // Simple text-based interactive user interface for // finding the roots of polynomials #include 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

9 FALL 2001ICOM 4015 - Lecture 08 // 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); } // 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); // 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 of ax 2 + bx + c Modular design using functions rootsMain.cc roots.h rootsUI.h Main module glues everything together Keep it simple! Header files provide linkage information

10 FALL 2001ICOM 4015 - Lecture 09 Complete Layered Application MAIN rootsMain.cc POLY ROOTS COMPONENT roots.cc API USER INTERFACE rootsUI.cc API rootsUI.hroots.h

11 FALL 2001ICOM 4015 - Lecture 010 Separate Compilation object files source files compile roots roots.cc compile roots.o rootsUI.cc compile rootsUI.o rootsMain.cc compile rootsMain.o link time compile time run time executable files

12 FALL 2001ICOM 4015 - Lecture 011 Layered Software Design Library/Component Appl Programming Interface (API) GUI Text-based Interactive Interface Text-based Batch Interface

13 FALL 2001ICOM 4015 - Lecture 012 An Alternative Use of roots Function Finding the roots of polynomials with integer coefficients in [0, 4) #include #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

14 FALL 2001ICOM 4015 - Lecture 013 Example 3 Factorization of an integer #include 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; } Challenge: Can we find a faster algorithm? A Brute Force Algorithm: Why?

15 FALL 2001ICOM 4015 - Lecture 014 Definitions and Summary 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

16 FALL 2001ICOM 4015 - Lecture 015 Definitions and Summary 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.

17 FALL 2001ICOM 4015 - Lecture 016 Definitions and Summary 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


Download ppt "FALL 2001ICOM 4015 - Lecture 01 ICOM 4015 Advanced Programming Lecture 0 Review of Programming I Reading: LNN Chapters 1-3,5-6."

Similar presentations


Ads by Google