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...
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:...
I am trying to write the code for an 8 bit adder in VHDL so that...
I am trying to write the code for an 8 bit adder in VHDL so that I can program it onto my Elbert V2 Spartan 3A FPGA Development Board, but I keep getting errors. Any ideas what I am doing wrong? library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity adder8bit is Port ( a : in STD_LOGIC_VECTOR(7 downto 0); b : in STD_LOGIC_VECTOR(7 downto 0); cin : in STD_LOGIC; o : out STD_LOGIC_VECTOR(7 downto 0); cout : out STD_LOGIC); end adder8bit; architecture Behavioral...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must read two digital input signals and turn on two LEDS as needed to show the sum of the inputs. e.g. 0 + 1 = 01.
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
To test whether my friend’s fish Googly had psychic powers, I wrote R code to display...
To test whether my friend’s fish Googly had psychic powers, I wrote R code to display two windows. I entered either “Left” or “Right” depending on which way Googly was facing. Then the random number generator in R selected either the left or the right window, with probability 0.5 for each, in which to display a star. Let p be the probability Googly guesses correctly on a given trial (assume this is constant.) In 80 trials, Googly correctly guessed the...
Hello, I am very new to 64-bit ARM assembly and would like the code to this...
Hello, I am very new to 64-bit ARM assembly and would like the code to this following question: The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so...
I am trying to create an 8-bit random number generator in verilog code using a mux,...
I am trying to create an 8-bit random number generator in verilog code using a mux, a d flip flop and a LFSR not sure what I am doing wrong but need some help with getting it working properly any help would be greatly appreciated. here is what I have so far: module RNG #(parameter size=8)(output [7:0]SO,output [7:0] RN,input clk,rst,input[size-1:0]seed,input L);    wire [7:0] Sin=SO[7]^SO[5]^SO[4]^SO[3];    ffw F1 (SO,clk,rst,Sin);    MUX M1 (Sin,seed,{SO[size-2:0],next},L);    xor X1 (next,SO[6],SO[7]);    assign RN=next;...
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would...
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would like to find another way to compile the program without using static_cast in my code. Thank you #include <iostream> #include <iomanip> using namespace std; class Population { private: int population, birth, death; public: Population() { population = 2; birth = 0; death = 0; } Population(int x, int y, int z) { if (x < 2) population = 0; else population = x; if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT