NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
November 18, 2017, 02:41:15 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
"Make priority, take action and start now." ...Wisdom
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

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

Posts: 1784


http://weelookang.blogspot.com


WWW
«
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? Grin
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
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,

ketotal = KE[0]+KE[1]+KE[2]+KE[3]+KE[4]+KE[5]+KE[6] works already.

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

Posts: 3080



WWW
«
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 Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
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 Angry

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 Embarrassed
« Last Edit: December 18, 2008, 10:25:48 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
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 Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
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 Grin
« Last Edit: December 19, 2008, 10:23:09 am by lookang » Logged
Pages: [1]   Go Up
  Print  
"Make priority, take action and start now." ...Wisdom
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
Find the fastest Path
Optics
Fu-Kwun Hwang 6 105599 Last post October 02, 2010, 01:18:23 am
by buymmlensf
how to view the data values assigned into a array that is [5]*[5] for example.
Questions related to EJS
lookang 1 5640 Last post March 30, 2009, 05:54:58 pm
by Fu-Kwun Hwang
how make loop for array for y coordinates?
Questions related to EJS
lookang 14 12440 Last post May 14, 2010, 11:22:41 pm
by Fu-Kwun Hwang
Find the fastest Path
optics
ahmedelshfie 1 4578 Last post October 01, 2010, 12:09:58 am
by ahmedelshfie
Find out your blink spot region
illusion
Fu-Kwun Hwang 0 3338 Last post January 04, 2011, 09:27:03 am
by Fu-Kwun Hwang
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.059 seconds with 22 queries.since 2011/06/15