litlmike Posted April 18, 2008 Posted April 18, 2008 (edited) I feel kinda dumb asking this because I have been working with 2-dimensional arrays for a while now, but I have always just been grabbing data from one location and pasting it in another. And it is always just a loop of getting data 1 piece at a time.But now, I want to create the array, and then use it based upon user input. So the question is, How to Represent a Data Table (Matrix)? Or, how do I create a 2-dimensional array? And in a way that if the user enters the data that I can just grab that 1 element?For Example:Here is a table, that I want to create.http://kirstyne.files.wordpress.com/2007/0...s_bmi_chart.gifThe user should be able to put in data, like 6'0" ($feet = 6 ; $inch = 0) and weight like 200 lbs and have the number 27 returned giving them their BMI. The only way that I know to create the array, is to do this, but that seems rudimentary and there should be a better way. This is a small chart, what if were 1,000 times this size, it seems like someone would have a better method than this:$aBMI[0][0] = 29 $aBMI[1][0] = 27 $aBMI[2][0] = 25The chart isn't perfectly linear and so making a loop doesn't seem to make sense to me. How do I make this table? And how do I grab data from it, based upon the users input?Thanks. Edited April 18, 2008 by litlmike _ArrayPermute()_ArrayUnique()Excel.au3 UDF
weaponx Posted April 18, 2008 Posted April 18, 2008 Do you even need the chart? BMI is weight / height^2
litlmike Posted April 18, 2008 Author Posted April 18, 2008 Do you even need the chart? BMI is weight / height^2Yea, I guess you are right about that, but for the sake of learning how to do this in the event there wasn't a formula is this easliy done? I guess I imagine it could just be an enormous table with the result just being True/False depending on the variables. I thought about it, and I just couldn't figure out how to program something like that.Thanks _ArrayPermute()_ArrayUnique()Excel.au3 UDF
PsaltyDS Posted April 18, 2008 Posted April 18, 2008 The rows and columns in that table are height and weight. You have to assign some standard values for $aBMI[$h][$w], like $h is height in inches divided by 2, and $w is weight in pounds divided by 10. So $aBMI[30][21] would be the entry for 60" tall and 210lbs. If you have the formula to convert height/weight to BMI, then you could loop it through the array and fill in the values easily. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
PsaltyDS Posted April 18, 2008 Posted April 18, 2008 (edited) Do you even need the chart? BMI is weight / height^2Well, the table he linked to was in english measure. So it would be weight(lbs.) * 703 / height(in.)^2.The real question is: If you have the formula, why bother with a table? Given $h and $w just do the math and get the BMI. Why bother with an array at all? Edited April 18, 2008 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
weaponx Posted April 18, 2008 Posted April 18, 2008 (edited) expandcollapse popup#include <GUIConstants.au3> #include <StaticConstants.au3> Const $boardColumns = 14 Const $boardRows = 14 Const $cellSize = 20 Const $cellSpacing = 2 Const $boardMargin = 40 Const $startHeight = 54 Const $startWeight = 120 ;Array to store all label handles Dim $boardArray[$boardRows][$boardColumns][2] ;Create GUI window GUICreate("BMI", (($cellSize+$cellSpacing)*$boardColumns) + ($boardMargin*2), (($cellSize+$cellSpacing)*$boardRows) + ($boardMargin*2)) ; will create a dialog box that when displayed is centered $currentHeight = $startHeight For $Y = 0 to $boardRows - 1 ;Reset current weight at beginning of each row $currentWeight = $startWeight For $X = 0 to $boardColumns - 1 ;Draw weight headings If $Y = 0 Then GuiCtrlCreateLabel($currentWeight,($X*($cellSize +$cellSpacing)) + $boardMargin, $boardMargin - 15, $cellSize, $cellSize, $SS_CENTER) EndIf ;Draw height headings If $X = 0 Then GuiCtrlCreateLabel(InchesToFeet($currentHeight),7, ($Y*($cellSize+$cellSpacing)) + $boardMargin, 30, $cellSize, $SS_RIGHT) EndIf ;Get BMI $currentBMI = BMI($currentHeight, $currentWeight) ;Draw label $boardArray[$Y][$X][0] = GuiCtrlCreateLabel($currentBMI,($X*($cellSize +$cellSpacing)) + $boardMargin, ($Y*($cellSize+$cellSpacing)) + $boardMargin, $cellSize, $cellSize, $SS_CENTER ) GUICtrlSetFont(-1, 9, 600) ;Bold GUICtrlSetColor(-1,0x44494a) ;Dark grey ;Identify severity of BMI using color Switch $currentBMI Case 1 To 18 GUICtrlSetBkColor (-1, 0xa9d7d9);Blue Case 19 To 24 GUICtrlSetBkColor (-1, 0xf37042);Red Case 25 To 29 GUICtrlSetBkColor (-1, 0x97c05b);Green Case Else GUICtrlSetBkColor (-1, 0xf5e77d);Yellow EndSwitch ;Increment 10 pounds at a time $currentWeight += 10 Next ;Increment 2 inches at a time $currentHeight += 2 Next GUISetState (@SW_SHOW) ; will display an empty dialog box ; Run the GUI until the dialog is closed While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop Wend ;Return BMI given height (in inches) and weight (in pounds) Func BMI($height, $weight) Return Floor(($weight * 703) / ($height^2)) EndFunc ;Convert inches to feet/inches string Func InchesToFeet($inches) $feet = Int($inches / 12) $remain = Mod($inches, 12) Return $feet & "' " & $remain & '"' EndFunc Edited November 14, 2008 by weaponx
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now