Unit 2 Assignment Using the selected pattern write a worked example with subgoal
ID: 3789254 • Letter: U
Question
Unit 2 Assignment
Using the selected pattern write a worked example with subgoal labels that could be used in a class you teach or could teach in the future.
The actual worked example should include the prompt for the problem and the solution with subgoal labels included.
Setting up selection statements - If/Else Statement
9-12th grade - First time programmers
JavaScript
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.
function start(){
if(facingSouth()){
turnLeft();
}else{
turnAround();
}
}
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int direction; //direction can take 4 values N-0 W-1 S-2 E-3
bool facingSouth()
{
if(direction == 2 )
return true;
}
void turnLeft()
{
direction = (direction+1)%4 ;
}
void turnAround()
{
direction = (direction+2)%4;
}
void function start()
{
if(facingSouth())
turnLeft();
else
turnAround();
}
int main()
{
cout<<"Enter any no(0-3) for direction";
cin>>direction;
start();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.