Question

In: Computer Science

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

Coded in Java script

Solutions

Expert Solution

Solution:

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

Code for function:

function Make2D(width,height)
{
  twoD= new Array(height);
  for(var i=0;i < height ;i++)
  {
    twoD[i] = new Array(width);
  }
  return twoD;
}

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

Code for function:

function fill2D(twoD,value)
{
  for (var i = 0; i < twoD.length; i++) 
  { 
    for (var j = 0; j < twoD[i].length; j++) 
    { 
        twoD[i][j] = value; 
    } 
  } 
  return twoD;
}

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

Code for function:

function Init2D(width, height, value)
{
  twoD=Make2D(width,height);
  twoD=fill2D(twoD,value);
  return twoD;
}

Complete code of all functions with implementation:

function Make2D(width,height)
{
  twoD= new Array(height);
  for(var i=0;i < height ;i++)
  {
    twoD[i] = new Array(width);
  }
  return twoD;
}
function fill2D(twoD,value)
{
  for (var i = 0; i < twoD.length; i++) 
  { 
    for (var j = 0; j < twoD[i].length; j++) 
    { 
        twoD[i][j] = value; 
    } 
  } 
  return twoD;
}
function Init2D(width, height, value)
{
  twoD=Make2D(width,height);
  twoD=fill2D(twoD,value);
  return twoD;
}
var twoD = Make2D(3,4);
console.log(twoD);

twoD = fill2D(twoD, 2);
console.log(twoD);

twoD = Init2D(5, 6, 7);
console.log(twoD);

Screenshot of complete code and output:


Related Solutions

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...
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 named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
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 recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return the linked list unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly and then return the linked list with the modifications, not a list. If you have a linked list 11 -> 12 -> 18 -> 20...
In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return head unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly. If you have a linked list 11 -> 12 -> 18 -> 20 -> 24 -> 32 -> 38 -> 44 and skip =2, then you should...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT