Homework Assignment for 3.12 and 3.13
Completing the Hacks given to us for Unit 3, Section 12 and 13
- This Group: Caleb, Qais, James, and Krishiv
- Notes
- 3.12 Hacks Part 1
- 3.12 Homework/Hacks part 2
- 3.13 Homework/Hacks for Part 1 and Part 2 of the lesson
- You Have Finished All the hacks Just Add a submisson to the comment in our fastpages under homework!
Notes
- A procedure is a named group of programming instructions that may have parameters and return values
- Procedures can be referred to as method or function depending on the programing language.
- A procedure call interrupts an execution of statements and makes the program execute the statements in the procedure.
- Arguments specify the values of the parameters
- If you have a set of statements and there is a procedure, when it reaches that procedure it will execute it and then go back to the rest of the statements
- If you see a return statement you automatically end the procedure even if there is more statements
- If you have a set of statements in a procedure, the statement will go through the top of the procedure to the bottom in order
- 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
- One common type of abstraction is procedural abstraction, which provides a name for a process and allows a procedure to be only knowing what it doe,not how it does it.
- The subdivision of a computer program into separate subprograms called modularity
- To name a procedure, it must be in all capitals meaning PROCEDURE and then the functions name should be lowercase, unless there is a part when a new word starts, so the first letter of the word should be capitalized
Vocabulary
- Procedure: is a named group of programming instructions that serves a purpose
- Parameter: are 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
3.12 Hacks Part 1
Skipped Question 1
Problem 2:
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) }
Explanation: It is the first option because we want to calculate the averages of the distances and the times, and in order to calculate the average speed, we need to do distance over time.
Problem 3: 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
False
Explanation: Nothing will show up because the inside is not an actual function. Even if it was defined as a function, that would only apply to the things outside of the parentheses. The parameter on the inside would not be defined and it does not really exist, but you can make it into DISPLAY("im mad")
Problem 4: 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)
-
totalFootprint ← calcFlightFootprint(2451, 118 + 3442, 252)
totalFootprint ← calcFlightFootprint((2451, 118) + (3442, 252))
4. laNyCarbon ← calcFlightFootprint(2451, 118) nyLondonCarbon ← calcFlightFootprint(3442, 252) totalFootprint ← laNyCarbon + nyLondonCarbon
Explanation: It is 1 and 3 because we can calculate the total footprint individually and then find the total by adding the two values up. Option 3 is similar to this because it runs the function in one but calculates it twice, it is like another way to write it. Option 2 does not work because it will just add them together before starting the function, and Option 4 might not work because the variables aren't being reset, I am not sure, this one was a little confusing for me to find out.
Practice Problem
Here is a practice problem that involves calling a procedure by having an input and an output. This is a apply discount and apply tax procedure.
$80 item receives a 20% discount and a tax is 8%
PROCEDURE applyDiscount (cost, percentDiscounted) { temp <-- 100 - percentDiscounted
temp <-- temp / 100
cost <-- cost x temp
Print(cost)
}
PROCEDURE applytax (cost, cpercentDiscounted) { temp <-- 100 + percentTaxed
temp <-- temp / 100
cost <-- cost x temp
Print(cost)
}
Explanation: I think it would be the first procedure because you should only find the price with tax after it has been discounted
Temperature Procedure
What is 80 degrees Fahrenheit in Celsius
PROCEDURE convert Fahrenheit (temperature) {
Celsius <-- temperature - 32
Celsius <-- Celsius x 5/9
Print (Celsius)
}
Explanation: The answer would be ~26.7 because when you plug it in Celsius is assigned to the value of 48 and then reassigned with a value of ~26.7 and then it is printed out
Problem 1
a -- ? b -- ? c -- 9
PROCEDURE find a ()
{ b <-- 9 9
a <-- b c
Print (a) }
What is a?
cost ⟵ 173 tax - 10%
PROCEDURE applytax (cost, cpercentDiscounted){ temp <-- 100 + percentTaxed temp <-- temp / 100 cost <-- cost x temp Print(cost)}
What is the cost?
Explanation: Well since c has a value of 9 and b has a value of 81 (99), then we can find out a since it is just b c, so it becomes 81 * 9, which assigns 729 to a
Tempature - 103 Degrees PROCEDURE convert Fahrenheit (tempature)
{ Celsius <-- tempature - 32 Celsius <-- Celsius x 5/9
Print (Celsius)}
Explanation: The Answer should be 39.4 degrees celsius because we are just taking 103 and going through the function. We are subtracting 32 from 103 and then multiplying it by 5/9 to get ~39.4
3.13 Homework/Hacks for Part 1 and Part 2 of the lesson
Problem 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)
Click for hint
Refer back to 3.13 Developing Procedures Part 1PROCEDURE CheckTopRB(currentbyyards, totalGames, topbyardspg)
rbAvg = currentrbyards/totalGames
if rbAvg > toprbyardspg {
toprbyardspg = rbAvg
}
print(toprbyardspg)
PROCEDURE moveA+ { # Function that will work
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()
}
PROCEDURE moveNumberOne { # My Guess at an Effcient Function
if(can_moveForward):
MOVE_FORWARD()
elif(can_moveRight):
TURN_RIGHT()
MOVE_FORWARD()
elif(can_moveLeft):
TURN_LEFT()
MOVE_FORWARD()
}
Problem 3
Which Is the Correct Way to define the Name of a Procedure?
- A. PROCEDURE MYLIST
- B. PROCEDURE MyList
- C. procedure mylist
Click For Hint
Remember the word procedure should be in all capitals and a part of the declared procedure should be in caps
Explanation: B, because you want to make sure the PROCEDURE is in all caps and when defining a function it should be lowercase unless you are starting a new word, and you can't put a space, so you capitalize the first letter of the words
Problem 4
Write A Procedure That gets the BeachBall To the Green Square
PROCEDURE moveBeachBall {
ROTATE_LEFT()
MOVE_FORWARD()
ROTATE_RIGHT()
REPEAT (6):
MOVE_FORWARD()
ROTATE_LEFT()
MOVE_FORWARD()
MOVE_FORWARD()
}