Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 19th, 2008, 6:36 PM   #1
Chem.E
Newbie
 
Join Date: Aug 2008
Posts: 10
Rep Power: 0 Chem.E is on a distinguished road
C Code

Hey guys, here's a question I have. I've not taken C or C++, but I have had Fortran. Any help in corrections would be appreciated:

Question: Develop the code to compute the sum of integers from 1 to 10,000?

Here is what I have:

int sum = 0, i;

for( i = 1; i <= 10000; i++ ) { sum = sum + i; }

printf( "The sum of integers 1-10000 is: %d", sum );
Chem.E is offline   Reply With Quote
Old Aug 19th, 2008, 7:13 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 655
Rep Power: 4 Jessehk is on a distinguished road
Re: C Code

With actually saving it to a file and compiling, the code you posted looks correct.
There is a better way, though.

\sum_{k=1}^n k = 1 + 2 + 3 + 4 + ... + n = \frac{n\left(n + 1\right)}{2}

Does that help at all? If you have trouble translating the arithmetic to C, post your problem and we'll try to help.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Aug 19th, 2008, 7:39 PM   #3
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 87
Rep Power: 1 JD-Salinger is on a distinguished road
Re: C Code

what equation is that? dont know math
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old Aug 19th, 2008, 8:59 PM   #4
Chem.E
Newbie
 
Join Date: Aug 2008
Posts: 10
Rep Power: 0 Chem.E is on a distinguished road
Re: C Code

Thanks so much, Jess. Yeah it makes sense, I actually used that fact in writing my program in Fortran!
Chem.E is offline   Reply With Quote
Old Aug 19th, 2008, 9:50 PM   #5
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 591
Rep Power: 5 Benoit is on a distinguished road
Re: C Code

Quote:
Originally Posted by JD-Salinger View Post
what equation is that? dont know math
That big thing that looks like an E, or a sideways W, or a sideays M, maybe even a C, that's the Greek letter Sigma, its used in math to denote summation: http://en.wikipedia.org/wiki/Summation
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Aug 20th, 2008, 9:56 AM   #6
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 87
Rep Power: 1 JD-Salinger is on a distinguished road
Re: C Code

which is equal to n(n+1)/2 . . . . ill look for the proof... hehehehe
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incorporating javascript code into java application csrocker101 Java 1 Feb 11th, 2008 7:36 AM
Viewing VB's Automated code john Wesley Visual Basic .NET 3 Jun 8th, 2006 6:37 AM
FTP and return code fetching Serinth C 2 May 29th, 2006 12:05 AM
String to Morse Code - Copying from string to string Xenon C 29 Nov 10th, 2005 3:30 PM
Help with code please LiverLad Java 0 Feb 9th, 2005 11:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC