Docsity

Midterm Exam Preparation: ECS10 Programming Concepts - Prof. Annamaria B. Amenta, Study notes of Computer Science

Toggle sidebar
bg1
1
ECS10ECS10
10/1710/17
MidtermMidterm
Midterm 1 is Monday 20, in classMidterm 1 is Monday 20, in class
Bring a Bring a ScantronScantron 2000 2000 formform
1515----17 multiple choice questions 17 multiple choice questions –– 65 pts ?65 pts ?
Sh rt (8 lin ?) pr r mSh rt (8 lin ?) pr r m
35 pt35 pt
??
Sh
o
rt
(8
lin
es
?)
pr
og
r
a
m
Sh
o
rt
(8
lin
es
?)
pr
og
r
a
m
––
35
pt
s
35
pt
s
??
Open book, open notes. Bring all the notes you Open book, open notes. Bring all the notes you
want. want.
I expect a doctor’s note or equivalent if you I expect a doctor’s note or equivalent if you
have to miss it.have to miss it.
WdsWds Oct 22 is the Oct 22 is the drop deadlinedrop deadline
Programming ProblemProgramming Problem ApproachApproach
First understand what the program is supposed First understand what the program is supposed
to do.to do.
Here, the program is supposed to Here, the program is supposed to
GiGi
iiil fdiiil fd
1.1.
G
et user
i
nput
G
et user
i
nput
––
i
n
i
t
i
a
l
mass, rate o
f
d
ecay
i
n
i
t
i
a
l
mass, rate o
f
d
ecay
2.2. Reduce mass by (rate of decay) percent Reduce mass by (rate of decay) percent
3.3. UnitlUnitl mass is reduced to < 1% of its initial massmass is reduced to < 1% of its initial mass
Heart of the program will be a while loop.Heart of the program will be a while loop.
Start in the middle…on scratch paper. Start in the middle…on scratch paper.
While loopWhile loop
The program will require a while loop. You The program will require a while loop. You
really have to understand how it works. really have to understand how it works.
while Boolean is True:
do block
do
block
The block under the while line is repeated until The block under the while line is repeated until
the Boolean test in the while line becomes False.the Boolean test in the while line becomes False.
Choose variablesChoose variables
initialMassinitialMass -- The mass the user inputs at the The mass the user inputs at the
beginning. Float. beginning. Float.
mass mass –– The mass that decreases every year. The mass that decreases every year.
.
.
rate rate –– The percent decay every year. Float.The percent decay every year. Float.
year year –– The number of years so far. Integer.The number of years so far. Integer.
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam Preparation: ECS10 Programming Concepts - Prof. Annamaria B. Amenta and more Study notes Computer Science in PDF only on Docsity!

ECS10ECS

MidtermMidterm

„„ Midterm 1 is Monday 20, in classMidterm 1 is Monday 20, in class „„ Bring aBring a ScantronScantron 2000 2000 formform „„ 15 15----17 multiple choice questions17 multiple choice questions –– 65 pts ?65 pts? „„„„ Sh rt (8 linSh rt (8 linShort (8 lines?) programShort (8 lines?) program – ?) pr?) pr r mr m – 35 pts35 pt35 pt ?35 pts??? „„ Open book, open notes. Bring all the notes youOpen book, open notes. Bring all the notes you want.want. „„ I expect a doctor’s note or equivalent if youI expect a doctor’s note or equivalent if you have to miss it.have to miss it. „„ WdsWds Oct 22 is theOct 22 is the drop deadlinedrop deadline

Programming ProblemProgramming Problem ApproachApproach

„„ First understand what the program is supposedFirst understand what the program is supposed to do.to do. „„ Here, the program is supposed toHere, the program is supposed to 1.1. GGGet user inputGet user input – ii – initial mass, rate of decayi i i li i i linitial mass, rate of decay f df d 2.2. Reduce mass by (rate of decay) percentReduce mass by (rate of decay) percent 3.3. UnitlUnitl mass is reduced to < 1% of its initial massmass is reduced to < 1% of its initial mass „„ Heart of the program will be a while loop.Heart of the program will be a while loop. „„ Start in the middle…on scratch paper.Start in the middle…on scratch paper.

While loopWhile loop

„„ The program will require a while loop. YouThe program will require a while loop. You really have to understand how it works.really have to understand how it works. while Boolean is True: do blockdo block

„„ The block under the while line is repeated untilThe block under the while line is repeated until the Boolean test in the while line becomes False.the Boolean test in the while line becomes False.

Choose variablesChoose variables

„„ initialMassinitialMass -- The mass the user inputs at theThe mass the user inputs at the beginning. Float.beginning. Float. „„ massmass –– The mass that decreases every year.The mass that decreases every year. FloatFloatFloat.Float. „„ raterate –– The percent decay every year. Float.The percent decay every year. Float. „„ yearyear –– The number of years so far. Integer.The number of years so far. Integer.

Block under the whileBlock under the while

while _________ : mass = mass - mass*rate/100. year = year + 1 print ‘After year’,year, print ‘The mass is’, mass

„„ What should the Boolean condition in the whileWhat should the Boolean condition in the while statement be?statement be?

The Boolean conditionThe Boolean condition

while mass > initialMass0.01: mass = mass - massrate/100. year = year + 1 print ‘After year’,year, print ‘The mass is’, mass

„„ What values should all the variables have beforeWhat values should all the variables have before the loop starts?the loop starts?

Getting user inputGetting user input

reply = raw_input(“Enter initial mass (grams):”) initialMass = float(reply) reply = raw_input(“Enter yearly decay rate in percent: “):percent: ): rate = float(reply)

„„ Does not say that input has to be checked toDoes not say that input has to be checked to avoid crashes; that would be optional.avoid crashes; that would be optional.

Beginning values for otherBeginning values for other

variablesvariables

mass = initialMass year = 0 while mass > initialMass/100: mass = mass - mass * rate / 100 year = year+ print “After year”,year, print “the mass is”,mass

After the loopAfter the loop

print “Years needed to drop below 1% of initialprint “Years needed to drop below 1% of initial\ massmass is”,yearis”,year

„„„„ Comments! Comments! Comments!Comments! Comments! Comments!Comments!Comments! Comments! Comments! Comments! Comments! „„ If you make mistakes in the code, but theIf you make mistakes in the code, but the comments show what you were trying to do, youcomments show what you were trying to do, you might get partial credit.might get partial credit.

Variables and assignmentVariables and assignment

x = 0

„ Variable on left. „ Expression (something which is or computes a data item) on the right.

x = x+ „ Uses old value of x on the right to compute new value of x, which is stored in variable x on left.

random.randrangerandom.randrange

„„ It’s in the book and 10/15 lecture. Test it out inIt’s in the book and 10/15 lecture. Test it out in IDLE.IDLE. random.rangrange(10)

„„ Produces a random number between 0 and 9.Produces a random number between 0 and 9.

Prepare for ifPrepare for if--elifelif--else questionelse question

„„ Review lecture notes from 10/15 and rps.pyReview lecture notes from 10/15 and rps.py program.program. „„ Read pages 61Read pages 61----64 in book and try out all64 in book and try out all examples in IDLEexamples in IDLEexamples in IDLE.examples in IDLE. „„ Programming exercise:Programming exercise: „„ Write a program that gets a random numberWrite a program that gets a random number between 1 and 4, prints it out, and then correctlybetween 1 and 4, prints it out, and then correctly identifies it with the sentence “It is four”, “It isidentifies it with the sentence “It is four”, “It is three”, “It is two”, or “It is one”, using ifthree”, “It is two”, or “It is one”, using if--elifelif--elifelif-- else.else.

Example questionExample question Example questionExample question

Example questionExample question Example questionExample question

Common errorsCommon errors

„„ Use a variable before you put something in it.Use a variable before you put something in it. Causes the program to crash!Causes the program to crash! „„ Value of an expression is not put into a variable.Value of an expression is not put into a variable.

float(reply) balance + (balance*rate) – payment

„„ Does not cause an error, but the value of allDoes not cause an error, but the value of all variables remains the same.variables remains the same.