Javascript help: arrays and a matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elizarod
    New Member
    • Nov 2011
    • 3

    Javascript help: arrays and a matrix

    I'm in an introductory course, and my professor is not very good. I've been working on this problem, but as of now, I'm stuck. Essentially, we have to declare a matrix of grades and create a "Z" formation. Using the "Z" locations we have to average and print grades. Here are the parameters:

    Declare an 8 x 8 matrix and an array of length 22.
    (declare any other variable that you use in the algorithm)

    1.- Populate the matrix.
    2.- Copy the elements of the first row of the matrix, the anti-diagonal, and the last row of the matrix into the array.
    (we call this in class the Z in the matrix)
    3.- Sort the array.
    4.- Assuming that the data in the arrays are grades, compute the average of the grades stored in the even locations of the array.
    5.- Copy the array back into the matrix.(back into the Z)
    6.- Print out the matrix values.

    This is what I have so far:

    Code:
    Var k=0;
    Var j=0;
    Var i=0;
    Var x=0;
    
    for (k=0;k<8;k++){
    x[j]=mat[i][k]
    j=j+1
    }
    
    var x = new array (22);
    j=8;
    k=j;
    for(i=1;i<7;i++){
    k[j]mat[i][k];
    j=j+1;
    }
    
    var mat=new array (8);
    i=7
    for(i=0;1<21;i=i+1){
    for(k=i+1;k<22;k=k+1){
    if(x[i]>x[k]){
    temp=x[i];
    x[i]=x
    x[k]=temp
    }
    }
    }
    x[i]=mat[i][k];
    mat[i][k]=x[j]
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    which part of the question belongs that code to? I can’t make any sense of what is written there.

    1) is it up to you how to fill the matrix (random values, …) ?
    2) Array.concat() comes in handy, but at least one for() loop will be required.
    3) look up array sorting in your favourite reference work.
    4) quite as simple as 3) as long as you know how to determine "even".
    5) enter Array.splice(). essentially the reverse of 2).
    6) there is a plethora of ways, depending on how it should look (optical & code-wise).

    PS. I had averages between 3.7 and 4.9 (random values 0 to 9)
    PPS. read up on array methods, they are really useful here.
    PPPS. this hardest thing is to figure out, what to do in principle to achieve the tasks (i.e. programme logic), the coding itself is quite easy
    Last edited by Dormilich; Nov 3 '11, 04:20 PM.

    Comment

    • elizarod
      New Member
      • Nov 2011
      • 3

      #3
      all of these are a great help, but as for number 4, no, i'm not sure how determine even numbers. suggestions?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        i'm not sure how determine even numbers.
        how would you personally determine, whether a number is even? (like in Math course)

        Comment

        Working...