why does this not work? how do I fix it?

G

greenflame

a = [
[1,2],
[3,4]
]
b = [
[5,6],
[7,8]
]
function addarr(A,B) {
C = A;
for (i=0;i<A.length;i++) {
for (j=0;j<A[0].length;j++) {
C[j] = A[j] + B[j];
}
}
}
function show(input) {
for (i=0;i<input.length;i++) {
switch (i) {
case 0:
document.write("<font face=symbol>é</font>");
break;
case input.length-1:
document.write("<font face=symbol>ë</font>");
break;
default: document.write("<font face=symbol>ê</font>");
}
for (j=0;j<input.length-1;j++) {
document.write(input[j] + " ");
}
document.write(input[input.length-1]);
switch (i) {
case 0:
document.write("<font face=symbol>ù</font>");
break;
case input.length-1:
document.write("<font face=symbol>û</font>");
break;
default: document.write("<font face=symbol>ï</font>");
}
document.write("<br>");
}
}
c = addarr(a,b);
show(c);

This code shows absolutely nothing. This code is supposed to show the
sum of the following matricies.

a = [1 2]
[3 4]

b = [5 6]
[7 8]

The sum of two matricies is the sum of the corresponding elements. Thus
the matrix c should be:

c = [6 8 ]
[10 12]
 
G

greenflame

I forgot to mention that the show function works all by it self. I have
no idea how to test the addarr function. Also the wierd characters in
the font tags are supposed to be peices of brackets. The characters
should have been of the format &#xxx where xxx is a number.
 
L

Lee

greenflame said:
a = [
[1,2],
[3,4]
]
b = [
[5,6],
[7,8]
]
function addarr(A,B) {
C = A;
for (i=0;i<A.length;i++) {
for (j=0;j<A[0].length;j++) {
C[j] = A[j] + B[j];
}
}
}
c = addarr(a,b);
show(c);


Your function addarr() doesn't return any value, so "c = addarr(a,b)"
does nothing.
Also note that within addarr(), when you set "C = A", you are creating a global
variable C that points to the global variable a. You are not creating a new
array.

You would probably see the results you expect with:

addarr(a,b);
show(c);

Although I haven't looked for errors in show()
 
L

Lee

Lee said:
greenflame said:
a = [
[1,2],
[3,4]
]
b = [
[5,6],
[7,8]
]
function addarr(A,B) {
C = A;
for (i=0;i<A.length;i++) {
for (j=0;j<A[0].length;j++) {
C[j] = A[j] + B[j];
}
}
}
c = addarr(a,b);
show(c);


Your function addarr() doesn't return any value, so "c = addarr(a,b)"
does nothing.
Also note that within addarr(), when you set "C = A", you are creating a global
variable C that points to the global variable a. You are not creating a new
array.

You would probably see the results you expect with:

addarr(a,b);
show(c);


Sorry, I meant:
show(a);
 
R

RobG

greenflame wrote:

