Home > Mobile >  I have two 2D arrays and I want to count them
I have two 2D arrays and I want to count them

Time:03-03

I want to count the numeric values ​​of arr2 in a two-dimensional array of arr1.

The index of arr1 starts from 1.

If the array of [0] in arr2 is 3 5 7 I want to get the value of index 3 = 1 index 5 = 5 index 7 = 1 of arr1[0].

It's hard to explain, but please understand and help.

let arr1 = [
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
]

let arr2 = [
 ['3', '1', '4'],
 ['2', '2', '6'],
]

let result = [
 [0, 1, 1, 0, 0],
 [1, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 1, 0, 0, 1, 0, 0]
]

CodePudding user response:

NOTE

The original question has been edited since I first began answering it. The example question data no longer makes sense - my answer addresses the original question and example data/result as I interpreted it, because otherwise I have no idea what your intended result is and you need to edit your question some more to make it make sense.

Original example data from the question:

let arr1 = [
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
]

let arr2 = [
 ['3', '1', '4'],
 ['2', '2', '6'],
 ['8', '4', '2']
]

let result = [
 [1, 0, 1, 1, 0],
 [0, 2, 0, 0, 0, 0, 1],
 [0, 1, 0, 1, 0, 0, 0, 0, 1]
]

A foreword

This question seems suspiciously like you are asking for help on a homework question. I'll give a more general answer first to try and point you in the right direction of solving it yourself. If you still need more specific help, I will also post a hidden detailed answer. Please only refer to the detailed answer if the general answer does not enable you to solve it yourself.

Generally

For a problem like this, it is often helpful to break it down into smaller, more easily solvable parts, and then combine those parts into some larger solution.

For example, consider the more simple problem of looking at a single pair of arrays within arr1 and arr2 in isolation:

let subArr1 = [0, 0, 0, 0, 0];

let subArr2 = ['3', '1', '4'];

Here, it's pretty clear in plain english what you want to do: "iterate" over every index of subArr2 and modify the current value of some index of subArr1 based on that value. Iteration is easy - the most fundamental structure for doing so is a classic for loop:

// Note that we're iterating over the second array here.
// This is important because that is the array that
//   is the "source" of the updates to the other array.
for (let i = 0; i < subArr2.length; i  ) {
  // do something to subArr1...?
}

What do something to subArr1 means is an exercise for the reader (and outlined in the hidden detailed answer) but is not very complicated; don't overthinkg this part!

Once you've figured out how to solve this subarray logic, it would be helpful to encapsulate it into some kind of function, perhaps like so:

function processSubarrays(inputArr1, inputArr2) {
  for (let i = 0; i < inputArr2.length; i  ) {
    // do something to inputArr1...?
  }
}

Then you could easily call this function to process and update some array inputArr1 based on inputArr2. Going with your original example of arr1 and arr2, a "naive" solution would be to just call our processSubarray() function for each pair of subarrays:

processSubarrays(arr1[0], arr2[0]);
processSubarrays(arr1[1], arr2[1]);
processSubarrays(arr1[2], arr2[2]);

But this is verbose and not very dynamic, there's a way to do it better. Again, with our classic iterating for loop!

// Note again, that we are iterating over the second array
for (let i = 0; i < arr2.length; i  ) {
  // do something with processSubarrays()...?
}

Again, what exactly you do in this for loop is an exercise for the reader. But once again, it is very straightforward - don't overthink it!

Secret hidden detailed answer

Please only reveal this if you have attempted to solve it with the explanation above and have been unsuccessful.


 function processSubarrays(subArr1, subArr2) {
   for (let i = 0; i < subArr2.length; i  ) {
     // Determine which index of arr1 we need to modify
     const indexToIncrement = subArr2[i] - 1;
     // Add 1 to that index in arr1
     subArr1[indexToIncrement] = subArr1[indexToIncrement]   1;
   }
 }

 for (let i = 0; i < arr2.length; i  ) {
   // Here we just process each subarray in order
   processSubarrays(arr1[i], arr2[i]);
 }
 

With this as the initial values:

let arr1 = [
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
]

let arr2 = [
 ['3', '1', '4'],
 ['2', '2', '6'],
 ['8', '4', '2']
]

The final values in arr1 after this would be, as desired:

[
  [1, 0, 1, 1, 0],
  [0, 2, 0, 0, 0, 1, 0],
  [0, 1, 0, 1, 0, 0, 0, 1, 0],
]

CodePudding user response:

Your question is a bit unclear, please check out this video for a clear understanding of multi-dimentional arrays : Cherno's C 2D Arrays Please check out this video and then post a question if you encounter any bugs

  • Related