Click here to Skip to main content
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 21:04pm

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2014
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