Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Form coils in a matrix
We will be using JavaScript to form coils in a matrix. The process involves manipulating the elements of the matrix to create a spiral pattern. This can be achieved by changing the direction of traversal, keeping track of the visited elements, and adjusting the indices accordingly.
Approach
One approach to form coils in a matrix using JavaScript would be the following ?
Define the size of the matrix.
Initialize the matrix with zeros.
Use nested loops to traverse the matrix and change the values of specific cells according to the pattern of the coil.
Keep track of the direction of traversal (right, down, left, up) and change direction as needed.
Use another loop to print the matrix.
Repeat the process to form multiple coils if needed.
Example
Here is an example of how you could implement a function in JavaScript to form coils in a matrix ?
function formCoils(matrix) {
let row = 0, col = 0, direction = 'down';
for (let i = 0; i < matrix.length * matrix[0].length; i++) {
matrix[row][col] = i + 1;
if (direction === 'down') {
if (row === matrix.length - 1 || matrix[row + 1][col] !== 0) {
direction = 'right';
col++;
} else {
row++;
}
} else if (direction === 'right') {
if (col === matrix[0].length - 1 || matrix[row][col + 1] !== 0) {
direction = 'up';
row--;
} else {
col++;
}
} else if (direction === 'up') {
if (row === 0 || matrix[row - 1][col] !== 0) {
direction = 'left';
col--;
} else {
row--;
}
} else if (direction === 'left') {
if (col === 0 || matrix[row][col - 1] !== 0) {
direction = 'down';
row++;
} else {
col--;
}
}
}
return matrix;
}
// Create a 4x4 matrix filled with zeros
const matrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
console.log("Original matrix:");
console.log(matrix);
// Form coils in the matrix
const result = formCoils(matrix);
console.log("\nMatrix with coils:");
console.log(result);
// Display matrix in a readable format
console.log("\nFormatted output:");
for (let i = 0; i < result.length; i++) {
console.log(result[i].join('\t'));
}
Original matrix: [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ] Matrix with coils: [ [ 1, 12, 11, 10 ], [ 2, 13, 16, 9 ], [ 3, 14, 15, 8 ], [ 4, 5, 6, 7 ] ] Formatted output: 1 12 11 10 2 13 16 9 3 14 15 8 4 5 6 7
How It Works
The formCoils function takes in a matrix and returns the same matrix with numbers formed in a coil shape starting from the top left corner.
The function uses a variable direction to keep track of the direction in which the number should be filled in the matrix. It starts with direction set to 'down', and updates direction based on the current position of the matrix and whether the next position is filled or not. The number is then placed in the current position and the row and column variables are updated accordingly.
This process is repeated until every position in the matrix has been filled with a number, creating a spiral or coil pattern from outside to inside.
Alternative Approach - Clockwise Spiral
Here's another approach that creates a clockwise spiral starting from the top-left corner:
function createClockwiseCoil(n) {
// Create n x n matrix filled with zeros
const matrix = Array(n).fill().map(() => Array(n).fill(0));
let num = 1;
let top = 0, bottom = n - 1, left = 0, right = n - 1;
while (num <= n * n) {
// Fill top row (left to right)
for (let i = left; i <= right && num <= n * n; i++) {
matrix[top][i] = num++;
}
top++;
// Fill right column (top to bottom)
for (let i = top; i <= bottom && num <= n * n; i++) {
matrix[i][right] = num++;
}
right--;
// Fill bottom row (right to left)
for (let i = right; i >= left && num <= n * n; i--) {
matrix[bottom][i] = num++;
}
bottom--;
// Fill left column (bottom to top)
for (let i = bottom; i >= top && num <= n * n; i--) {
matrix[i][left] = num++;
}
left++;
}
return matrix;
}
const spiralMatrix = createClockwiseCoil(4);
console.log("Clockwise spiral matrix:");
for (let i = 0; i < spiralMatrix.length; i++) {
console.log(spiralMatrix[i].join('\t'));
}
Clockwise spiral matrix: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
Conclusion
Forming coils in a matrix involves strategic direction changes and boundary checking. Both approaches demonstrate different spiral patterns - one starting with downward movement and another with clockwise traversal from the outer edges inward.
