Question

In: Computer Science

I wrote a code snippet to draw a bit of 'abstract art': yertle.penDown(); yertle.forward(10); yertle.right(Math.PI/2); yertle.forward(10);...

I wrote a code snippet to draw a bit of 'abstract art':

yertle.penDown();
yertle.forward(10);
yertle.right(Math.PI/2);
yertle.forward(10);
yertle.right(Math.PI/2);
yertle.forward(10);
yertle.right(Math.PI/2);
yertle.forward(10);
yertle.right(Math.PI/2);
yertle.left(Math.PI/2);
yertle.forward(20);
yertle.right(Math.PI/2);
yertle.forward(20);
yertle.right(Math.PI/2);
yertle.forward(20);
yertle.right(Math.PI/2);
yertle.forward(20);
yertle.right(Math.PI/2);
yertle.left(Math.PI/2);
yertle.forward(30);
yertle.right(Math.PI/2);
yertle.forward(30);
yertle.right(Math.PI/2);
yertle.forward(30);
yertle.right(Math.PI/2);
yertle.forward(30);
yertle.right(Math.PI/2);
yertle.left(Math.PI/2);
yertle.forward(40);
yertle.right(Math.PI/2);
yertle.forward(40);
yertle.right(Math.PI/2);
yertle.forward(40);
yertle.right(Math.PI/2);
yertle.forward(40);
yertle.right(Math.PI/2);
yertle.left(Math.PI/2);
yertle.penUp();

My instructor suggested I could get the same result from a much shorter program, but I don't see how.
What should I write instead?
(Keep it concise, and don't include anything outside the scope of the question)

Solutions

Expert Solution

yes,

this seems a pretty long code. the thing you want to do can be done with much lesser steps.

i can see from the code that you are running similar lines of code many times.

like all your code is full of 'yertle.right(Math.PI/2);' and 'yertle.forward(i);'.

now you have done this 15-16 times. but suppose if we get a similar question where we need to do it a 1000 times. in that case we can't write these instructions a 1000 times manually. right?

for purposes like these we use the concept of 'loop'. loop is something which helps us to write multiple lines of code in just 3 lines.

this does nothing but saves our time.

just to give you an example,

see the following code

instead of writing the same stuff 4 times, what we can do is to run a loop.

we use 'for' function to run a loop.

in the above example, we need to run the loop 4 times. so we will use the 'for' loop as

more precisely, it can be written as:

for(int i = 0;i<4;i++)

{

  yertle.forward(40);

yertle.right(PI/2);

}

this will work same as

so you see, we can save so much of time if we run a loop.

your instructor might be talking about this.

hope it helps


Related Solutions

When I wrote this code in the Eclipse program, I did not see a output .....
When I wrote this code in the Eclipse program, I did not see a output .. Why? _______ public class AClass { private int u ; private int v ; public void print(){ } public void set ( int x , int y ) { } public AClass { } public AClass ( int x , int y ) { } } class BClass extends AClass { private int w ; public void print() { System.out.println (" u + v...
I wrote this code and just realized I need to put it into at least 6...
I wrote this code and just realized I need to put it into at least 6 different functions and I don't know how. No specific ones but recommended is: Read Data, Calculate Installation Price, Calculate Subtotal, Calculate Total, Print -> 1) Print Measurements & 2) Print Charges. Can somebody help? #include <stdio.h> // Function Declarations int length, width, area, discount; int main () { // Local Declarations double price, cost, charge, laborCharge, installed, amtDiscount, subtotal, amtTax, total; const double tax...
please i need the code for an 8 bit by 8 bit multiplier in verilog ....
please i need the code for an 8 bit by 8 bit multiplier in verilog . ( pls the code should be clearly written and BOLD)
Every time I run this code I get two errors. It's wrote in C# and I...
Every time I run this code I get two errors. It's wrote in C# and I can't figure out what I'm missing or why it's not running properly. These are two separate classes but are for the same project. using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RecursiveMethod { class Program { static bool ordering = true; static string str; static int quantity = 0; static string Invoic = string.Empty; static double itemPrice; static double...
1. Translate the following code into MIPS code. B[i + 10] = B[i -2] + 40;...
1. Translate the following code into MIPS code. B[i + 10] = B[i -2] + 40; i = i + 10; B[3] = B[i - 1]; a) Assume B is an array of integers (each integer takes 4 bytes). B's address is stored at register $10. Also assume that the compiler associates the variable i to the register $11. b) Assume B is an array of characters (each character takes one byte). B's address is stored at register $10. Also...
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet...
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If...
I just wrote Python code to solve this problem: Write a generator that will return a...
I just wrote Python code to solve this problem: Write a generator that will return a sequence of month names. Thus gen = next_month('October') creates a generator that generates the strings 'November', 'December', 'January' and so on. If the caller supplies an illegal month name, your function should raise a ValueError exception with text explaining the problem. Here is my code: month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def next_month(name: str) -> str:...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello”...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello” << endl;      num--; } while(num > 1); After the above code is executed: “Hello” printed how many times: ___________ What is value ofnum:  ____________ language is c++
I wrote this code and it produces a typeError, so please can you fix it? import...
I wrote this code and it produces a typeError, so please can you fix it? import random def first_to_a_word(): print("###### First to a Word ######") print("Instructions:") print("You will take turns choosing letters one at a time until a word is formed.") print("After each letter is chosen you will have a chance to confirm whether or not a word has been formed.") print("When a word is formed, the player who played the last letter wins!") print("One of you has been chosen...
I need the full code. No hardcoding. Code a function to convert any 8-bit number from...
I need the full code. No hardcoding. Code a function to convert any 8-bit number from binary to hexadecimal. You are not allowed to use libraries that automate this process. a) Test your function with the following binary numbers. 11110000 and 00100010 The binary number will be provided by the user and you must read it using fgets. b) Draw a flowchart of your code. You can draw it by hand but it must be readable, and respect the flowchart...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT