Click here to Skip to main content
11,713,482 members (83,319 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML
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:
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 :
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]

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 29-Mar-13 20:04pm

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

  Print Answers RSS
0 CHill60 335
1 Sergey Alexandrovich Kryukov 285
2 OriginalGriff 200
3 Maciej Los 145
4 Andy Lanng 140
0 CHill60 335
1 Sergey Alexandrovich Kryukov 320
2 OriginalGriff 225
3 Andy Lanng 165
4 Richard MacCutchan 150


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100