473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how to calculate the number of looping?

19
i use vb 6.0


Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit
  3.  
  4. Dim Error As Double
  5. Dim x As Integer
  6. Dim y As Double
  7. Dim z As Integer
  8. Dim eps As Double
  9. Dim N As Integer
  10.  
  11. Private Sub cmdCalculate_Click()
  12.  
  13. 'user input
  14.  
  15. If IsNumeric(txtN) And IsNumeric(txtEps) And IsNumeric(txtX) Then
  16. N = txtN.Text
  17. eps = txtEps.Text
  18. x = txtX.Text
  19. Else
  20.     MsgBox "all input must be numeric", vbExclamation, "numeric test"
  21.     txtN.SetFocus
  22. End If
  23.  
  24. 'calculate
  25.  
  26. Do
  27.     y = (x) - (x - (N ^ (1 / 2)))
  28.  
  29.     Error = y - x
  30.     If Error < 0 Then
  31.         Error = Error * -1
  32.     End If
  33.  
  34.   z = z + 1
  35.  
  36. Loop Until Error < eps
  37.  
  38. 'display the result
  39. lblDisplay.Caption = "root : " & y & "  iteration : " & z
  40.  
  41.  
  42.  
  43. End Sub
  44.  
  45. Private Sub cmdExit_Click()
  46. Unload Me
  47. End Sub
  48.  
the problems..

the 'eps' is the decimal places.in my program above it need user to insert the number of decimal places like this '0.001'.. but actually i just want the user put the value as '3'...

the iteration value must be in integer data type as the result of number of looping it takes to complete the loop.. the number of looping (iteration) didn't match its actually value..
Feb 26 '08 #1
6 2309
Stewart Ross
2,545 Expert Mod 2GB
Hi vbwire. It would be helpful to know what the loop calculation is for. When I test it the calculated error value remains constant and the loop does not exit.

I wonder if your reference to root N (N ^ (1/2)) in the loop is correct? N is not changing after it is entered by the user, and I guess there should a use of the loop counter z somewhere to change the value of N if the iterative calculation is intended to reduce the error value below the loop threshold. This is just a guess, though.

To refer to the number of decimal places for the loop threshold as an integer value you could define a variable NDecimals as an integer, get the value of NDecimals from the user instead of the double eps, then replace your loop exit test with
Expand|Select|Wrap|Line Numbers
  1. Loop Until Error < 10^(-NDecimals)
Another change is to use the Abs (absolute value) function to replace
Expand|Select|Wrap|Line Numbers
  1. Error = y - x
  2. If Error < 0 Then 
  3. Error = Error * -1
  4. End If
with
Expand|Select|Wrap|Line Numbers
  1. Error = abs(y-x)
Regards

Stewart
Feb 26 '08 #2
Killer42
8,435 Expert 8TB
As far as I could see, z should correctly return the number of iterations.

As for the calculation, I haven't looked into it in detail. But if you have any Integer data types in there, VB may convert interim results to Integer, thus losing precision. Try using all Double data types in there. And that includes for the literal values. If you say "1#" instead of just "1", you will force VB to use double data type for the value. In any case, why use (1/2), when you could just use the actual value, 0.5?

Oh, one other thing. Between lines 20 and 21, I believe you should Exit Sub.
Feb 26 '08 #3
vbwire
19
Hi vbwire. It would be helpful to know what the loop calculation is for. When I test it the calculated error value remains constant and the loop does not exit.
the loop calculate the value for y until the different between y and x is smaller than 'eps' ..

z is the number of looping it takes to complete the loop..

the value of x will take the value of y for the next looping.. for example in the first looping, x=2, then get the value of y.. then it will loop,use the value of y we get to calculate the new y. its continue give a new y until the different of the new y and y before it smaller than 'eps'
Feb 27 '08 #4
MikeTheBike
639 Expert 512MB
the loop calculate the value for y until the different between y and x is smaller than 'eps' ..

z is the number of looping it takes to complete the loop..

the value of x will take the value of y for the next looping.. for example in the first looping, x=2, then get the value of y.. then it will loop,use the value of y we get to calculate the new y. its continue give a new y until the different of the new y and y before it smaller than 'eps'
Hi
I started to look at this yesterday, but work got in the way (such a pain!).

Apart from the use of Abs() and changing the loop exit condition to

Loop Until Error < 0.1 ^ eps

