473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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[i][j] = A[i][j] + B[i][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[i].length-1;j++) {
document.write(input[i][j] + " ");
}
document.write(input[i][input[i].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]

Jul 23 '05 #1
14 1450
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.

Jul 23 '05 #2
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[i][j] = A[i][j] + B[i][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()

Jul 23 '05 #3
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[i][j] = A[i][j] + B[i][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);

Jul 23 '05 #4
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[i][j] = A[i][j] + B[i][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[i] = [];

do {

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

// Must use undefined here or '0' may cause premature exit
// Combine test with column increment
} while ( undefined != A[i][++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.
--
Rob
Jul 23 '05 #5
ok thanks all I will try all the sugestions and report back.

Jul 23 '05 #6
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[i] = new Array(A0l);
do {
C[i][j] = A[i][j] + B[i][j];
} while (undefined != A[i][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[i].length-1;j++) {
document.write(input[i][j] + " ");
}
document.write(input[i][input[i].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);

Jul 23 '05 #7
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 ]

Jul 23 '05 #8
JRS: In article <11*********************@g47g2000cwa.googlegroups. com>,
dated Sat, 28 May 2005 16:40:51, seen in news:comp.lang.javascript,
greenflame <al*********@yahoo.com> posted :
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
<ln************@nuthaus.mib.org> :-
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9
greenflame wrote:
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[i] = new Array(A0l);
Just create C[i] as a new array, no need to specify the length, new
elements are added below.

C[i] = [];
do {
C[i][j] = A[i][j] + B[i][j];
} while (undefined != A[i][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[i][++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[i][j], 3) + ' ' );
} while ( input[i][++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;
}

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

--
Rob
Jul 23 '05 #10
Ok. Thanks to all I will try things out and see what happens. :)

Jul 23 '05 #11
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[i][j], 3) + ' ' );
} while ( input[i][++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);

Jul 23 '05 #12
greenflame wrote:
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>');


--
Rob
Jul 23 '05 #13
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;
}

Jul 23 '05 #14
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[i] = new Array(cols);
}
return output;
}
function copy1darr(input) {
output = create1darr(input.length);
for (i=0;i<input.length;i++) {
output[i] = input[i];
}
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[i][j] = input[i][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[i] + " ");
}
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[i].length-1;j++) {
document.write(input[i][j] + " ");
}
document.write(input[i][input[i].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[i][j] = a[i][j].toString();
L[i][j] = S[i][j].length;
T[j][i] = L[i][j];
}
}
//thus T[i] represents the ith column of L
for (i=0;i<M.length;i++) {
M[i] = findmax(T[i]);
}
show(a);
document.write("<br><br>");
show(L);
document.write("<br><br>");
show(T);
document.write("<br><br>");
show(M);
</script>

Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
0
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.