Write a program that utilises a 8x8 2-dimensional array.
(a) Initialise the array elements via nested for loops as follows
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
...
...
57 58 59 60 61 62 63 64
(b) Add some code that will display all array elements in an 8x8 HTML table.
(c) Use nested for loops for calculating the sum and the average of the
values stored in the array.
Let a function display these values on screen eg use alert().
我到目前为止的代码是:
x = matrix( 8 , 8, 0 ); // 8 lines, 8 cols filled with empty string
function matrix( rows, cols, defaultValue){
var arr = [];
// Creates all lines:
for(var i=0; i < rows; i++){
var add = 1
// Creates an empty line
arr.push([]);
// Adds cols to the empty line:
arr[i].push( new Array(cols));
for(var j=0; j < cols; j++){
// Initializes:
arr[i][j] = defaultValue + add;
}
var add = add + 1
}
return arr;
}
function displayInDiv() {
var output_string_ = "" ;
var lastElement = 64 ;
output_string_ = output_string_
+'
'
+'
'
+'
ARRAY INDEX
'
+'
array_1
'
+'
array_2
'
+'
array_3
'
+'
array_4
'
+'
array_5
'
+'
array_6
'
+'
array_7
'
+'
array_8
'
+'
'
;
for ( var i = 1 ; i < 9 ; i++ ) {
for ( var j = 0 ; j < 7 ; j++ ) {
output_string_ = output_string_
+'