there does not seem to be any iteration in the loop, ie. nothing changes from one loop to the next! So I've moded the code as follows:-

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Error As Double
  4. 'Dim x As integer
  5. Dim x As Double
  6. Dim y As Double
  7. Dim z As Integer
  8. Dim eps As Double
  9. Dim N As Integer
  10.  
  11. Private Sub cmdCalculate_Click()
  12.  
  13.     Dim txtX As Integer
  14.     'Dim txtY As Double
  15.     Dim txtZ As Integer
  16.     Dim txtN As Integer
  17.     Dim txtEps As Integer
  18.  
  19.  
  20. 'user input
  21.     txtX = 3
  22.     txtZ = 5
  23.     txtN = 1000
  24.     txtEps = 3
  25.  
  26.     z = 0
  27.     If IsNumeric(txtN) And IsNumeric(txtEps) And IsNumeric(txtX) Then
  28.     N = txtN
  29.     eps = txtEps
  30.     x = txtX
  31.     Else
  32.         MsgBox "all input must be numeric", vbExclamation, "numeric test"
  33.         'txtN.SetFocus
  34.         Exit Sub
  35.     End If
  36.  
  37.     'calculate
  38.  
  39.     Do
  40.         y = (x) - (x - (N ^ (1 / 2)))
  41.  
  42.         Error = Abs(y - x)
  43.         x = y
  44.         z = z + 1
  45.  
  46.     Loop Until Error < 0.1 ^ eps
  47.  
  48.     'display the result
  49.     'lblDisplay.Caption = "root : " & y & "  iteration : " & z
  50.     MsgBox "root : " & y & "  iteration : " & z
  51.  
  52. End Sub
ie have added x=y in the loop (as you indicted in the last post), but this always calculated the squre root of N in 2 iterations !!??

I still don't know what you are trying to do ?

Is this homework ?

ps. as this is not written in a form module I've hard coded values of txtN etc.



MTB
Feb 27 '08 #5
vbwire
19
thank to both of you..

this is the program after i corrected it..

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Error As Double
  4. Dim x As Double
  5. Dim y As Double
  6. Dim z As Integer
  7. Dim eps As Double
  8. Dim a As Integer
  9. Dim N As Integer
  10.  
  11. Private Sub cmdCalculate_Click()
  12.  
  13. 'user input
  14.  
  15. If IsNumeric(txtN) And IsNumeric(txtEps) And IsNumeric(txtX) Then
  16. N = txtN.Text
  17. a = txtEps.Text
  18. x = txtX.Text
  19. Else
  20.     MsgBox "all input must be numeric", vbExclamation, "numeric test"
  21.     txtN.SetFocus
  22. End If
  23.  
  24. 'calculate
  25.  
  26. eps = (10 ^ (-a))
  27.  
  28. Do
  29.     y = x - (x - (N ^ (1 / 3)))
  30.  
  31.     Error = Abs(y - x)
  32.  
  33.     x = y
  34.  
  35.     z = z + 1
  36.  
  37. Loop Until Error < eps
  38.  
  39. 'display the result
  40. lblDisplay.Caption = "root : " & y & "             iteration : " & z
  41.  
  42. End Sub
  43.  
  44. Private Sub cmdExit_Click()
  45. Unload Me
  46. End Sub
  47.  
  48.  
please look at my code for displaying the result.. how to make it in two line but i want it in same label.

also.. i want to display the result for root in the decimal palaces chosen by user,'eps'..
Feb 27 '08 #6
vbwire
19

ie have added x=y in the loop (as you indicted in the last post), but this always calculated the squre root of N in 2 iterations !!??

I still don't know what you are trying to do ?

Is this homework ?

ps. as this is not written in a form module I've hard coded values of txtN etc.



MTB
thank you mike.. =)

i just realise my mistake in the calculation..

Expand|Select|Wrap|Line Numbers
  1. y = (x) - (x - (N ^ (1 / 2)))
  2.  
its actually..

Expand|Select|Wrap|Line Numbers
  1. y = x - (x - (N ^ (1 / 3)))
  2.  
and check my previous post.. i still have something for you..haha..
Feb 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
7
by: rick | last post by:
Can anyone help, I am try to create a simple form using a table, where a user can fill out quanty and price and have a total automatically calculated and inserted in another field. I stuck trying...
4
by: Qwert | last post by:
Hello, I do: Debug.WriteLine("A: " & DateTime.Now.Ticks.ToString) REM Calculate a bunch of stuff. Some loops and math functions. Debug.WriteLine("B: " & DateTime.Now.Ticks.ToString) but...
5
by: milan.letic | last post by:
Hello, I believe that this has been answered somewhere but I cannot find it. I have 51 textboxes on page. In 50 I can enter value. 51st should tell me what is average of entered numbers. As...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
6
by: happyhondje | last post by:
Hello everyone, I've got a little issue, both programming and performance-wise. I have a set, containing objects that refer to other sets. For example, in a simple notation: (<a, b, c>, <d, e>)...
2
by: AccessHunter | last post by:
Hi, I have a table (Admittance) that has birth dates of all cases admitted, fields being (Case Number and Birth Date). I also have a form that asks the user to select a year and click the 'Find'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.