1 ENGI 2420 Structured Programming (Lab Tutorial 5) Memorial University of Newfoundland
ENGI 2420 – Structured Programming Lab Tutorial 5 2 Assignment 4 -- Style Readability –No or incorrect indentation. –Poor choice of names Too many redundant parentheses Too many parentheses hurt readability sum = ((sum)+((sign)*(power(x,i)/factorial(i))) ; A few redundant parentheses can reinforce precedence rules. This is fine. sum = sum + (sign * power(x,i) * factorial(i)) ; Spacing reinforces precedence rules. Also fine. sum = sum + sign*power(x,i)*factorial(i) ; Unneeded if statements Variables declared before loop rather than in the loop. Comments –No documentation of local variables. –No function header comments
ENGI 2420 – Structured Programming Lab Tutorial 5 3 Assignment 4 -- Correctness Compilation –Failing to declare a function before it is used. Bugs –Summing the wrong number of terms –Failing to initialize variables before they are used. –Make a habit of declaring variables only when they are ready to be initialized. –Incorrect factorial calculation. –Infinite loops.
ENGI 2420 – Structured Programming Lab Tutorial 5 4 Assignment 4 -- Copying Many assignments were copied We will be dealing with these cases The effect was plainly visible on the midterm. Doing the assignments is integral to understanding the material, which (if you need further motivation) is important come exam time.
ENGI 2420 – Structured Programming Lab Tutorial 5 5 Assignment-5 Structure of C++ files - assign5.h: contains the declarations for the functions - assign5.cpp: contains your implemented functions (submit this one via web-submit) - your own test code in a separate.cpp file (do NOT submit your test code) Implement two functions as follows
ENGI 2420 – Structured Programming Lab Tutorial 5 6 Function 1 bool lineParameters(double x1, double y1, double x2, double y2, double& a, double& b) Given the (x,y) coordinates of two points, find the parameters, a and b for the equation of a line, y = a*x + b, going through the two points, if possible (using the slope and y-intercept approach) If the line can't be represented in this form then the function should return false, otherwise it should return true. Must define and call at least two additional functions to i) compute the slope of the line connecting the points and ii) find the y-intercept of the line given its slope and a point on the line.
ENGI 2420 – Structured Programming Lab Tutorial 5 7 Execution Example (I) (0.0, 0.0) and (10.0, 10.0) will return true –and give 1.0*x (3.0, 3.0) and (6.0, 4.0) will return true –and give 0.333*x (3.0,3.0) and (3.0, 4.0) will give a false result –there is no need to assign to the a and b parameters
ENGI 2420 – Structured Programming Lab Tutorial 5 8 Function 2 double outstandingDebt (double principle, double interestRate, double payment, int periods) Returns the amount that will be outstanding on a loan after periods payments of $payment have been made, assuming that the original loan amount was $principle and that the interest rate per period is interestRate, as a decimal (i.e., 0.05 represents a rate of 5%) Interest should be computed once per period and added to the principle before the payment is deducted Use loops to do the calculation iteratively
ENGI 2420 – Structured Programming Lab Tutorial 5 9 Execution Example (II) outstandingDebt (100.00, 0.10, 20.0, 3) Periods passedDebt
ENGI 2420 – Structured Programming Lab Tutorial 5 10 Top-down design & bottom-up testing Top down design –First I pseudo code lineParameters –As I write lineParameters I specify its auxiliary functions. –Then I write the auxiliary functions Bottom up testing –First I test functions that call no others –I only test a function once I am certain all functions it calls are working.