Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
3.89/5 (2 votes)
See more:
How to input values to 2D array using two for loops and get back using same way.. (I am going to code a simulator for matrices addition)...

I made matrices by using tables and text-boxes
this is my code:

declaration:
JavaScript
var matA = new Array(2);
var matB = new Array(2);
var matAns = new Array(2);

for(i=0; i<2; i++)
{
    matA[i] = new Array(2);
    matB[i] = new Array(2);
    matAns[i] = new Array(2);
}


Code (Input Data form :
JavaScript
function getValueFromMatA()
{
   for(r=0; r<2; r++)
   {
        for(c=0; c<2; c++)
        {
              matA[r,c]=parseInt(document.getElementById('nodeA'+r+''+c).value);
              //getting values from each text-box in matrix(table)
        }
   }
}

function getValueFromMatB()
{
    for(r=0; r<2; r++)
    {
        for(c=0; c<2; c++)
        {
              matB[r,c]=parseInt(document.getElementById('nodeB'+r+''+c).value);
              //getting values from each text-box in matrix(table)
        }
    }
}


Code: Output(In this i can't get matAns[0,0] value and matAns[0,1] value. it gives me always matAns[1,0] value and matAns[1,1]

JavaScript
function addMaAMatB()
{
   for(r=0; r<2; r++)
   {
       for(c=0; c<2; c++)
       {
            matAns[r,c] = matA[r,c]+matB[r,c];
       }
   }
}
function printMatAns()
{
    for(r=0; r<2; r++)
    {
        for(c=0; c<2; c++)
        {
            alert(matAns[r,c]);
        }
    }
}


Please help me... where am I wrong??
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900