Sandbox

SANDBOX For Computational Mathematics Projects

The project begins with four methods. 

pow(a, b) returns the value for a^b

m = pow(2,3);  //variable m holds the value 8.0 since 2*2*2 = 8.

sqrt(a) returns the square root of a

n = sqrt(25.); //variable n holds the value 5.0 since the square root of 25 is 5.

abs(a) returns the absolute value of a

o = abs(2) + abs(-2);  //variable o holds the value 4.

drawPoint(a,b) draws a point on the graph at (a,b).

drawPoint(3,5);  //This will draw a point on the graph where x = 3 and y = 5.


Copy this code and paste in the text box to get started:


array = [ 3.0, 2.0, 5.0 ];

a = array[0] + array[1];

b = abs(-3.0);

drawPoint(a,b);


c = pow(2.0,3.0);

d = sqrt(25.0);

drawPoint(c,d);


sum = 0;

for (i = 0; i < array.length; i = i + 1)

{

    sum = sum + array[i];

}


if (sum > 8)

{

    sum = 1 + 3 *2;

}


sum2 = 0;

number = 1;

while (number <=3)

{

      sum2 = sum2 + number;

      number = number + 1;

}

drawPoint(sum, sum2);

EXPLANATION:


a is initialized to be 3.0 + 2.0 = 5.0 (Note how the indexes for the array begins at 0 and not 1)

b is initialized to be the absolute value of -3.0, which is 3.0.

The first point should be drawn at (5, 3)


c is initialized to be 2.0^3.0 = 8.0 

d is initialized to be the square root of 25.0, which is 5.0

The second point should be drawn at (8, 5)


sum is initialized to be 0.  The "for" loop iterates while i is 0,1, and 2 (array.length is 3).  The values for array[0], array[1], and array[2] are 3, 2, and 5.  For each iteration, these values are added to 'sum'.  When the "for" loop terminates, 'sum' is 10.


Since 10 > 8, sum becomes 7 (remember order of operations; the multiplication evaluates before addition).


The "while" loop iterates while 'number' is 1, 2, and 3.  These values are added to 'sum2' through each iteration.  Therefore, 'sum2' is equal to 6.

The third point should be drawn at (7,6).


This sandbox shows an example of the coding concepts (variables, arrays, if statements, and loops) you should be familiar with when you start.   However, your understanding of these concepts should increase dramatically as you progress.  Also, try some commands on your own and experiment.