es 314 advanced programming instructors: b. ravikumar j. agrawal a.kojoory department of engg...

7
ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

Upload: scot-anderson

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

ES 314 Advanced Programming

Instructors:

B. RavikumarJ. AgrawalA.Kojoory

Department of Engg Science

Page 2: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.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

Page 3: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

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

Page 4: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

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?

Page 5: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

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

Page 6: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

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.

Page 7: ES 314 Advanced Programming Instructors: B. Ravikumar J. Agrawal A.Kojoory Department of Engg Science

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;