Question

In: Computer Science

Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...

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

Solutions

Expert Solution

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");
}


Related Solutions

Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns...
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...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT