3.12 3.13 hacks and notes
for week 3
- notes
- hacks for 3.12
- 3.13 Homework/Hacks
- 2. Write a procedure that will allow the A+ to get to the 1, while avoiding the black boxes.
- missing hacks (oops)
notes
procedure: a named group of programming instructions that may have parameters and return values.
Parameters: input values of a procedure.
Modularity: the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program
HINT: procedures should be descriptive. also consider what parameters to set and what data is needed for the procedure
Note: There are two types of procedures, one that returns a value or some type of data and on that just executes a block of statements. also remember to keep procedures simple.
make sure to have multiple different functions in a procedure
Think of procedures like functions I guess?
hacks for 3.12
Q 1:
a -- ? b -- ? c -- 9 PROCEDURE find a () { b <-- 9 9
a <-- b c
Print (a) } What is a?
ANS: a would become 891 since the procedure puts b as 99 then sets a to be b*c which is 891 (i may be wrong)
Q 2:
cost ⟵ 173 tax - 10% PROCEDURE applytax (cost, cpercentDiscounted){ temp <-- 100 + percentTaxed temp <-- temp / 100 cost <-- cost x temp Print(cost)}
What is the cost?
ANS: temp = 110/100 so cost = 173 * 1.1 = 190.3 so cost = 190.3
3.13 Homework/Hacks
Q 1: Create a procedure that is meant to replace the top running backs yards per game in one season if the current running back has more yards per game
Necessary Parameters: toprbyardspg(100), currentrbyards(1260), totalGames(12)
ANS below
toprbyardspg = 100
currentrbyards = 1260
totalGames = 12
procedure topyards(toprbyardspg, currentrbyards, totalGames) {
currentrbyards = 1260 / totalGames
if toprbyardspg < currentrbyards:
currentrbyards = toprbyardspg
}
PROCEDURE toEnd() {
move_forward()
rotate_left()
move_forward()
move_forward()
rotate_right()
move_forward()
move_forward()
move_forward()
rotate_left()
move_forward()
move_forward()
rotate_left()
move_forward()
move_forward()
rotate_right()
move_forward()
rotate_left()
move_forward()
move_forward()
# using loops and if statements may work but for me it would be harder and
# take more time
}
Q 3:
Which Is the Correct Way to define the Name of a Procedure?
A. PROCEDURE MYLIST
B. PROCEDURE MyList
C. procedure mylist
ANS: B because procedure is in caps and it's camel cased.
Q 4:
Write A Procedure That gets the BeachBall To the Green Square
PROCEDURE toEnd() {
move_forward()
rotate_left()
move_forward()
rotate_right()
move_forward()
move_forward()
move_forward()
move_forward()
move_forward()
rotate_left()
move_forward()
move_forward()
}
missing hacks (oops)
Problem 1: This problem involves parameters Qais is writing code to calculate formulas from his math class. He's currently working on a procedure to calculate average speed, based on this formula:
Average speed=
Total Time/Total Distance
Highlight which of these is the best procedure for calculating and displaying average speed.
PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (distance/time) }
PROCEDURE calcAvgSpeed (distance) { DISPLAY (distance/time) }
PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (time/distance) }
This is the correct equation, the others have the wrong equation or don't add the extra variables needed.
Problem 2: Procedures with return values
James Hunter is looking through his classmate's program and sees a procedure called heightenEmotions: PROCEDURE heightenEmotions(myEmotion)
{ moreEnergy ← CONCAT(myEmotion, "!!!")
moreVolume ← UPPER(moreEnergy)
RETURN moreVolume }
That procedure manipulates strings using two built-in procedures, CONCAT for concatenating two strings together, and UPPER for converting a string to uppercase.
James Hunter then sees this line of code:
heightenEmotions("im mad")
After that line of code runs, will nothing be displayed?
True
because there is nothing displaying the text
False
Problem 3: Procedures with return values Bubz is writing a program to calculate the carbon footprint of his activities. The procedure calcFlightFootprint calculates the pounds of carbon dioxide produced per passenger in a flight that covers a given number of miles and seats a given number of passengers.
PROCEDURE calcFlightFootprint(numMiles, numPassengers) { CO2_PER_MILE ← 53.29
carbonPerFlight ← numMiles * CO2_PER_MILE
carbonPerPassenger ← carbonPerFlight / numPassengers
RETURN carbonPerPassenger
}
Bubz wants to use that procedure to calculate the total footprint for his two upcoming flights: LA to NY: 2,451 miles and 118 passengers NY to London: 3,442 miles and 252 passengers
Which of these code snippets successfully calculates and stores her total footprint? Highlight 2 answers.
totalFootprint ← calcFlightFootprint(2451, 118) + calcFlightFootprint(3442, 252)
this is adding the 2 flights co2 production seperately
totalFootprint ← calcFlightFootprint(2451, 118 + 3442, 252)
totalFootprint ← calcFlightFootprint((2451, 118) + (3442, 252))
this is summing up the passengers and miles and then calculating the total flight footprint
laNyCarbon ← calcFlightFootprint(2451, 118) nyLondonCarbon ← calcFlightFootprint(3442, 252) totalFootprint ← laNyCarbon + nyLondonCarbon