Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   problem (http://www.programmingforums.org/showthread.php?t=15399)

tyson642 Mar 13th, 2008 5:27 AM

problem
 
i am trying to find a way to get the power of something for my program but i cant sem to find anything can anyone help

e.g 4 to the power of 2

i need to know the coding to get the power of please help

Grich Mar 13th, 2008 6:10 AM

Re: problem
 
Look for something called a POW() function. You could hard code it like this (in BASIC psuedo):
:

DIM number_1 AS INTEGER = 4
DIM number_2 AS INTEGER = 2
DO UNTIL number == 0
number _2 -= 1
number_1 *= number_1
LOOP
RETURN number_1


OpenLoop Mar 13th, 2008 6:42 AM

Re: problem
 
:

  1. DIM x AS INTEGER = 4
  2. DIM y AS INTEGER = 0
  3. y = x^3        'x to the power of 3, y = 64


Ooble Mar 14th, 2008 8:46 PM

Re: problem
 
OpenLoop's solution also works in VB Classic (VB 6 and below). Just use the ^ operator.

tyson642 Mar 17th, 2008 9:40 AM

Re: problem
 
i have tried using the ^ to no effect

the program i am using requires to find the bmi (body mass index) of a person with the formula

BMI = weight in kilograms / height in metres^2

i have tried using this to no effect please help

i also need ther code to put the answer to 1 decimal place

thanks

edit: here is the code that i have been using

usersbmi(Counter) = usersweight(Counter) / usersheight(Counter) ^ 2
i think that it may be working but justs needs to be rounded to the decimal place e.g 24=24.3

tyson642 Mar 17th, 2008 10:22 AM

Re: problem
 
here is a copy of my program

:

Private Sub cmdok_Click()
Dim usersweight(5) As Integer
Dim usersheight(5) As Integer
Dim usersbmi(5) As Integer
Dim underweight As Integer
Dim idealweight As Integer
Dim overweight As Integer



underweight = 0
idealweight = 0
overweight = 0

Call get_inputs(usersweight(), usersheight())
Call calculate_bmi(usersbmi(), usersweight(), usersheight())
Call count_weight(usersbmi(), underweight, idealweight, overweight)
Call display_details(usersbmi(), usersweight(), usersheight(), underweight, idealweight, overweight)
End Sub

Private Sub get_inputs(ByRef usersweight() As Integer, ByRef usersheight() As Integer)
For Counter = 0 To 4

'validate users weight

Do


 usersweight(Counter) = InputBox("please enter your weight")
If (usersweight(Counter) <= 0) Then MsgBox ("Sorry invalid weight, value must be greater then zero, please re-enter")
Loop Until (usersweight(Counter) > 0)

' validate users height

Do
usersheight(Counter) = InputBox("please enter your height")
If (usersheight(Counter) <= 0) Then MsgBox ("Sorry invalid height, value must be greater then zero, please re-enter")
Loop Until (usersheight(Counter) > 0)
Next




End Sub

Private Sub calculate_bmi(ByRef usersbmi() As Integer, usersweight() As Integer, usersheight() As Integer)

For Counter = 0 To 4
 usersbmi(Counter) = usersweight(Counter) / usersheight(Counter) ^ 2
Next
End Sub

Private Sub count_weight(usersbmi() As Integer, ByRef underweight As Integer, ByRef idealweight As Integer, ByRef overweight As Integer)

For Counter = 0 To 4
If usersbmi(Counter) < 18.5 Then underweight = underweight + 1
If usersbmi(Counter) >= 18.5 And usersbmi(Counter) < 25 Then idealweight = idealweight + 1
If usersbmi(Counter) >= 25 Then overweight = overweight + 1
Next
End Sub

Private Sub display_details(usersbmi() As Integer, usersweight() As Integer, usersheight() As Integer, underweight As Integer, idealweight As Integer, overweight As Integer)

picdisplay.Print "weight"; Tab(25); "height"; Tab(50); "bmi"; Tab(75)
picdisplay.Print "---------------------"; Tab(25); "--------------------------"; Tab(50); "-------------------------"; Tab(75)

For Counter = 0 To 4
picdisplay.Print usersweight(Counter); Tab(25); usersheight(Counter); Tab(50); usersbmi(Counter); Tab(75)
Next

picdisplay.Print ""
picdisplay.Print "underweight"; Tab(25); underweight
picdisplay.Print "idealweight"; Tab(25); idealweight
picdisplay.Print "overweight"; Tab(25); overweight
End Sub


as you can see my main problem is the calculation
:

Private Sub calculate_bmi(ByRef usersbmi() As Integer, usersweight() As Integer, usersheight() As Integer)

For Counter = 0 To 4
usersbmi(Counter) = usersweight(Counter) / usersheight(Counter) ^ 2
Next
End Sub


OpenLoop Mar 17th, 2008 11:36 AM

Re: problem
 
What does 'to no effect' mean? are you getting an incorrect answer? can you give a sample output with expected results?

If you want a decimal point, you should define your variables as Float not Integer (not sure what VB's type for float)
Also, Try using parenthesis to isolate the calculation.
:

  1. usersbmi(Counter) = (usersweight(Counter) / usersheight(Counter)) ^ 2
  2. or?
  3. usersbmi(Counter) = usersweight(Counter) / (usersheight(Counter) ^ 2)



All times are GMT -5. The time now is 6:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC