NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
January 29, 2020, 10:52:55 pm

Progress is impossible without change; and those who cannot change their minds cannot change anything. ..."George Bernard Shaw(1865-1950, Irish dramatist, essayist and critic, Nobel Prize for Literature 1925)"

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

Offline

Posts: 1793

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: 1793

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: 3085

 « 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: 1793

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: 3085

 « 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: 1793

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
Progress is impossible without change; and those who cannot change their minds cannot change anything. ..."George Bernard Shaw(1865-1950, Irish dramatist, essayist and critic, Nobel Prize for Literature 1925)"

 Related Topics Subject Started by Replies Views Last post Find the fastest Path Optics Fu-Kwun Hwang 6 109309 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 6332 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 15958 May 14, 2010, 11:22:41 pm by Fu-Kwun Hwang Find the fastest Path optics ahmedelshfie 1 5496 October 01, 2010, 12:09:58 am by ahmedelshfie Find out your blink spot region illusion Fu-Kwun Hwang 0 3961 January 04, 2011, 09:27:03 am by Fu-Kwun Hwang
Page created in 0.055 seconds with 22 queries.since 2011/06/15