Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive function for matrix determinant calculator
    primarykey
    data
    text
    <p>I am playing with arrays and I try to make a recursive function to calculate the determinant of a matrix A[i][j], where i=j=k. The function that I wrote is as follows. It seems that I am doing something wrong but I can't tell what. The A array is defined by user and read by another function. I try to do it without using and libraries or predefined objects. I have added the whole code as for you to make a general idea:</p> <pre><code>&lt;script&gt; var k; function readGrad() { //reads the matrix dimesnions k = parseInt(document.getElementById("grad").value); if (isNaN(k)) { alert('Gradul introdus nu este intreg, reintroduceti gradul matricii'); } if (k == 0) { alert ('Determinantul trebuie sa fie mai mare ca 1'); } if (k == 1) { alert ('Determinantul trebuie sa fie mai mare ca 1'); } return k; } function genTable(i,j) { //generates the table for the user to insert the values //var i,j = parseInt(document.getElementById("grad").value); var myTable = '&lt;TABLE BORDER="1" BORDERCOLOR="BLACK"&gt;\n &lt;TBODY&gt;\n'; for (i = 0; i &lt; 1; i++) { myTable += ' &lt;TR&gt;\n'; for (j = 0; j &lt; k+1; j++) { myTable += ' &lt;TD&gt;'+j+'&lt;/TD&gt;\n'; } myTable += ' &lt;/TR&gt;\n'; } for (i = 1; i &lt; k+1; i++) { myTable += ' &lt;TR&gt;\n'; for (j = 0; j &lt; 1; j++) { myTable += ' &lt;TD&gt;'+i+'&lt;/TD&gt;\n'; } for (j = 1; j &lt; k+1; j++) { myTable += ' &lt;TD&gt;&lt;input class="element" id="A' + i + j + '"value="0"&gt;&lt;/TD&gt;\n'; } myTable += ' &lt;/TR&gt;\n'; } myTable += ' &lt;/TBODY&gt;\n&lt;/TABLE&gt;\n'; document.getElementById('container').innerHTML = myTable; } function calcDet () { var A = []; //generates the array for (var i = 0; i &lt; k; i++) { A[i] = []; for (var j = 0; j &lt; k; j++) { A[i][j] = i + ':' + j; } } for (var i = 0; i &lt; k; i++) { //reads the array for (var j=0; j&lt;k; j++) { var id = "A" + (i + 1) + (j + 1); A[i][j] = parseFloat(document.getElementById(id).value); } } var s; var det = 0; function calcRec (A) { if (A.length == 1) { //bottom case of the recursive function alert (A) } if (i == 2) { det = A[0][0] * A[1][1] - A[1][0] * A [0][1]; alert (det); } for (var i=0; i&lt;k; i++) { //creates smaller matrix- values not in same row, column var smaller=new Array(A.length-1); for(h=0;h&lt;smaller.length;h++){ smaller[h]=new Array(smaller.length); } } for(a=1;a&lt;A.length;a++){ for(b=0;b&lt;A.length;b++){ if(b&lt;i){ smaller[a-1][b]=A[a][b]; } else if(b&gt;i){ smaller[a-1][b-1]=A[a][b]; } } } if (i%2==0) { s=1; } else { s=-1; } det+=s*matrix[0][i]*(calcRec(smaller)); return (det); } } &lt;/script&gt; &lt;body style="background-color: #777; color: ddd;"&gt; &lt;div style="margin: 20px;"&gt; &lt;h1&gt;Program de calculare determinant matrice de orice grad.&lt;/h1&gt; &lt;/div&gt; &lt;div&gt; Introduceti gradul matricei &lt;input id="grad" type="text" value="" style="width: 50px;" onChange="readGrad()"&gt; &lt;input style="margin-top: 20px;" type="button" name="Calculate determinant" value="Generati tabel" onClick="genTable()"&gt; &lt;/div&gt; &lt;form name="Det Matrice"&gt; &lt;div style="margin-top: 100px; float: left; width: 100%;"&gt; Introduceti valorile: &lt;table style="text-align: center;"&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/table&gt; &lt;br&gt; &lt;/div&gt; &lt;div style="float: left; margin-top: 20px;"&gt; &lt;input type="button" name="Calculate determinant" value="Calculati determinant" onClick="calcDet()"&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload