NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
October 21, 2020, 07:05:42 am

 Pages: [1]   Go Down
 Author Topic: how to find the sum of an array  (Read 12529 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message on: December 18, 2008, 09:35:15 pm » posted from:Singapore,,Singapore

For example, in my newtons cradle applet,

Code:
for (int i=0; i<n; i++) {
KE[i] = 0.5*m[i]*L*L*omega[i]*omega[i];
}

ketotal = KE[0]+KE[1]+KE[2]+KE[3]+KE[4]+KE[5]+KE[6]+KE[7];  // why this line of code prevents the applet for running.
I want to show conservation of energy of the system but i am only able to find individual array value now......strange

any tips?
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #1 on: December 18, 2008, 09:44:22 pm » posted from:Singapore,,Singapore

nevermind, careless mistake

n is 0 to 6,

so when there was a KE[7] the EJS cannot compile the applet
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #2 on: December 18, 2008, 10:02:37 pm »

I will change the code to
(assume n=7)
ketotal=0;
for (int i=0; i<n;i++){// i++ means i=i+1;
KE = 0.5*m*L*L*omega*omega;
ketotal=ketotal+KE; // add energy from each one -- can be written as ketotal+=KE;
}
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #3 on: December 18, 2008, 10:21:29 pm » posted from:Singapore,,Singapore

that was what i was thinking

Code:
for (int i=0; iketotal = ketotal+KE[i];
}

but it did a cumulative sum.

until i saw your example, i now realise i need to reset to zero by using
Code:
ketotal = 0;

thanks.

think a long time didnt realise need programming logic, kept thinking what was wrong?  ARRGGHH

so it is cool now
Code:
ketotal = 0 ;
for (int i=0; iketotal = ketotal+KE[i];
}

prof u need to save for me the post for the '++' to show
 « Last Edit: December 18, 2008, 10:25:48 pm by lookang » Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #4 on: December 18, 2008, 11:39:20 pm »

I have told you before. Because I allow you to add html code into the message.

And all the html code starts with < end with >
when you type i<n , the forum system will think you want a html command (actually an invalid html command). So nothing show up.

If you want to display <  you need to type &lt;
If you want to display > you need to type &gt;

To display for(int =0;i<n;i++) you need to type for(int i=0;i&lt;n;i++)
Another question for you: How did I display the above syntax when I edit it?

 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #5 on: December 19, 2008, 07:51:37 am » posted from:Singapore,,Singapore

oic.... it is the <> brackets.
(int i=0;i(int i=0;i(int i =0;i(int i=0;i(int i=0;i
nvm it is a small thing, more impt is learn to use EJS
 « Last Edit: December 19, 2008, 10:23:09 am by lookang » Logged
 Pages: [1]   Go Up