Solving of tasks using two-dimensional arrays. Examples
Contents
- 1. Creation of a third matrix based on two matrices
- 2. Processing a matrix consisting of rows. Determining the number of rows according to the criterion
- 3. Create a matrix based on the other two according to the formula
- 4. Determine the contents of a two-dimensional table according to a condition
- 5. Formation of a one-dimensional array based on a two-dimensional matrix. Determining the arithmetic mean column of a matrix
- Related topics
Search other resources:
1. Creation of a third matrix based on two matrices
Task. Given two matrices of numbers A and B. Dimension of matrices m×n (m – number of rows, n – number of columns). The numbers of matrices are formed randomly. Form a matrix C in which each element is determined by the formula
where i = 1, 2, …, m; j = 1, 2, …, n.
Solution.
// Two-dimensional arrays. Solution of tasks. // Creating a third matrix based on two // 1. Declaring matrices and creating them var A = new Array(), B = new Array() // original matrices var C = [] // resulting matrice // 2. Enter the dimension of matrices m, n var m = prompt("m = ") var n = prompt("n = ") // 3. Allocate memory for matrix cells // 3.1. Allocate memory for m rows of matrices A.length = m B.length = m // 3.2. Allocate memory for n columns in each row for (var i=0; i<A.length; i++) { // Create the array A[i] and allocate memory for it A[i] = new Array() A[i].length = n // Create the array B[i] and allocate memory for it B[i] = new Array() B[i].length = n } // 4. Fill matrices A, B with random numbers in the interval [min; max] var min = -10 // lower bound of interval var max = 10 // upper bound of the interval for (var i=0; i<A.length; i++) for (var j=0; j<A[i].length; j++) { // The array sizes are the same, so you can form two arrays at once A[i][j] = parseInt(min + Math.random()*(max - min + 1)) B[i][j] = parseInt(min + Math.random()*(max - min + 1)) } // 5. Display matrix A document.write("A:<br>") for (var i=0; i<A.length; i++) { for (var j=0; j<A[i].length; j++) document.write(A[i][j] + " ") document.write("<br>") } document.write("<br>") // 6. Display matrix B document.write("B:<br>") for (var i=0; i<B.length; i++) { for (var j=0; j<B[i].length; j++) document.write(B[i][j] + "\t") document.write("<br>") } document.write("<br>") // 7. Form the resulting matrix C // 7.1. Allocate memory for the matrix C with size m*n C.length = A.length // number of rows for (var i=0; i<C.length; i++) { C[i] = [] // create the array C[i] C[i].length = n // set the number of columns } // 7.2. Fill in the values of the matrix cells according to the task for (var i=0; i<C.length; i++) for (var j=0; j<C[i].length; j++) C[i][j] = A[i][j]*A[i][j] + B[i][j] // 8. Display the resulting matrix C document.write("C:<br>") for (var i=0; i<C.length; i++) { for (var j=0; j<C[i].length; j++) document.write(C[i][j] + " ") document.write("<br>") }
Result
A: -1 5 -8 5 3 5 8 -3 -5 0 -5 10 0 -5 9 B: 9 -4 -9 1 -4 0 2 5 -1 6 1 -4 5 0 8 C: 10 21 55 26 5 25 66 14 24 6 26 96 5 25 89
⇑
2. Processing a matrix consisting of rows. Determining the number of rows according to the criterion
Task. A matrix A of words (rows) of size m×n is given. Determine how many rows of the matrix have length greater than the given k. Matrix elements A and m, n, k are entered from the keyboard.
Solution.
// Two-dimensional arrays. Solution of tasks. // Determining the number of matrix rows according to the criterion // 1. Matrix declaration var A = new Array() // 2. Input the dimension of the matrix m*n var m = prompt("m = ") var n = prompt("n = ") // 3. Input k var k = prompt("k = ") // 4. Allocate memory for matrix A.length = m for (var i=0; i<m; i++) { A[i] = new Array() A[i].length = n } // 5. The cycle of forming a matrix of rows for (var i=0; i<m; i++) for (var j=0; j<n; j++) { A[i][j] = prompt("A[" + i + "][" + j + "] = ") } // 6. Display matrix A document.write("A:<br>") for (var i=0; i<A.length; i++) { for (var j=0; j<A[i].length; j++) document.write(A[i][j] + " ") document.write("<br>") } document.write("<br><br>") // 7. Loop for calculating the number of rows with length no more than k. var count = 0 // result for (var i=0; i<A.length-1; i++) for (var j=0; j<A[i].length; j++) if (A[i][j].length <= k) count++ // 7. Display the result document.write("-----------------------------<br>") document.write("count = " + count)
Result
A: abcd jklmn jprstq 123456 ----------------------------- count = 2
⇑
3. Create a matrix based on the other two according to the formula
Task. Given natural numbers m, n and a matrix of real numbers Aij, Bij, where i = 1, 2, …, m; j = 1, 2, …, n. Calculate the value of matrix elements Cij, if:
// Two-dimensional arrays. Solution of tasks. // Formation of the third matrix based on two given ones // 1. Ввод m, n var m = prompt("m = ") var n = prompt("n = ") // 2. Declaring matrices A, B and filling them with values. // 2.1. Create arrays A, B var A = [] var B = [] // 2.2. Set the number of rows m A.length = m B.length = m // 2.3. Create subarrays and customize them. for (var i=0; i<A.length; i++) { // Create subarrays in arrays A, B. A[i] = [] A[i].length = n B[i] = [] B[i].length = n } // 2.4. Input numbers into arrays A, B for (var i=0; i<A.length; i++) for (var j=0; j<A[i].length; j++) A[i][j] = prompt("A["+i+"]["+j+"] = ") for (var i=0; i<B.length; i++) for (var j=0; j<B[i].length; j++) B[i][j] = prompt("B["+i+"]["+j+"] = ") // 3. Display arrays A and B document.write("A:<br>") for (var i=0; i<A.length; i++) { for (var j=0; j<A[i].length; j++) document.write(A[i][j]+" ") document.write("<br>") } document.write("<br>") document.write("B:<br>") for (var i=0; i<B.length; i++) { for (var j=0; j<B[i].length; j++) document.write(B[i][j]+" ") document.write("<br>") } document.write("<br>") // 4. Creating a C matrix and setting the dimensions of the matrix var C = [] C.length = m for (var i=0; i<C.length; i++) { C[i] = [] C[i].length = n } // 5. Filling the matrix C with the resulting values according to the formula for (var i=1; i<=C.length; i++) for (var j=1; j<=C[i-1].length; j++) { if (i<j) C[i-1][j-1] = i*i + j - 5; else if (i==j) C[i-1][j-1] = 1 / ((i+j)*(i+j)*(i+j)); else C[i-1][j-1] = Math.sin(A[i-1][j-1])*Math.sin(B[i-1][j-1]) + Math.cos(B[i-1][j-1]) } // 6. Display the result document.write("C:<br>") for (var i=0; i<C.length; i++) { for (var j=0; j<C[i].length; j++) document.write(C[i][j]+" ") document.write("<br>") }
Result
A: 1 2 3 4 5 6 B: 1 1 1 1 1 1 C: 0.125 -2 -1 -0.09652503516369604 0.015625 2
⇑
4. Determine the contents of a two-dimensional table according to a condition
Task.
A natural number n is given. Determine the number of positive and the number of negative elements of table A with dimension n×n, if
where i, j = 1, 2, … , n.
Solution.
// Two-dimensional arrays. Solving of tasks. // A natural number n is given. Determine the number // of positive and the number of negative elements of table A. // 1. Input n var n = prompt("n = ") // 2. Table A declaration var A = [] // 3. Set the dimensions of table A A.length = n for (var i=0; i<A.length; i++) { A[i] = [] A[i].length = n } // 4. Form table A according to the formula for (var i=0; i<A.length; i++) for (var j=0; j<A[i].length; j++) A[i][j] = Math.sin(i*i-j) // 5. Print table A with 2 decimal places. document.write("A:<br>") for (var i=0; i<A.length; i++) { for (var j=0; j<A[i].length; j++) document.write(A[i][j].toFixed(2)+" ") document.write("<br>") } document.write("<br>") // 6. Calculate the number of positive and negative elements. var n1 = 0 // positive elements var n2 = 0 // negative elements for (var i=0; i<A.length; i++) for (var j=0; j<A.length; j++) { if (A[i][j]<0) n1++ else n2++ } // 7. Display the result document.write("n1 = " + n1 + "<br>") document.write("n2 = " + n2 + "<br>")
Result
A: 0.00 -0.84 -0.91 -0.14 0.84 0.00 -0.84 -0.91 -0.76 0.14 0.91 0.84 0.41 0.99 0.66 -0.28 n1 = 7 n2 = 9
⇑
5. Formation of a one-dimensional array based on a two-dimensional matrix. Determining the arithmetic mean column of a matrix
Task. The matrix Aij is given, where i=1, 2, …, m, j = 1, 2, …, n. Create a new one-dimensional array Bi of size m, in which the elements are equal to the arithmetic mean of each column of matrix A.
Solution.
// Two-dimensional arrays. Solution of tasks. // Form an array of average arithmetic columns of a given matrix. // 1. Input m, n var m = prompt("m = ") var n = prompt("n = ") // 2. Declaring the A matrix var A = [] // 3. Set the dimensions of matrix A. A.length = m for (var i=0; i<A.length; i++) { A[i] = [] A[i].length = n } // 4. Input matrix A for (var i=0; i<A.length; i++) for (var j=0; j<A[i].length; j++) A[i][j] = prompt("A["+i+"]["+j+"] = ") // 5. Display m, n document.write("m = " + m + "<br>") document.write("n = " + n + "<br>") // 6. Display matrix A document.write("A:<br>") for (var i=0; i<A.length; i++) { for (var j=0; j<A[i].length; j++) document.write(A[i][j]+" ") document.write("<br>") } // 7. Create the resulting array B and set its dimensions var B = [] B.length = n // number of columns, one-dimensional array // 8. The cycle of calculating the arithmetic mean and filling the matrix B var avg // additional variables - average for (var i=0; i<n; i++) // outer loop over columns { avg = 0; for (var j=0; j<m; j++) // calculate the sum of column i avg += parseFloat(A[j][i]) B[i] = avg / m } // 9. Return the result document.write("B:<br>") for (var i=0; i<B.length; i++) document.write(B[i].toFixed(2) + " ") document.write("<br>")
Result
m = 2 n = 3 A: 1 2 3 4 5 6 B: 2.50 3.50 4.50
⇑
Related topics
- One-dimensional arrays. Creating an array. The length property. Array formation
- Two-dimensional arrays. Arrays within arrays. Three-dimensional arrays
- Task solving using one dimensional arrays. Examples
⇑