don’t know where to start someone to help me?

Asked

Viewed 63 times

1

Read two integer matrices A and B (5 X 5) and, from them, manages the SOMA matrix (corresponding to the sum of the two matrices: A + B) and the matrix SUBTRACTION (corresponding to subtraction between the two matrices: A - B).

What I’ve done so far is:

var matrizA= [
  [],[],[],[],[],
  [],[],[],[],[],
  [],[],[],[],[],
  [],[],[],[],[],
  [],[],[],[],[] 
];
var matrizB=[ 
  [],[],[],[],[],
  [],[],[],[],[],
  [],[],[],[],[],
  [],[],[],[],[],
  [],[],[],[],[] 
];
for (var linha = 0; linha <= 4; linha++) {
  matrizA = prompt("digite 5 valores para matrizA: ");
  matrizB = prompt("digite 5 valores para matrizB: ")
  for (var coluna = 0; coluna <= 4; coluna++) {
    matrizA[linha][coluna] = matrizB[linha][coluna] = (linha + 1) + (coluna + 1);
    document.write("[" + (linha + 1) + "] + [" + (coluna + 1) + "] =" + matrizA[linha][coluna] + matrizB[linha][coluna] + "<br>");
  }
  document.write("<br>");
}

  • Hi, I copied what you put in comment to the question. You can always [Edit] the question to clarify it. Avoid changing the direction of the question, but to clarify it is good to edit.

  • ok got it. end another attempt where I can go through the matrix row as far as the matrix column

1 answer

1


I read that question like this:

Create two arrays, with integer numbers.
Then generates a new matrix where each position contains the sum of the respective numbers in the other matrices.
Then generates a new matrix where each position contains the sum of the respective numbers in the other matrices.

I don’t know how you want to add these numbers, but in case you already have them, you can create new arrays with the .map() because what you want are new arrays with the same structure but with the sums/subtractions...

An example would be:

const a = [
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5]
]

const b = [
  [6, 7, 8, 9, 10],
  [6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 10],
  [6, 7, 8, 9, 0],
]

const AmenosB = a.map((arraydeCinco, i) => {
  return arraydeCinco.map((numero, x) => {
    return numero - b[i][x];
  });
});
const AmaisB = a.map((arraydeCinco, i) => {
  return arraydeCinco.map((numero, x) => {
    return numero + b[i][x];
  });
});

console.log('AmenosB', JSON.stringify(AmenosB));
console.log('AmaisB', JSON.stringify(AmaisB));

Since you have a lot of duplicate code you can simplify it like this:

const a = [
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5]
]

const b = [
  [6, 7, 8, 9, 10],
  [6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 10],
  [6, 7, 8, 9, 0],
]


const calc = (arrA, arrB, sign) => {
  return arrA.map((arr, i) => {
    return arr.map((numero, x) => {
      return numero + (arrB[i][x] * sign);
    });
  });
}

const AmenosB = calc(a, b, -1);
const AmaisB = calc(a, b, 1);


console.log('AmenosB', JSON.stringify(AmenosB));
console.log('AmaisB', JSON.stringify(AmaisB));

The same example with a for could be:

const a = [
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5]
]

const b = [
  [6, 7, 8, 9, 10],
  [6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 10],
  [6, 7, 8, 9, 0],
]

const AmenosB = [];
const AmaisB = [];
for (let x = 0, lx = a.length; x < lx; x++) {
  const soma = [];
  const diff = [];
  for (let y = 0, ly = a[x].length; y < ly; y++) {
    soma.push(a[x][y] + b[x][y]);
    diff.push(a[x][y] - b[x][y]);
  }
  AmenosB.push(diff);
  AmaisB.push(soma);
}

console.log('AmenosB', JSON.stringify(AmenosB));
console.log('AmaisB', JSON.stringify(AmaisB));

  • you can explain to me how I would wear with a for loop?

  • @anamarialeiteramos the .map() does the same as the bow for only that it takes advantage of the result. You can do with for also but you have to use more lines...

  • ah, okay I get it. Really worth it!!

  • @anamarialeiteramos I also joined an example with for

  • really with the map becomes much simpler and smaller the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.