1 / 10

ICOM 4015 Advanced Programming

This lecture covers the concept of procedural arguments and top-down stepwise refinement in programming, using integration functions as examples.

kirkk
Download Presentation

ICOM 4015 Advanced Programming

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. ICOM 4015 Advanced Programming Lecture 5 Procedural Abstraction III Reading: Chapter 3 Prof. Bienvenido Velez ICOM 4015

  2. Administrivia • Program 0 (Due Feb 16) • Office Hours • Lunes y Miércoles 10:30-12:30 • or by appointment • Office T-212 • Email • Make sure your email address is correct and functional • Check your email daily! ICOM 4015

  3. Procedural Abstraction III Outline • Procedural arguments • Top-down stepwise refinement ICOM 4015

  4. IntegrationWithout Procedural Arguments #include <iostream> // Forward definitions double integrateSqr(double a, double b, double n); double integrateCube(double a, double b, double n); int main() { cout << "Integral of x^2 in [0,1] = " << integrateSqr(0.0, 1.0, 10000) << endl; cout << "Integral of x^3 in [0,1] = " << integrateCube(0.0, 1.0, 10000) << endl; } double integrateSqr(double a, double b, double n) { double delta = (b-a) / double(n); double sum = 0.0; for (int i=0; i<n; i++) { float x = a + delta * i; sum += x * x * delta; } return sum; } double integrateCube(double a, double b, double n) { double delta = (b-a) / double(n); double sum = 0.0; for (int i=0; i<n; i++) { float x = a + delta * i; sum += x * x * x * delta; } return sum; } [bvelez@amadeus] ~/icom4015/lec05 >> example2 Integral of x^2 in [0,1] = 0.333283 Integral of x^3 in [0,1] = 0.24995 [bvelez@amadeus] ~/icom4015/lec05 >> ICOM 4015

  5. Example 3 IntegrationWith Procedural Arguments #include <iostream> // Forward definitions double integrate(double a, double b, double n, double f(double x)); double cube(double x); double sqr(double x); int main() { cout << "Integral of x^2 in [0,1] = " << integrate(0.0, 1.0, 10000, sqr) << endl; cout << "Integral of x^3 in [0,1] = " << integrate(0.0, 1.0, 10000, cube) << endl; } double integrate(double a, double b, double n, double f(double x)) { double delta = (b-a) / double(n); double sum = 0.0; for (int i=0; i<n; i++) { sum += f(a + delta * i) * delta; } return sum; } double cube(double x) { return x * x * x; } double sqr(double x) { return x * x; } [bvelez@amadeus] ~/icom4015/lec05 >> example2 Integral of x^2 in [0,1] = 0.333283 Integral of x^3 in [0,1] = 0.24995 [bvelez@amadeus] ~/icom4015/lec05 >> ICOM 4015

  6. Step 0 - Outline // top-down.cc // Computes weighted average score of grades. Grades // include two assignments two midterm exams and one final exam. // All grades are input from standard input, but the weights of // each type of grade are hard coded. // C header files extern "C" { } // Standard C++ header files #include <iostream> // My own C++ header files // Macro definitions // Forward definitions of auxiliary functions // Global declarations // Main function int main() { // Read assignment grades // Read exam grades // Read final exam grade // Calculate average // Print report return 0; } // Auxiliary functions ICOM 4015

  7. Step 1 – Code + Stubs int main() { float assignment1, assignment2; float exam1, exam2; float finalExam; readAssignmentGrades(assignment1, assignment2); readExamGrades(exam1, exam2); readFinalGrade(finalExam); float avg; avg = calculateAverage(assignment1, assignment2, exam1, exam2, finalExam); printReport(assignment1, assignment2, exam1, exam2, finalExam, avg); return 0; } // Auxiliary functions void readAssignmentGrades(float& assignment1, float& assignment2) {} void readExamGrades(float& ex1, float& ex2) {} void readFinalGrade(float& final) {} float calculateAverage(float assignment1, float assignment2, float exam1, float exam2, float finalExam) {} void printReport(float assignment1, float assignment2, float exam1, float exam2, float finalExam, float average) {} ICOM 4015

  8. Step 2 - Refine // Auxiliary functions void readAssignmentGrades(float& assignment1, float& assignment2) { // Read a float in [0,100] into assignment1 // Read a float in [0,100] into assignment2 } void readExamGrades(float& ex1, float& ex2) { // Read a float in [0,100] into ex1 // Read a float in [0,100] into ex2 } void readFinalGrade(float& final) { // Read a float in [0,100] into final } float calculateAverage(float assignment1, float assignment2, float exam1, float exam2, float finalExam) { // Calculate assignments average // Calculate exams average // Calculate weighted average } void printReport(float assignment1, float assignment2, float exam1, float exam2, float finalExam, float average) { // print assignment grades // print exam grades // print final exam grades // print weighted average } ICOM 4015

  9. Top-down stepwise refinement cycle outline refine code + stubs ICOM 4015

  10. Summary of Concepts • Procedural arguments • Allow abstraction over processes and functions • Top-Down design / stepwise refinement • A cyclic development technique • Each cycle adds a level of detail to the code • We have a functioning (although incomplete) program after every iteration of the process ICOM 4015

More Related