es 314 advanced programming instructors: b. ravikumar j. agrawal a.kojoory department of engg...
TRANSCRIPT
ES 314 Advanced Programming
Instructors:
B. RavikumarJ. AgrawalA.Kojoory
Department of Engg Science
2
Prerequisite: CS 115: Programming I.
Co-requisites: MATH 345: Probability Theory and ES 220: Electric Circuits, or consent of instructor.
Programming is central to Engineering
• modeling and simulation are central to engineering • programming is a crucial component of modeling and simulation
3
Course will have two modules
• programming• data structures• features of c++ programming
•Objects and classes•Pointers, recursion etc.
• some fundamental algorithms• sorting• searching
• simulation and modeling
4
Example of modeling and simulation
A soft drink company offers a promotional award. Each can of the soft drink contains a sticker with a number from 1 to 20. Once you have collected all the different stickers, you can mail it and receive a prize.
Assume that the sticker labels are randomly generated integers from 1 to 20, what is the average number of cans one should buy in order to win the prize?
5
What tools do we need to solve this problem?
• entities to be simulated • can, buyer
• can• random number generator
• buyer• keep track of the stickers currently owned, number of cans bought so far
• simulation• stop when all stickers collected
6
Details of the solution
General solution: k stickers {1, 2, …, k}
class buyer:
•Data: stickers – array of size k numCans – number of cans bought
•Functions: buy – will make the buyer buy one more can. Update stickers and numCans
done – check if the buyer owns all the stickers.
7
Overall solution
total = 0;for j = 1 to N { // repeat the simulation N times initialize a buyer A; // stickers[] array is set to all 0; numCans is set to 0; while !(A.done) A.buyCan(); total = total + A.numCans;}
Output total / N;