Please don't post code with tabs, replace them with 2 or 4 spaces
before posting.
a = [
[1,2],
[3,4]
]
b = [
[5,6],
[7,8]
]
function addarr(A,B) {
C = A;

This makes 'C' a reference to the object passed to the function and
given the name 'A'. 'C', 'A' and 'a' now all point to the same object
(the one you defined up above as 'a').
for (i=0;i<A.length;i++) {
for (j=0;j<A[0].length;j++) {
C[j] = A[j] + B[j];
}


This does the addition OK, but you are modifying the original array,
not the new one you think you created. You are also depending on
global variables to transmit the results, you don't return anything.

Getting the length of the array on every loop is slow, so get the
length once. If you can also tie that up with doing the row/column
reference and a do..while loop, things will really hum.

Try:

function addarr(A,B) {

// Create a new object 'X'
var X = [];
var i = 0;
var j;

do {

// Reset column counter
j = 0;

// For every row of A, create a row for X
X = [];

do {

// For every element in A, do addition
X[j] = A[j] + B[j];

// Must use undefined here or '0' may cause premature exit
// Combine test with column increment
} while ( undefined != A[++j] )

// Combine test with row increment
} while ( A[++i] )

// Return the result to the calling function
return X;
}
}
}
function show(input) {
[...]

Ideally, you should ensure that A and B have the same dimensions before
calling the function and that they are in fact matrices or vectors and
not scalar numbers.
 
G

greenflame

Rob is this what you mean? If so then this showed nothing.
a = [
[1,2],
[3,4]
]
b = [
[5,6],
[7,8]
]
function addarr(A,B) {
C = [];
i=0;
j=0;
Al = A.length;
A0l = A[0].length;
do {
j = 0;
C = new Array(A0l);
do {
C[j] = A[j] + B[j];
} while (undefined != A[j++])
} while (A[i++])
return C;
}
function show(input) {
for (i=0;i<input.length;i++) {
switch (i) {
case 0:
document.write("<font face=symbol>é</font>");
break;
case input.length-1:
document.write("<font face=symbol>ë</font>");
break;
default: document.write("<font face=symbol>ê</font>");
}
for (j=0;j<input.length-1;j++) {
document.write(input[j] + " ");
}
document.write(input[input.length-1]);
switch (i) {
case 0:
document.write("<font face=symbol>ù</font>");
break;
case input.length-1:
document.write("<font face=symbol>û</font>");
break;
default: document.write("<font face=symbol>ï</font>");
}
document.write("<br>");
}
}
c = addarr(a,b);
show(c);
 
G

greenflame

Also how do I fix the fact the all the numbers may not be of the same
length? I would like the numbers to be aligned to the right if
possible. For example:

[ 10 6 456 ]
[ 2 7 23 ]
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 28 May 2005 16:40:51, seen in
greenflame said:
Also how do I fix the fact the all the numbers may not be of the same
length? I would like the numbers to be aligned to the right if
possible. For example:

[ 10 6 456 ]
[ 2 7 23 ]

Please quote correctly :
For proper quoting when using Google for News :-
Keith Thompson wrote in comp.lang.c, message ID
<[email protected]> :-
If you want to post a followup via groups.google.com, don't use
the "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.

The answer to your question should be obvious from part of FAQ 4.6.
 
R

RobG

greenflame said:
Rob is this what you mean? If so then this showed nothing.
a = [
[1,2],
[3,4]
]
b = [
[5,6],
[7,8]
]
function addarr(A,B) {
C = [];
i=0;
j=0;
Al = A.length;
A0l = A[0].length;

You don't need either A1 or A01, so you can delete those lines.
do {
j = 0;
C = new Array(A0l);


Just create C as a new array, no need to specify the length, new
elements are added below.

C = [];
do {
C[j] = A[j] + B[j];
} while (undefined != A[j++])


There is a big difference between ++j and j++. ++j will increment j
before the while is evaluated, j++ will increment it afterward.

Here you are post-incrementing j. As a result, when j gets to 1 (which
will be the last element in the array), the while will evaluate to
true. But then j is incremented, so the loop is attempted with j=2.

There is no element and the script fails.

You need to pre-increment j so that the while tests for the next
element.

} while ( undefined != A[++j] )

Now when j=1, the while will look for j=2, which will return undefined
and the loop will end gracefully.

} while (A[i++])

Same here

} while (A[++i])
return C;
}
function show(input) {
for (i=0;i<input.length;i++) {
switch (i) {
case 0:
document.write("<font face=symbol>é</font>");

The font element is depreciated and the symbol used here does not, on
my system, represent a '[' which is what I think you are after.

[...]

Below is a function that will pre-pend spaces to an integer up to the
length specified. Read the FAQ entry suggested by Dr J for a more
complete solution.

During your add function, you may want to find out the longest number
and use that for the number of spaces to be filled. That will give all
numbers the same width (I've just guessed at 3) if you want to work out
a different length for each column, you'll have a more complex
algorithm.


function show(input) {
var j, i=0;
var t=[];
do {
t.push('[ ');
j=0;
do {
t.push( intLen(input[j], 3) + ' ' );
} while ( input[++j] )
t.push(' ]<br>');
} while ( input[++i] )
document.write(
'<pre>',
t.join(''),
'<\/pre>'
);
}

// Make integer x length i
function intLen(x, i){
x += '';
while (x.length < i) {
x = ' ' + x;
}
return x;
}
 
G

greenflame

I tryed the intLen function and it did not add the spaces. I tested the
function from the addarr function and it worked. Here is my test
script:

function show(input) {
var j, i=0;
var t=[];
do {
t.push('[ ');
j=0;
do {
t.push( intLen(input[j], 3) + ' ' );
} while ( input[++j] )
t.push(' ]<br>');
} while ( input[++i] )
document.write(
'<pre>',
t.join(''),
'<\/pre>'
);
}

// Make integer x length i
function intLen(x, i) {
while (x.length < i) {
x = ' ' + x;
}
return x;
}
c = [
[2.3,2],
[3,456]
];
a = 3;
b = intLen(a,5);
document.write("ZZZZZZZZ<br>");
document.write(b);
 
R

RobG

greenflame said:
I tryed the intLen function and it did not add the spaces. I tested the
function from the addarr function and it worked. Here is my test
script:

intLen() expects a string, which is what intLen sends it. But there
are two issues (see below).

[...]
// Make integer x length i
function intLen(x, i) {
while (x.length < i) {
x = ' ' + x;
}
return x;
}
c = [
[2.3,2],
[3,456]
];
a = 3;
b = intLen(a,5);
document.write("ZZZZZZZZ<br>");
document.write(b);

1. intLen() is sent a number, so the 'while' is never true (numbers
don't have a length property). So ensure 'x' is a string before
getting its length and all is sweet again.

function intLen(x, i) {
x += ''; // Converts x to a string
while (x.length < i) {
x = ' ' + x;
}
return x;
}

2. The document.write statement just splats 'b' into the page. All
browsers do automatic whitespace removal unless told to do otherwise.
Any number of spaces, tabs, newlines, etc. are reduced to a single
space so inserting extra spaces has no visible effect.

The (simple) fix is to wrap 'b' in a <pre> element. <pre> elements
preserve whitespace and use a mono-spaced font, so ' 3' looks different
to ' 3'. An alternative would be to replace all the spaces with
non-breaking spaces '&nbsp;', but using <pre> is simpler.

Here's your test:

function intLen(x, i) {
x += '';
while (x.length < i) {
x = ' ' + x;
}
return x;
}

a = 3;
document.write('<pre>a = ' + intLen(a, 5) + '<pre>');
 
G

greenflame

I used the &nbsp method and I have this new intLen function and it
works. Yay! Thank you.

function intLen(x, i) {
x += '';
j=6*(i-x.length);
while (x.length < j) {
x = '&nbsp;' + x;
}
return x;
}
 
G

greenflame

ok heres the problem. I have been trying to work on a script to align
the numbers in the matrix column by column. I have the following
script. It is supposed to show someting like the following ouput:

[2.3 4 456]
[3 23 5.3]


[3 1 3]
[1 2 3]


[3 1]
[1 2]
[3 3]

[3 2 3]

but it shows [3 undefined undefined] instead of the [3 2 3]. I dont
know what is wrong. :(

<script>
function dims(input) {
return [input.length,input[0].length]
}
function create1darr(length) {
output = new Array(length);
return output;
}
function create2darr(rows,cols) {
output = new Array(rows);
for (i=0;i<rows;i++) {
output = new Array(cols);
}
return output;
}
function copy1darr(input) {
output = create1darr(input.length);
for (i=0;i<input.length;i++) {
output = input;
}
return output;
}
function copy2darr(input) {
output = create2darr(input.length,input[0].length);
for (i=0;i<input.length;i++) {
for (j=0;j<input[0].length;j++) {
output[j] = input[j];
}
}
return output;
}
function findmax(input) {
Temp = copy1darr(input);
for (i=0;i<Temp.length;i++) {
for (j=0;j<Temp.length-1;j++) {
Temp[j] = Math.max(Temp[j],Temp[j+1]);
}
}
return Temp[0];
}
function intLen(x, i) {
x += '';
j=6*(i-x.length);
while (x.length < j) {
x = '&nbsp;' + x;
}
return x;
}
function show(input) {
if (input[0].length == undefined) {
document.write("<b>[</b>");
for (i=0;i<input.length-1;i++) {
document.write(input + " ");
}
document.write(input[input.length-1] + "<b>]</b><br>");
}
if (input[0].length != undefined) {
for (i=0;i<input.length;i++) {
switch (i) {
case 0:
document.write("<font face=symbol>é</font>");
break;
case input.length-1:
document.write("<font face=symbol>ë</font>");
break;
default: document.write("<font face=symbol>ê</font>");
}
for (j=0;j<input.length-1;j++) {
document.write(input[j] + " ");
}
document.write(input[input.length-1]);
switch (i) {
case 0:
document.write("<font face=symbol>ù</font>");
break;
case input.length-1:
document.write("<font face=symbol>û</font>");
break;
default: document.write("<font face=symbol>ï</font>");
}
document.write("<br>");
}
}
}
a = [
[2.3,4,456],
[3,23,5.3]
];
S = create2darr(a.length,a[0].length);
L = create2darr(a.length,a[0].length);
A = create2darr(a.length,a[0].length);
T = create2darr(a[0].length,a.length);
M = create1darr(T.length);
for (i=0;i<a.length;i++) {
for (j=0;j<a[0].length;j++) {
S[j] = a[j].toString();
L[j] = S[j].length;
T[j] = L[j];
}
}
//thus T represents the ith column of L
for (i=0;i<M.length;i++) {
M = findmax(T);
}
show(a);
document.write("<br><br>");
show(L);
document.write("<br><br>");
show(T);
document.write("<br><br>");
show(M);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top