Skip to main content

✍️ Practice (Medium)

1. f of x​

Write a function which helps apply this formula:

f(x)=(x−4)(x+7)f(x) = (x - 4)(x + 7)

Then, use it to evaluate f(x)f(x) for the values of xx:

  • x=4x = 4
  • x=−7x = -7
  • x=2x = 2
  • x=−6x = -6

2. Melon Farm​

You are cultivating a melon farm in minecraft and need to figure out your total farmable area.

Write a function to calculate the area of a rectangle:

Arearectangle=width∗heightArea_{rectangle} = width * height

Write a function to calculate the area of a square:

Areasquare=side2Area_{square} = side^2

Using the functions from before, calculate the total farmable area of your melon farm

farm

3. Tracing Trash​

Consider the following code:

static void Main()
{
int a = 2, b = 3, c = 4;
Snowing (a, b, c);
Snowing (c, b, b);
Snowing (a, c, a);
}

static void Snowing (int r, int grapes, int g)
{
int mylocal;
mylocal = (r + grapes) * g;
Console.WriteLine("You passed in " + r + " and " + grapes + " and " + g);
Console.WriteLine("Your output is: " + mylocal);
}

What will be the output when running this:

Main();

4. Tracing Garbage​

Consider the following code:

static void Main()
{
int x = 2, y = 4, z = 7;
double a = 0, b = 0;

a = Crazy(z, y, y);
b = Crazy(y, x, z) + Crazy(x, y, z);
Console.WriteLine("I see 'a' as " + a + " and 'b' as " + b);
}

static double Crazy (int y, int x, int z)
{
double mylocal;
mylocal = x - y;
mylocal *= 2;
mylocal += z;
return mylocal;
}

What will be the output when running this:

Main();

5. Tracing Absolute Junk​

Consider the following code:

static void Main() {
int a = 5, b = 4, c = 9;
a = AddEmUp(b, c, c);
c = a + AddEmUp(a, c, b);
b = AddEmUp(a, a, a) + b;
Console.WriteLine("a = " + a + " b = " + b + " c = " + c);
}

static int AddEmUp (int da, int db, int dc)
       {
int anothervar;
anothervar = (da * 2) + db - dc;
// more code here
          return anothervar;
       } // end of void myfunc

What will be the output when running this:

Main();