an introduction to programming though c++cs101/2019.1/lectures/lecture6.pdf · an introduction to...

28
An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example for CS101 Autumn 2019

Upload: others

Post on 01-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

An Introduction to Programming though C++

Abhiram G. RanadeLecture 6

Ch. 4: A Program Design Example

for CS101 Autumn 2019

Page 2: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

How to write programs• So far, we wrote very simple programs.• Simple programs can be written intuitively.• Even slightly complex programs should be written with

some care and planning.• Your program could be doing very important calculations

– controlling the flight of a plane– decide the amount of radiation to give to a patient

• Important to learn what can be done to ensure that a program works correctly.

Page 3: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Chapter Goal• Develop a slightly more complex program than

what you have seen so far.• Study the “typical program development

strategy”.

Page 4: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

The “typical program development strategy"

• Specification: State what is the input data and what output is required.– Description or real life problems can be ambiguous or incomplete

• Construct test cases.• Think how you would solve the problem using pencil and paper.

– Understand the structure of the manual solution process– Is something repeated? How many times?

• Write the program.– Manual solution process structure should be reflected in the program– Need to decide what variables to use

• Run the test cases.• Debug: what to do if the program does not work..

Page 5: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

The problemThe following series approaches e as n increases:e = 1/0! + 1/1! + 1/2! + … + 1/n!• Write a program which takes n as input and

prints the sum of the series.

Page 6: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

The specification for our problem• Input: an integer n, where n ≥ 0• Output: The sum 1/0! + … + 1/n!

• This specification is obvious, but still is more than the problem statement – We have made explicit that n cannot be a negative number.

• Ask yourself: can something be misunderstood in this? Can it be made clearer?

• Possible careless belief: n = number of terms to be added together. • The number of terms being added together = n+1. • The number of additions = n.

Page 7: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Constructing test casesPick some input values, and calculate required output values.• For n=0, required output = 1/0! = 1• For n=1, required output = 1/0!+1/1! = 2• For n=2, required output = 1/0!+1/1!+1/2! = 2.5• For large n, required required = close to 2.718281828, known value of e

Test cases will be needed after you finish writing the program

Constructing them early helps ensure you understand what is needed

Page 8: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

What we discussed• First steps of program development process– Understand the specification– Construct test cases

• Next: understand how you solve manually!

Page 9: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

How do you sum the series manually?• Step 0: Calculate the zeroth term, 1/0!, which is just 1.• Step 1: Calculate the first term, 1/1! which is just 1. Add to zeroth.• Step 2: Calculate the second term, 1/2!, add to sum so far.• Step 3: Calculate the third term 1/3!, add the sum so far. And so on.Will you calculate each term independently?• You will calculate third term by dividing the second term by 3

1/3! = 1/2! / 3• The general pattern for step i:

– Calculate ith term, 1/i! = i-1th term 1/(i-1)! / i. Add to sum.• Pattern applicable to steps 1..n. No previous term for step 0

Page 10: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Going from manual computation towards a program

• The general pattern for step i:– Calculate ith term, 1/i! = i-1th term 1/(i-1)! / i. Add to sum.

• Pattern applicable to steps 1..n. No i-1th term for step 0Overall program structure:• Step 0: perform separately.• Steps 1 to n: put in a repeat loop.What variables to use?What do we need to have with us at the beginning of iteration i?• Sum calculated in previous iteration• Term calculated in previous iteration• Value of i, since we divide previous term by i to get current termSo we need 3 variables: S, T, I.

Page 11: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Developing the programmain_program{int n; cin >> n;double I, T, S;I=1; T=1; S=1; //Implement plan for iteration 1repeat(n){ // Plan for iteration i=1..n

// I=i, T=1/(i-1)!, S=1/0!+..+1/(i-1)!T = T/I; // Now T = 1/i!S = S + T; // Now S = 1/0!+..+1/i!I = I + 1; // Now I = i+1

// Need I=i+1,T=1/i!,S=1/0!+..+1/i!}cout <<S<< endl;

}

Page 12: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Remarks• The program has comments about the plan being

followed.– Very essential to explain this

• The order in which we updated T,S,I is important.– Result will be different if you change the order– “Do we want to divide T by the old value of I or the

new value?”– ”Do we want to add new T to S or old T?”

Page 13: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Remark continued• There is a way to update values without worrying about the order of updating:• Write the loop body asdouble newI = I+1;double newT = T/I;double newS = S+T;I = newI;T = newT;S = newS;• With this, all new values will be based on old values.• The order of the red/green statements does not matter; so long as all red

statements appear before all red statements.

Page 14: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Next step: execute the program• Type the program into a file or the IDE• Compile it• Run.– Use the test cases constructed earlier.

• Demo

Page 15: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

What we discussedTranslating manual algorithms into computer programs• Figure out structure of manual algorithm

– Will tell you how many iterations• Identify what you need to remember at different points in

time in the manual algorithm– Will tell you what variables you need.

Most important activity in the course• Next: how to recover if you made a mistake.!

Page 16: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Suppose you made a mistake..main_program{int n; cin >> n;double I, T, S;I=1; T=1; S=1; //Implement plan for iteration 1repeat(n){ // Plan for iteration i=1..n

// I=i, T=1/(i-1)!, S=1/0!+..+1/(i-1)!S = S + T; T = T/I; // Updates to S, T are exchangedI = I + 1;

// Need I=i+1,T=1/i!,S=1/0!+..+1/i!}cout <<S<< endl;

}

Page 17: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Now you run and find..• Demo

Program made a mistake!

Page 18: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

How do you try to recover?• Put print statements to see whether

intermediate values are correct.• Manually “trace” the program execution.• Symbolically trace the program execution.

Page 19: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Putting print statementsmain_program{int n; cin >> n;double I, T, S;I=1; T=1; S=1; repeat(n){ // Plan for iteration i=1..n

// I=i, T=1/(i-1)!, S=1/0!+..+1/(i-1)!cout <<“I: “<<I<<“ T: “<<T<<“ S: “<<S<<endl;S = S + T; T = T/I; // Updates to S, T are exchangedI = I + 1;

// Need I=i+1,T=1/i!,S=1/0!+..+1/i!}cout <<S<< endl;

}

Page 20: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Manual program tracingmain_program{int n; cin >> n;double I, T, S;I=1; T=1; S=1; repeat(n){S = S + T; T = T/I; I = I + 1;

}cout <<S<< endl;

}

Manually execute program.• Keep a table with one column per

variable.• Keep a row per execution step• Whenever the value of a variable

changes, make an entry in the row for current step, column of variable

Check whether the values are as you expectMany experienced programmers prefer this to putting print statements

Page 21: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Symbolic TracingSymbolically check whether the variable values are consistent with the plan.Check 1: Check if values are consistent for the first iteration.• I, T, S will be 1, 1, 1 respectively, and this is as per plan.Check 2: Check for other iterations:• Assume the values are as per plan on entry on ith iteration.• Symbolically determine how values change.• Are they as needed at the end of the iteration?• If yes, we have proved that they will be correct at the beginning of the

next iteration.So proved because of mathematical induction!

Page 22: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Symbolic Tracingmain_program{int n; cin >> n;double I, T, S;I=1; T=1; S=1; repeat(n){ // Plan for iteration i=1..n

// I=i, T=1/(i-1)!, S=1/0!+..+1/(i-1)!S = S + T; // What is the new value of S?T = T/I; I = I + 1;

// Need I=i+1,T=1/i!,S=1/0!+..+1/i!}cout <<S<< endl;

}// new S = 1/0! + ..1/(i-1)! + 1/(i-1)! Hence wrong!

Page 23: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Concluding Remarks• Write down specifications clearly– Many programmers start writing code and only

later discover they misunderstood what to do• Constructing test cases forces you to

understand specification– Useful also when the program is ready to be

tested.

Page 24: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Concluding remarks contdGood program design strategy: mimic manual computationUnderstand clearly what you do manually• Are there similar phases? How many?• State in general terms what each phase accomplishes. “At the beginning

of phase i, ... At end of phase i ...”• Values at end of phase i = values at beginning of phase i+1• Phases typically become iterations of repeat statement• You may number phases conveniently, start from 0 or 1..• What you need to have at beginning of phase: store in variables• The plan determines how you should initialize the variables and update

them in the iteration.

Page 25: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Concluding remarks contdMany, many programs have a standard structure: You have a set of variables and you want them to assume specific values at beginning of iteration i• State what is desired by putting comments in the code.• “What is desired” – called invariant for the loop• Ensure the invariant in first iteration by initializing variables correctly

before the loop.• Assume that the invariant is correct at the beginning of each iteration• The code in the iteration should construct the values you need next from

the values currently in the variables, i.e. to ensure invariant for next iteration.

• Make sure you update variables in correct order.

Page 26: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Concluding Remarks contdIf your program does not work correctly you can• Put print statements to check if variables have the values

you expect in each iteration• Trace through the code/execute it by hand to see if the plan

is followed• Symbolically execute the code to see if the plan is followed.If your program is not following the plan: change the programIf your program is following the plan: change the plan

Page 27: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

Exercise: fill in the blanks as per the plan given in the comments

main_program{int n; cin >> n;double i=…, term = …, result = …;repeat(n){// On tth entry, t=1..n

// i=t-1, term=1/t!// result = 1/0!+..+1/(t-1)!...

}cout << result << endl;

}

Page 28: An Introduction to Programming though C++cs101/2019.1/lectures/Lecture6.pdf · An Introduction to Programming though C++ Abhiram G. Ranade Lecture 6 Ch. 4: A Program Design Example

End of this lecture sequence• Try other exercises given at the end of chapter

4 of book.

!!