Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Sum of Series?

sum series loops

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 StainedSilva

StainedSilva

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 13 March 2015 - 12:52 AM

Hi Guys,

 

Stuck on a loop question..

 

Write a program to calculate and display the sum of the series:

 

1 - 1/2 + 1/3 - 1/4...

 

until a term is reached that is less than 0.0001

 

 

It might be that I have been up for hours and my brain is now dead, but I have no idea what Im supposed to do here.. 

 



#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 13 March 2015 - 09:38 AM

What a nice homework question!

Sadly, we don't answer homework question.

 

Tell us what you have at the moment, and we'll help you.


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 13 March 2015 - 11:29 PM

As Vaielab said, we help, but we don't sit and make your homework for you. So show us the code you've got so far and we will help you finish it.

 

BTW, do you know how to it in your mind? Because if that's the problem maybe you need to read a book or an article, else, is just translating your mind's procedure to code.



#4 StainedSilva

StainedSilva

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 14 March 2015 - 04:07 AM

Hi Guys,

Thanks for the response. See im basically doing a self study course and have never done any sort of programming prior.

I dont have a class or teacher, no support group or friends with the knowledge so I refer to forums for help and guidance.

I honestly dont know where to start with this question. I dont really understand in my mind what its asking as I havent seen an example in my book of this type before.

Regards,

Silva

#5 RadioSpace

RadioSpace

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 20 March 2015 - 04:50 PM

did ya get it yet?



#6 StainedSilva

StainedSilva

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 21 March 2015 - 05:49 AM

Not yet haha, well im getting a result but its not yielding an answer im looking for. Im tryung to get 0.0001.. but get everything but that.

This is what I have :

 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            int sign = -1;
            string symbol = "";
            double total = 0;
            int input = 0;
 
            //textBox1.Text += Convert.ToString(input);
            input = Convert.ToInt32(textBox1.Text);
 
            for (int x = 1; x <= input; x++)
            { 
            sign *= -1;
 
            if (sign == 1)
            {
                symbol = " + ";
            }
            else
            {
                symbol = " - ";
            }
            if (x == 1)
            {
                textBox1.Text += Convert.ToString(" 1 +");
            }
            else if (x == input)
            { 
            textBox1.Text += Convert.ToString(" (1 / " + x + ")");
            }
            else
            {
            textBox1.Text += Convert.ToString(" (1 / " + x + ") " + symbol);
            }
                total += sign * 1.0 / x;
            }
            MessageBox.Show(" The sum of the given series is :  " + Convert.ToString(total));
            }
        }
    }
 
 
 
I need to reach a term that is less then 0.0001


#7 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 05 April 2015 - 12:53 PM

1. Sum starts from 0.0
2. Each odd denominator means that the fraction is added, if the denominator is even (divisibile by 2), then the fraction is added to the sum
3. A term is not less than 0.0001 until the fraction is smaller than 1/10000, thus the denominator is greater than 10,000 (much easier than doing floating point comparison of the division result, but C# has the System.Decimal type, which would be good to use here.)
(NOTE: The keyword here is less than 0.0001, meaning a term of 0.0001 is still valid because it's equal, not less than)

 

This is a simple assignment. Some pseudocode:

for denominator [1 .. <= 10000]
{
  term = 1.0 / denominator;
  if (even(term)) {
    sum = sum - term;
  } else {
    sum = sum + term
  }
}

Edited by AceInfinity, 05 April 2015 - 12:57 PM.

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download