Question:
Easy Programmming Question.?
2011-06-03 03:52:02 UTC
I am studying Visual Basic alone using a book. I stumbled upon this question in the book and I couldnt find a way to solve it. I understand Codes easier so if you could please write the code I could study it. Thanks :)

Sum of series 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

This is not Homework I'm studying this alone I need to finish this so I can continue studying
Four answers:
?
2011-06-03 05:23:41 UTC
Here is a hint on how to alternate between a positive and negative number.



Consider what happens to a number when you multiply it by -1 over and over again



1 * -1 = -1

-1 * -1 = 1

1 * -1 = -1

-1 * -1 = 1



See how the results alternate.



do that to a variable which is loaded with the value of 1 and use that value to multiply another number or variable by such as one you get from a For/Next loop. Of course you have to set the value of n=1 before you enter the loop , and withing the loop you multiply it by -1 at the and of each pass through the loop.





N=1



N * -1 = N (N now equals -1) , the next pass through the loop N * -1 = N is done again which make s N equal to a positive one. You can use then use N to multiply a copy of the loop variable so that it alternates between positive and negative for each pass through the loop.





You can also do this with an IF Else statement



IF N=1 then

N = -1

Else

N = 1

End IF





EDIT:



HINT.... The term you are looking to test is one of the fractions and NOT the total of those fractions.

example on the third pass through the loop the number 3 is used to make the fraction 1/3 which is 0.3333 This is the number that you will test against 0.0001 to see if you continue looping or not. The fourth pass results in 1/4 which is 0.25 etc... and so on until you generate a fraction that is less than 0.0001 . Also do not get the pos and neg value mixed up in your test so it the second pass you are subtracting or adding a negative -1/2 = -0.5 which is less than 0.0001 and would prematurely end your loop





ANSWER:



Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles Button1.Click



Dim denominator As Integer

Dim PosNeg_toggle As Integer

Dim total, term As Double







denominator = 1 ' initalize

PosNeg_toggle = 1





Do

'compute term

term = 1 / denominator ' this is always a positive result

'use the posneg_toggle to alternate between ADD and SUB (by adding a NEG)

total = total + (term * PosNeg_toggle)

'

'update variables for next pass through loop

denominator = denominator + 1 'increment

PosNeg_toggle = PosNeg_toggle * -1 ' change polarity of the number one



Loop While term > 0.0001

'display results

MsgBox("Total = " & total & " after " & denominator & " loops")



End Sub
2011-06-03 10:11:50 UTC
I don't think it's possible and I've gone over my code and I'm sure it's the right answer but seems to crash before it goes anywhere near 0.0001. I used VB6 however, if you look at this code and change the variables to decimal type it may work. I attached watches and it calculates it exactly to the pattern you provided.



See below:



Private Sub Form_Load()



Dim denominator As Integer, numerator As Integer

Dim holder As Double, total As Double

Dim check As Boolean



numerator = 1

denominator = 1

check = True

total = numerator / denominator



Do Until total < 0.0001



denominator = denominator + 1

holder = numerator / denominator



If check = False Then

total = total + holder

check = True



Else



total = total - holder

check = False



End If



Loop



MsgBox (total)



End Sub
?
2011-06-03 04:47:01 UTC
You can always read ahead first, and come back again to try to solve this problem.
2011-06-03 03:58:37 UTC
Go open torrent an and search what you need, got complete idea and solve your problem.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...