In: Computer Science
Challenge 3 – Make 2D Array
Write a function that takes 3 parameters and makes use of your
prior two functions to create a 2D array filled with a default
parameter.
var twoD = Init2D(<width>, <height>, <fill
val>);
Challenge 4 – Random Integer in Range
Write a function to return a random integer between a minimum value
and maximum value.
var ival = IntRandomRange(<min>, <max>);
Challenge 5 – Random Int 2D Array
Use your prior functions to provide a function that returns a
two-dimension array filled with random integers.
var rand2D = Rand2D(<width>, <height>, <min>,
<max>);
Challenge 1 – 2D Array Maker
Create a function that takes in two parameters and returns a
created two-dimension array.
var twoD = Make2D(<width>, <height>);
Challenge 2 – Fill 2D Array
Create a function that takes a single parameter and fills the 2D
array with that parameter
Use Cases:
Fill2D(<twoD array>, <fill value>); //fills twoD
twoD = fill2D(<twoD array>, <fill value>); //fills
twoD, but also returns reference
Challenge 3 – Make 2D Array
Write a function that takes 3 parameters and makes use of your
prior two functions to create a 2D array filled with a default
parameter.
var twoD = Init2D(<width>, <height>, <fill
val>);
Challenge 4 – Random Integer in Range
Write a function to return a random integer between a minimum value
and maximum value.
var ival = IntRandomRange(<min>, <max>);
Challenge 5 – Random Int 2D Array
Use your prior functions to provide a function that returns a
two-dimension array filled with random integers.
var rand2D = Rand2D(<width>, <height>, <min>,
<max>);
Need to be coded in Java script
Challenge 1 -
Create a function that takes in two parameters and returns a
created two-dimension array.
class TwoDimensionalArray
{
public static void main(String[] args)
{
String[][] salutation = { {"Mr. ",
"Mrs. ", "Ms. "}, {"Snow"}
};
// Mr. Snow
System.out.println(salutation[0][0] + salutation[1][0]);
// Mrs. Snow
System.out.println(salutation[0][1] + salutation[1][0]);
}
}
The output from this program is:
Mr. Snow
Mrs. Snow
Challenge 2 -
Create a function that takes a single parameter and fills the 2D
array with that parameter
// Java program to fill a multidimensional array with given
value.
import java.util.Arrays;
public class Main
{
public static void main(String[] args)
{
int [][]ar = new int [3][4];
for (int[] row : ar) // Fill each row with 10.
Arrays.fill(row, 10);
System.out.println(Arrays.deepToString(ar));
}
}
Output:
[[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]]
Challenge 3 –
Write a function that takes 3 parameters and makes use of your
prior two functions to create a 2D array filled with a default
parameter.
class MultidimensionalArray
{
public static void main(String[] args)
{
// create a 2d array
int[][] a =
{
{1, 2, 3},
{4, 5, 6},
{7},
};
// calculate the length of each row
System.out.println("Length of row 1: " + a[0].length);
System.out.println("Length of row 2: " + a[1].length);
System.out.println("Length of row 3: " + a[2].length);
}
}
Challenge 4 -
Write a function to return a random integer between a minimum value
and maximum value.
Any number from the interval min..max must appear with the same
probability.
alert( randomInteger(1, 5) ); // output as 1
alert( randomInteger(1, 5) ); // output as 3
alert( randomInteger(1, 5) ); // output as 5
Challenge 5 -
Use your prior functions to provide a function that returns a
two-dimension array filled with random integers.
public static void main(String[] args)
{
int values[][] = new int[3][4];
for (int i = 0; i < values.length; i++)
{
for (int j = 0; j < values[i].length; j++) // do the for in the
row according to the column size
{
values[i][j] = ((int) (Math.random() * 10)); // multiple the random
by 10 and then cast to in
System.out.print(values[i][j]);
}
System.out.println(); // add a new line
}
System.out.println("Done");
}