|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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! |
|
#2
|
||||
|
||||
|
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.” |
|
#3
|
||||
|
||||
|
Quote:
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/ |
|
#4
|
||||
|
||||
|
Quote:
What another site you mean? mathbin? Sorry, but I consider it a pretty helpful page... Quote:
I know that - that's why I put 'loop' in the title of this topic ![]() But I don't know how to do it... |
|
#5
|
|||
|
|||
|
please remove this post
|
|
#6
|
|||
|
|||
|
Quote:
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. |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
|||
|
|||
|
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? ![]() |
|
#9
|
||||
|
||||
|
Quote:
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. |
|
#10
|
||||
|
||||
|
It helps to know what you want to do. Tough beans, I know. Ask Mom, she'll make you a man or die trying.
|
|
#11
|
|||
|
|||
|
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;
}
|
|
#12
|
||||
|
||||
|
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. |
|
#13
|
|||
|
|||
|
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)? |
|
#14
|
|||
|
|||
|
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 ???
|
|
#15
|
|||
|
|||
|
It's here: mathbin.net/35392
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > [C] sum of the series - loop? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|