Unit 2 Assignment using the code given below write a write a worked example with
ID: 3790262 • Letter: U
Question
Unit 2 Assignment
using the code given below write a write a worked example with subgoal labels The actual worked example should include the prompt for the problem and the solution with subgoal labels included.
an example of a worked example with sublabels is posted below the code in the image.
JavaScript
Prompt:
Karel got spun around and is facing the wrong direction. Karel could be facing south or west, and you need to get Karel back to facing east. You must use an if/else statement here, and be sure to test your code on both worlds.
Code:
function start(){
if(facingSouth()){
turnLeft();
}else{
turnAround();
}
}
Worked Example #1 for Sub-Goal CS Study – Calculating Average
Your friend is working as a server in a restaurant. He has a collection of tips, but wants to know what his average tip is on a Friday
night. You have volunteered to write a program for him to help him calculate his average tip.
Here are his tips for last Friday night. What is his average tip?
$15.00, $5.50, $6.75, $10.00, $12.00, $18.50, $11.75, $9.00
Solution
SUBGOAL: define and initialize variables
Step one: define and initialize variable to hold the collection of tips
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9] list containing all the tip values
Step two: define an initialize variable to hold the sum
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9] list containing all the tip values
sum = 0 accumulator to hold sum of values
SUBGOAL: initialize the loop
Step three: initialize the loop to start at the beginning of the list of tips
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9]
sum = 0
lcv = 0 lcv is loop control variable
# We want to start by looking at the first value in the list which has an index of 0
SUBGOAL: determine loop condition
Sub-SUBGOAL: determine termination condition of loop
Step four: determine when we are done with the list of tips
# In this case we want to process every element in the list, so we will terminate when we reach the end of the list, or when lcv has the value of length(tips)
lcv >= length(tips)
Sub-SUBGOAL: invert the termination condition into a continuation condition
Step five: change the ending condition to be a continuing condition
(continue looking at tips while…)
# if the termination is when lcv >= length(tips), then to reverse that we have:
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9]
sum = 0
lcv = 0
WHILE lcv < length(tips) DO
ENDWHILE
SUBGOAL: update loop
Step six: move to the next tip
# We want to look at every element in the list so the update of lcv is by one
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9]
sum = 0
lcv = 0
WHILE lcv < length(tips) DO
lcv = lcv + 1
ENDWHILE
SUBGOAL: process body of loop (why did we write it?)
Step seven: add the current tip to the sum
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9]
sum = 0
lcv = 0
WHILE lcv < length(tips) DO
sum = sum + tips[lcv] as lcv is incremented by one each time through the loop, the next
lcv = lcv + 1 value in the list will be added to sum
ENDWHILE
SUBGOAL: determine results
Step eight: calculate the average
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9]
sum = 0
lcv = 0
WHILE lcv < length(tips) DO
sum = sum + tips[lcv]
lcv = lcv + 1
ENDWHILE
average = sum / length(tips) the average is the sum of the elements divided by the number of elements
Step nine: print results
tips = [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9]
sum = 0
lcv = 0
WHILE lcv < length(tips) DO
sum = sum + tips[lcv]
lcv = lcv + 1
ENDWHILE
average = sum / length(tips)
PRINTLN average
Step one: define and initialize variable to hold the collection of tips tips [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9] list containing all the tip values Step two: define and initialize variable to hold the sum tips [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9] list containing all the tip values accumulator to hold sum of values sum 0 Step three: initialize the loop to start at the beginning of the list of tips tips [15, 5.50, 6.75, 10, 12, 18.50, 11.75, 9] Sum 0 lcv is loop control variable We want to start by looking at the first value in the list which has an index of 0 Step four: determine when we are done with the list of tips In this case we want to process every element in the list, so we will terminate when we reach the end of the list, or when lc has the value of length(tips) lcv length (tips)Explanation / Answer
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
LOOP:do {
if( a == 15) {
// skip the iteration.
a = a + 1;
goto LOOP;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.