C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 23rd, 2009, 05:48 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
[C] sum of the series - loop?

Hi guys.

I have to calculate the sum of the following series: mathbin.net/35392 with the precision of 10^-8

I need to use the following approximation: mathbin.net/35394 while the stopping criterion should be: mathbin.net/35396

The next partial sum should be calculated from the previous one.

The program should be pretty similar to electrofriends.com/source_codes/C/sine.html however here it's the user who enters the number of terms.

How to do this with the other way?

Thanks!

Reply With Quote
  #2  
Old October 23rd, 2009, 06:12 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Click here for more information.
 
Join Date: Jan 2005
Location: Internet
Posts: 6,932 gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)gimp User rank is General 41st Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time spent in forums: 3 Months 18 h 13 m 23 sec
Reputation Power: 3729
Send a message via AIM to gimp
You'll get a lot more responses if you take the time to write out your problem without referencing another site.
__________________
Chat Server Project & Tutorial | WiFi-remote-control sailboat (planned) | Joke Thread
“Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”

Reply With Quote
  #3  
Old October 23rd, 2009, 06:16 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,530 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 58 m 20 sec
Reputation Power: 603
Quote:
Originally Posted by jacus
How to do this with the other way?


What other way?

You can calculate the sum of a series either using a loop or a recursive function. The loop is the safest and probably the fastest approach.

You should read this:

http://forums.devshed.com/c-program...rst-259106.html
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
  #4  
Old October 23rd, 2009, 06:50 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by gimp
You'll get a lot more responses if you take the time to write out your problem without referencing another site.


What another site you mean? mathbin? Sorry, but I consider it a pretty helpful page...


Quote:
Originally Posted by jwdonahue
The loop is the safest and probably the fastest approach.


I know that - that's why I put 'loop' in the title of this topic

But I don't know how to do it...

Reply With Quote
  #5  
Old October 23rd, 2009, 06:51 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
please remove this post

Reply With Quote
  #6  
Old October 23rd, 2009, 08:06 PM
Oler1s Oler1s is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2006
Posts: 1,989 Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 5 Days 14 h 36 m 28 sec
Reputation Power: 1083
Quote:
But I don't know how to do it...
Let's talk about problem solving. So you don't know how to write this loop in particular. Ok, then set aside that issue.

What about writing a simpler loop? Can you write a loop that does something much much simpler? Like saying "hello, world" repeatedly?

If you cannot, why not?
__________________
When you ask a question, be prepared to tell us: what have you tried? If you think you don't need to try anything, we will never be interested in helping you. If you agree with the link, and you refuse to answer that question, you are being a hypocrite.

Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
Want better answers? Tell us what you Googled for and what steps you took to answer your own question.

Reply With Quote
  #7  
Old October 23rd, 2009, 09:10 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,783 sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 11 h 24 m 8 sec
Reputation Power: 4482
How close is close enough?
__________________
Write no code whose complexity leaves you wondering what the hell you did.
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

Reply With Quote
  #8  
Old October 24th, 2009, 07:14 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
OK, so far i have written this:

Code:
#include <stdio.h>
#include <math.h>

int main()
    { 
    float sum,epsilon,x,elem;
    int k;

    sum=0;
	
    do
    {
    printf("Enter the value of x");
    scanf("%d", &x);
    if (x<(3.14)) &&  (x>(3.14));
    {
    printf("X must be between -pi and pi");
    }
    
     


Could you help me and tell what to do next?

Reply With Quote
  #9  
Old October 24th, 2009, 07:51 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,530 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 58 m 20 sec
Reputation Power: 603
Quote:
Originally Posted by jacus
Could you help me and tell what to do next?


Think about your goal. Write down the steps (in your native language) required to attain your goal, then write some more code. Let us know if you encounter any difficulties with that.

The fragment you posted is not even compilable. Start compiling your code frequently as you work. That should flag any syntax errors as you go, allowing you to fix them before so many of them accumulate you become overwhelmed.

Reply With Quote
  #10  
Old October 24th, 2009, 07:54 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,783 sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 11 h 24 m 8 sec
Reputation Power: 4482
It helps to know what you want to do. Tough beans, I know. Ask Mom, she'll make you a man or die trying.

Reply With Quote
  #11  
Old October 25th, 2009, 12:32 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
I have been working a few hours on this. I think that it shows what I want to get. Help will be appreciated.

Code:
#include <stdio.h>
#include <math.h>

int main()
    { 
    float sum,x,element;
    int n;
    
    n=1;
    sum=0;
    
    while ((x < 3.14) || (x>3.14))
    {
    printf("Enter the value of x. It must be between -pi and pi");
    scanf("%f", &x);
    }
    
    element=sin(x); //first element
    sum=element;
    
    while (element>1e-8)
    {
    n+1
    element=(sin(n*x))/(n^3);
    printf("Elements %d: %.10f sum:%.10f n:%d", n,element,sum,n);
    sum=element+sum;
    }
    
    
    return 0;
    }

Reply With Quote
  #12  
Old October 25th, 2009, 03:25 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,530 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 58 m 20 sec
Reputation Power: 603
You haven't chosen the one true brace and indentation style. Other than that, is there something we can help you with?

Still haven't read the How to post a question sticky have you? It is required that you read, understand and make an effort to follow its guidelines.

We are not mind readers. We don't know what "help" means to you at this juncture. If you are to become a programmer (unlikely as that seems to be), you will have to learn to communicate effectively. That is after all, a major part of programming. First you acquire domain knowledge and then you document the captured knowledge in the form of source code and other artifacts such as requirements and design documentation.

In order to acquire domain knowledge you must learn to do research. This includes asking questions. But not just any question; "can you help me please?" is a poor question because most of us can simply answer "yes". Does that solve your problem? No. You have to think about your goal and then work out how to go about reaching it. If you tell us; "the sky is blue", we'll generally agree with you (depending on the weather perhaps), but if you wanted to find out why, then you have to ask; "why is the sky blue?". This is very elemental stuff. We're not carrying on casual conversations; there's other forums for that. We're talking science and engineering here, or should be.

You also have to consider that we are providing you a free service. You should do as much of the leg work as you possibly can. If there's a problem with your code, you should describe it to us and tell us what you think it should be doing. Tell us what you have done to diagnose the issue. We need information. Just laying your code out there and crying for help will just get you flamed. We don't have time for you. There are others with more promising futures that we want to see succeed. We want you to fail because you do not exhibit any of the natural attributes that make up a good scientist, engineer or programmer.

So, consider your goals. If/when you are motivated sufficiently, manifest those traits we wish to foster in our soon to be associates and peers.

Now assuming you intended to ask for a code review:

Your bracing style sucks, mostly because you are not consistent.

Your code will not compile as posted. You should have mentioned that you were having troubles getting it to compile and supplied the build logs to save us some time by helping us to zero in on the culprit.

Your implementation does not conform to the algorithm you said you were trying to implement. What exactly do you think n+1 does?

Last edited by jwdonahue : October 25th, 2009 at 03:28 PM.

Reply With Quote
  #13  
Old October 25th, 2009, 03:38 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
Again, I made some changes.

Code:
#include <stdio.h>
#include <math.h>

int main()
    { 
    float sum,x,element;
    int n;
    
    n=1;
    sum=0;
    
    do
    {
    printf("Enter the value of x. It must be between -pi and pi");
    scanf("%f", &x);
    }
    while (x < -atan(1)*4 || x>atan(1)*4);	 //atan(1)*4=3.14...
    
    element=sin(x);	//first element
    sum=element;
    
    while (element>1e-8 || element<-1e-8)
    {
    n=n+1;
    element=(sin(n*x))/pow(n,3);
    printf("Elements %d: %.10f sum:%.10f n:%d\n", n,element,sum,n);
    sum=element+sum;
    }
    
    
    return 0;
    }


I'm not completely sure if my program does what it should do. I'm pretty confused because I don't know even how to check it. I particularly mean: shouldn't it add AND substract next elements alternately, not just add (as I guess it is what is doing now)?

Reply With Quote
  #14  
Old October 25th, 2009, 03:55 PM
Nyan Nyan is online now
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 230 Nyan Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 Days 20 h 27 m 12 sec
Reputation Power: 0
I'm not quite clear what series you are trying to calculate. Describe in mathematical notation or pseudo code first. Is it sigma 1 to infinity sin(n*x)/n^3 ???

Reply With Quote
  #15  
Old October 25th, 2009, 03:59 PM
jacus jacus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 15 jacus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 42 m 5 sec
Reputation Power: 0
It's here: mathbin.net/35392

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [C] sum of the series - loop?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek