nigma_x Posted May 14, 2007 Posted May 14, 2007 This program is suppose to allow you to enter any number of ages and then figure out the oldest age, youngest age, and then the average of all the ages. Right now when I run the program if I put in 11 and 12 as an age the first time it will say 11 is the oldest 12 is the youngest and average is 11. If I enter 11 and 12 again with out exiting the program it then figures 12 as the oldest and 11 the youngest. If I enter more then one number sometimes it does it right and other time it will miss the smallest number. What I want to know if there is a bug in AUTOIT or is my code wrong. I am using for next with a nested for next loop to decide the which numbers are correct. The code is listed below. CODE#include <GUIConstants.au3> #include <Array.au3> ;Array Created to store the stundent ages Global $StudentAgeArray[100] ;Variable for counting the number of students added Global $StudentCountVar = 0 ;Variable for keeping track of the youngest student Global $YoungestStudentVar = 0 ;Variable for keeping track of the oldest student Global $OldestStudentVar = 0 ;Variable to total the ages of the students to find the average Global $AverageStudentAgeVar = 0 WarmUp() Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=C:\Projects\StudentAge\StudentAge.kxf $StudentAge = GUICreate("Student Age", 413, 298, 303, 219) GUISetOnEvent($GUI_EVENT_CLOSE, "StudentAgeClose") GUISetOnEvent($GUI_EVENT_MINIMIZE, "StudentAgeMinimize") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "StudentAgeMaximize") GUISetOnEvent($GUI_EVENT_RESTORE, "StudentAgeRestore") $StudentInpt = GUICtrlCreateInput(" ", 88, 24, 65, 21, BitOR($ES_AUTOHSCROLL,$ES_NUMBER)) GUICtrlSetOnEvent(-1, "StudentInptChange") $StudentsAgeLbl = GUICtrlCreateLabel("Student's Age", 16, 24, 70, 17) $NextAgeBtn = GUICtrlCreateButton("Next Age", 168, 24, 76, 22, $BS_FLAT) GUICtrlSetOnEvent(-1, "NextAgeBtnClick") $CalculateAgeBtn = GUICtrlCreateButton("Calculate Age", 24, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CalculateAgeBtnClick") $CloseBtn = GUICtrlCreateButton("Close", 288, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CloseBtnClick") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func NextAgeBtnClick() ;If Input box is blank then do not add a number to the variable. if GUICtrlRead($StudentInpt) <> " " Then ;Read the input box and store the interger to the current array $StudentAgeArray[$StudentCountVar] = GUICtrlRead($StudentInpt) ;Reset the input box to blank GUICtrlSetData ( $StudentInpt, " ", "" ) ;Add one to count $StudentCountVar = $StudentCountVar + 1 EndIf ; Set the control focus to the input box to allow another number to be entered into the array. ControlFocus ( "Student Age", " ", 3 ) EndFunc Func CalculateAgeBtnClick() ;test the function to see if the rest of the function should be started. if $StudentAgeArray[0] > 0 Then ;if $StudentCountVar is less then 2 then do not subtract from the count. if $StudentCountVar >= 2 then $StudentCountVar = $StudentCountVar - 1 ;start a loop to test each number to find the oldest and youngest student. for $CurrentAgeLoop = 0 to $StudentCountVar for $MaxAgeLoop = 0 to $StudentCountVar ;if a number is greater then the other numbers in the array then store the number in a variable called $OldestStudentVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;if a number is lesser then the other numbers in the array then store the number in a variable called $YoungestStudentVar if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next ;add the currnet number being tested to a variable called $AverageStudentAgeVar which will be used to find the average age $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] next if $StudentCountVar >= 2 then $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int( $AverageStudentAgeVar ) ;reset $StudentCountVar to 0 for a new count of oldest and youngest. $StudentCountVar = 0 ;Create a pop up box to show the oldest, youngest, and average age of all the students. MsgBox(0,"Oldest Student is " , $OldestStudentVar) MsgBox(0,"Youngest Student is " , $YoungestStudentVar) MsgBox(0,"Average Student is " , $AverageStudentAgeVar) ;reset $AverageStudentAgeVar to 0 for a new count of oldest and youngest. $AverageStudentAgeVar = 0 EndIf EndFunc ;function to have the program exit when the close button is pressed. Func CloseBtnClick() exit EndFunc ;function to have the program exit when the close button is pressed. Func StudentAgeClose() exit EndFunc ;Built in function of AUTOIT Func StudentAgeMaximize() EndFunc ;Built in function of AUTOIT Func StudentAgeMinimize() EndFunc ;Built in function of AUTOIT Func StudentAgeRestore() EndFunc ;Built in function of AUTOIT Func StudentInptChange() EndFunc ;EXTRA Function to test without user input. Func WarmUp() $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentAgeArray[3] = 15 $StudentAgeArray[4] = 99 $StudentAgeArray[5] = 8 $StudentCountVar = 5 if $StudentAgeArray[0] > 0 Then if $StudentCountVar >= 2 then $StudentCountVar = $StudentCountVar - 1 for $CurrentAgeLoop = 0 to $StudentCountVar for $MaxAgeLoop = 0 to $StudentCountVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next #CS for $MinAgeLoop = 0 to $StudentCountVar MsgBox(0,"MinAgeLoop", "$CurrentAgeLoop " & $StudentAgeArray[$CurrentAgeLoop] & " < " & " $MinAgeLoop " & $StudentAgeArray[$MinAgeLoop]) MsgBox(0,"$YoungestStudentVar", "$YoungestStudentVar = " & $YoungestStudentVar) Next #CE $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] next if $StudentCountVar >= 2 then $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int( $AverageStudentAgeVar ) $StudentCountVar = 0 MsgBox(0,"Oldest Student is " , $OldestStudentVar) MsgBox(0,"Youngest Student is " , $YoungestStudentVar) MsgBox(0,"Average Student is " , $AverageStudentAgeVar) $AverageStudentAgeVar = 0 EndIf $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentCountVar = 0 EndFunc
nitekram Posted May 14, 2007 Posted May 14, 2007 (edited) It is your math - if you change the greater and less than for the compare for only 2 numbers it works - with three it failes - recheck your math This program is suppose to allow you to enter any number of ages and then figure out the oldest age, youngest age, and then the average of all the ages. CODE#include <GUIConstants.au3> #include <Array.au3> ;Array Created to store the stundent ages Global $StudentAgeArray[100] ;Variable for counting the number of students added Global $StudentCountVar = 0 ;Variable for keeping track of the youngest student Global $YoungestStudentVar = 0 ;Variable for keeping track of the oldest student Global $OldestStudentVar = 0 ;Variable to total the ages of the students to find the average Global $AverageStudentAgeVar = 0 WarmUp() Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=C:\Projects\StudentAge\StudentAge.kxf $StudentAge = GUICreate("Student Age", 413, 298, 303, 219) GUISetOnEvent($GUI_EVENT_CLOSE, "StudentAgeClose") GUISetOnEvent($GUI_EVENT_MINIMIZE, "StudentAgeMinimize") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "StudentAgeMaximize") GUISetOnEvent($GUI_EVENT_RESTORE, "StudentAgeRestore") $StudentInpt = GUICtrlCreateInput(" ", 88, 24, 65, 21, BitOR($ES_AUTOHSCROLL,$ES_NUMBER)) GUICtrlSetOnEvent(-1, "StudentInptChange") $StudentsAgeLbl = GUICtrlCreateLabel("Student's Age", 16, 24, 70, 17) $NextAgeBtn = GUICtrlCreateButton("Next Age", 168, 24, 76, 22, $BS_FLAT) GUICtrlSetOnEvent(-1, "NextAgeBtnClick") $CalculateAgeBtn = GUICtrlCreateButton("Calculate Age", 24, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CalculateAgeBtnClick") $CloseBtn = GUICtrlCreateButton("Close", 288, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CloseBtnClick") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func NextAgeBtnClick() ;If Input box is blank then do not add a number to the variable. if GUICtrlRead($StudentInpt) <> " " Then ;Read the input box and store the interger to the current array $StudentAgeArray[$StudentCountVar] = GUICtrlRead($StudentInpt) ;Reset the input box to blank GUICtrlSetData ( $StudentInpt, " ", "" ) ;Add one to count $StudentCountVar = $StudentCountVar + 1 EndIf ; Set the control focus to the input box to allow another number to be entered into the array. ControlFocus ( "Student Age", " ", 3 ) EndFunc Func CalculateAgeBtnClick() ;test the function to see if the rest of the function should be started. if $StudentAgeArray[0] > 0 Then ;if $StudentCountVar is less then 2 then do not subtract from the count. if $StudentCountVar >= 2 then $StudentCountVar = $StudentCountVar - 1 ;start a loop to test each number to find the oldest and youngest student. for $CurrentAgeLoop = 0 to $StudentCountVar for $MaxAgeLoop = 0 to $StudentCountVar ;if a number is greater then the other numbers in the array then store the number in a variable called $OldestStudentVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;if a number is lesser then the other numbers in the array then store the number in a variable called $YoungestStudentVar if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next ;add the currnet number being tested to a variable called $AverageStudentAgeVar which will be used to find the average age $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] next if $StudentCountVar >= 2 then $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int( $AverageStudentAgeVar ) ;reset $StudentCountVar to 0 for a new count of oldest and youngest. $StudentCountVar = 0 ;Create a pop up box to show the oldest, youngest, and average age of all the students. MsgBox(0,"Oldest Student is " , $OldestStudentVar) MsgBox(0,"Youngest Student is " , $YoungestStudentVar) MsgBox(0,"Average Student is " , $AverageStudentAgeVar) ;reset $AverageStudentAgeVar to 0 for a new count of oldest and youngest. $AverageStudentAgeVar = 0 EndIf EndFunc ;function to have the program exit when the close button is pressed. Func CloseBtnClick() exit EndFunc ;function to have the program exit when the close button is pressed. Func StudentAgeClose() exit EndFunc ;Built in function of AUTOIT Func StudentAgeMaximize() EndFunc ;Built in function of AUTOIT Func StudentAgeMinimize() EndFunc ;Built in function of AUTOIT Func StudentAgeRestore() EndFunc ;Built in function of AUTOIT Func StudentInptChange() EndFunc ;EXTRA Function to test without user input. Func WarmUp() $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentAgeArray[3] = 15 $StudentAgeArray[4] = 99 $StudentAgeArray[5] = 8 $StudentCountVar = 5 if $StudentAgeArray[0] > 0 Then if $StudentCountVar >= 2 then $StudentCountVar = $StudentCountVar - 1 for $CurrentAgeLoop = 0 to $StudentCountVar for $MaxAgeLoop = 0 to $StudentCountVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next #CS for $MinAgeLoop = 0 to $StudentCountVar MsgBox(0,"MinAgeLoop", "$CurrentAgeLoop " & $StudentAgeArray[$CurrentAgeLoop] & " < " & " $MinAgeLoop " & $StudentAgeArray[$MinAgeLoop]) MsgBox(0,"$YoungestStudentVar", "$YoungestStudentVar = " & $YoungestStudentVar) Next #CE $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] next if $StudentCountVar >= 2 then $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int( $AverageStudentAgeVar ) $StudentCountVar = 0 MsgBox(0,"Oldest Student is " , $OldestStudentVar) MsgBox(0,"Youngest Student is " , $YoungestStudentVar) MsgBox(0,"Average Student is " , $AverageStudentAgeVar) $AverageStudentAgeVar = 0 EndIf $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentCountVar = 0 EndFunc EDIT Fixed format Edited May 14, 2007 by nitekram 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow." WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator
martin Posted May 14, 2007 Posted May 14, 2007 There is element [2] missing in the warm up. Also, I think the code is too complicated. One of the for/next loops is not only not needed but is very confusing. My suggestion is that you simplify your code as much as possible. You also have repeats of near identical code so these could be put into a function. The way I see it, the more you write the more can go wrong and the more difficult it is to fix. I modified your code a bit here but it still needs a bit of work, but I think it might work now. expandcollapse popup#include <GUIConstants.au3> #include <Array.au3> ;Array Created to store the stundent ages Global $StudentAgeArray[100] ;Variable for counting the number of students added Global $StudentCountVar = 0 ;Variable for keeping track of the youngest student Global $YoungestStudentVar = 0 ;Variable for keeping track of the oldest student Global $OldestStudentVar = 0 ;Variable to total the ages of the students to find the average Global $AverageStudentAgeVar = 0 WarmUp() Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=C:\Projects\StudentAge\StudentAge.kxf $StudentAge = GUICreate("Student Age", 413, 298, 303, 219) GUISetOnEvent($GUI_EVENT_CLOSE, "StudentAgeClose") GUISetOnEvent($GUI_EVENT_MINIMIZE, "StudentAgeMinimize") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "StudentAgeMaximize") GUISetOnEvent($GUI_EVENT_RESTORE, "StudentAgeRestore") $StudentInpt = GUICtrlCreateInput(" ", 88, 24, 65, 21, BitOR($ES_AUTOHSCROLL, $ES_NUMBER)) GUICtrlSetOnEvent(-1, "StudentInptChange") $StudentsAgeLbl = GUICtrlCreateLabel("Student's Age", 16, 24, 70, 17) $NextAgeBtn = GUICtrlCreateButton("Next Age", 168, 24, 76, 22, $BS_FLAT) GUICtrlSetOnEvent(-1, "NextAgeBtnClick") $CalculateAgeBtn = GUICtrlCreateButton("Calculate Age", 24, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CalculateAgeBtnClick") $CloseBtn = GUICtrlCreateButton("Close", 288, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CloseBtnClick") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func NextAgeBtnClick() ;If Input box is blank then do not add a number to the variable. If GUICtrlRead($StudentInpt) <> " " Then ;Read the input box and store the interger to the current array $StudentAgeArray[$StudentCountVar] = GUICtrlRead($StudentInpt) ;Reset the input box to blank GUICtrlSetData($StudentInpt, " ", "") ;Add one to count $StudentCountVar = $StudentCountVar + 1 EndIf ; Set the control focus to the input box to allow another number to be entered into the array. ControlFocus("Student Age", " ", 3) EndFunc ;==>NextAgeBtnClick Func CalculateAgeBtnClick() ;test the function to see if the rest of the function should be started. If $StudentAgeArray[0] > 0 Then ;if $StudentCountVar is less then 2 then do not subtract from the count. ;If $StudentCountVar >= 2 Then $StudentCountVar = $StudentCountVar - 1 ;start a loop to test each number to find the oldest and youngest student. $OldestStudentVar = 0 $YoungestStudentVar = 200 $counted = 0 $TotalStudentAgeVar = 0 For $CurrentAgeLoop = 0 To $StudentCountVar - 1 ; For $MaxAgeLoop = 0 To $StudentCountVar ;if a number is greater then the other numbers in the array then store the number in a variable called $OldestStudentVar If $StudentAgeArray[$CurrentAgeLoop] > $OldestStudentVar Then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;if a number is lesser then the other numbers in the array then store the number in a variable called $YoungestStudentVar If $StudentAgeArray[$CurrentAgeLoop] < $YoungestStudentVar Then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ; Next ;add the currnet number being tested to a variable called $AverageStudentAgeVar which will be used to find the average age $TotalStudentAgeVar = $TotalStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] $counted += 1 Next consolewrite('counted = ' & $counted & @crlf) ;If $StudentCountVar >= 2 Then ; $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) ; Else $AverageStudentAgeVar = $TotalStudentAgeVar / $counted ; EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int($AverageStudentAgeVar) ;reset $StudentCountVar to 0 for a new count of oldest and youngest. $StudentCountVar = 0 ;Create a pop up box to show the oldest, youngest, and average age of all the students. MsgBox(0, "Oldest Student is ", $OldestStudentVar) MsgBox(0, "Youngest Student is ", $YoungestStudentVar) MsgBox(0, "Average Student is ", $AverageStudentAgeVar) ;reset $AverageStudentAgeVar to 0 for a new count of oldest and youngest. $AverageStudentAgeVar = 0 EndIf EndFunc ;==>CalculateAgeBtnClick ;function to have the program exit when the close button is pressed. Func CloseBtnClick() Exit EndFunc ;==>CloseBtnClick ;function to have the program exit when the close button is pressed. Func StudentAgeClose() Exit EndFunc ;==>StudentAgeClose ;Built in function of AUTOIT Func StudentAgeMaximize() EndFunc ;==>StudentAgeMaximize ;Built in function of AUTOIT Func StudentAgeMinimize() EndFunc ;==>StudentAgeMinimize ;Built in function of AUTOIT Func StudentAgeRestore() EndFunc ;==>StudentAgeRestore ;Built in function of AUTOIT Func StudentInptChange() EndFunc ;==>StudentInptChange ;EXTRA Function to test without user input. Func WarmUp() $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentAgeArray[2] = 15 $StudentAgeArray[3] = 99 $StudentAgeArray[4] = 8 $StudentCountVar = 5 $OldestStudentVar = 0 $YoungestStudentVar = 200 $count = 0 $totalage = 0 If $StudentAgeArray[0] > 0 Then ;If $StudentCountVar >= 2 Then $StudentCountVar = $StudentCountVar - 1 For $CurrentAgeLoop = 0 To $StudentCountVar - 1 ; For $MaxAgeLoop = 0 To $StudentCountVar If $StudentAgeArray[$CurrentAgeLoop] > $OldestStudentVar Then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] If $StudentAgeArray[$CurrentAgeLoop] < $YoungestStudentVar Then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;Next #cs for $MinAgeLoop = 0 to $StudentCountVar MsgBox(0,"MinAgeLoop", "$CurrentAgeLoop " & $StudentAgeArray[$CurrentAgeLoop] & " < " & " $MinAgeLoop " & $StudentAgeArray[$MinAgeLoop]) MsgBox(0,"$YoungestStudentVar", "$YoungestStudentVar = " & $YoungestStudentVar) Next #CE $totalage += $StudentAgeArray[$CurrentAgeLoop] $count += 1 Next ; If $StudentCountVar >= 2 Then ; $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) ; Else $AverageStudentAgeVar = $totalage / $count ; EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int($AverageStudentAgeVar) $StudentCountVar = 0 MsgBox(0, "Oldest Student is ", $OldestStudentVar) MsgBox(0, "Youngest Student is ", $YoungestStudentVar) MsgBox(0, "Average Student is ", $AverageStudentAgeVar) $AverageStudentAgeVar = 0 EndIf $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentCountVar = 0 EndFunc ;==>WarmUp Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Valuater Posted May 14, 2007 Posted May 14, 2007 My attempt ( did not fix everything that i would have changed personally ) expandcollapse popup#include <GUIConstants.au3> #include <Array.au3> ;Array Created to store the stundent ages Global $StudentAgeArray[1] ;Variable for counting the number of students added Global $StudentCountVar = 0 ;Variable for keeping track of the youngest student Global $YoungestStudentVar = 0 ;Variable for keeping track of the oldest student Global $OldestStudentVar = 0 ;Variable to total the ages of the students to find the average Global $AverageStudentAgeVar = 0 ;WarmUp() Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=C:\Projects\StudentAge\StudentAge.kxf $StudentAge = GUICreate("Student Age", 413, 298, 303, 219) GUISetOnEvent($GUI_EVENT_CLOSE, "StudentAgeClose") ;GUISetOnEvent($GUI_EVENT_MINIMIZE, "StudentAgeMinimize") ;GUISetOnEvent($GUI_EVENT_MAXIMIZE, "StudentAgeMaximize") ;GUISetOnEvent($GUI_EVENT_RESTORE, "StudentAgeRestore") $StudentInpt = GUICtrlCreateInput(" ", 88, 24, 65, 21, BitOR($ES_AUTOHSCROLL, $ES_NUMBER)) ;GUICtrlSetOnEvent(-1, "StudentInptChange") $StudentsAgeLbl = GUICtrlCreateLabel("Student's Age", 16, 24, 70, 17) $NextAgeBtn = GUICtrlCreateButton("Next Age", 168, 24, 76, 22, $BS_FLAT) GUICtrlSetOnEvent(-1, "NextAgeBtnClick") $CalculateAgeBtn = GUICtrlCreateButton("Calculate Age", 24, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CalculateAgeBtnClick") $CloseBtn = GUICtrlCreateButton("Close", 288, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "StudentAgeClose") ;"CloseBtnClick") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func NextAgeBtnClick() ;If Input box is blank then do not add a number to the variable. If GUICtrlRead($StudentInpt) <> " " Then ;Add one to count $StudentCountVar = $StudentCountVar + 1 ;Read the input box and store the interger to the current array ReDim $StudentAgeArray[$StudentCountVar + 1] $StudentAgeArray[0] = $StudentCountVar $StudentAgeArray[$StudentCountVar] = GUICtrlRead($StudentInpt) ;Reset the input box to blank GUICtrlSetData($StudentInpt, " ", "") EndIf ; Set the control focus to the input box to allow another number to be entered into the array. ControlFocus("Student Age", " ", 3) ;_ArrayDisplay($StudentAgeArray, "display") EndFunc ;==>NextAgeBtnClick Func CalculateAgeBtnClick() ;test the function to see if the rest of the function should be started. If $StudentAgeArray[0] > 1 Then ;if $StudentCountVar is less then 2 then do not subtract from the count.... > is greater ;If $StudentCountVar <= 2 Then $StudentCountVar = $StudentCountVar - 1 ;start a loop to test each number to find the oldest and youngest student. For $CurrentAgeLoop = 1 To $StudentCountVar ;For $MaxAgeLoop = 1 To $StudentCountVar ; ;if a number is greater then the other numbers in the array then store the number in a variable called $OldestStudentVar ; If $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] Then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ; ;if a number is lesser then the other numbers in the array then store the number in a variable called $YoungestStudentVar ; If $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] Then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;Next ;add the currnet number being tested to a variable called $AverageStudentAgeVar which will be used to find the average age $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] $StudentAgeArray[$CurrentAgeLoop] = Number($StudentAgeArray[$CurrentAgeLoop]) Next _ArraySort($StudentAgeArray, 1, 1) $OldestStudentVar = $StudentAgeArray[1] $YoungestStudentVar = $StudentAgeArray[$StudentCountVar] ;_ArrayDisplay($StudentAgeArray, "display") ; for testing If $StudentCountVar >= 2 Then $AverageStudentAgeVar = $AverageStudentAgeVar / $StudentCountVar Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int($AverageStudentAgeVar) ;reset $StudentCountVar to 0 for a new count of oldest and youngest. $StudentCountVar = 0 ReDim $StudentAgeArray[1] ;Create a pop up box to show the oldest, youngest, and average age of all the students. MsgBox(0, "Oldest Student is ", $OldestStudentVar) MsgBox(0, "Youngest Student is ", $YoungestStudentVar) MsgBox(0, "Average Student is ", $AverageStudentAgeVar) ;reset $AverageStudentAgeVar to 0 for a new count of oldest and youngest. $AverageStudentAgeVar = 0 EndIf EndFunc ;==>CalculateAgeBtnClick Func StudentAgeClose() Exit EndFunc ;==>StudentAgeClose 8)
nigma_x Posted May 15, 2007 Author Posted May 15, 2007 I don't understand what you mean? It is your math - if you change the greater and less than for the compare for only 2 numbers it works - with three it failes - recheck your math CODE#include <GUIConstants.au3> #include <Array.au3> ;Array Created to store the stundent ages Global $StudentAgeArray[100] ;Variable for counting the number of students added Global $StudentCountVar = 0 ;Variable for keeping track of the youngest student Global $YoungestStudentVar = 0 ;Variable for keeping track of the oldest student Global $OldestStudentVar = 0 ;Variable to total the ages of the students to find the average Global $AverageStudentAgeVar = 0 WarmUp() Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=C:\Projects\StudentAge\StudentAge.kxf $StudentAge = GUICreate("Student Age", 413, 298, 303, 219) GUISetOnEvent($GUI_EVENT_CLOSE, "StudentAgeClose") GUISetOnEvent($GUI_EVENT_MINIMIZE, "StudentAgeMinimize") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "StudentAgeMaximize") GUISetOnEvent($GUI_EVENT_RESTORE, "StudentAgeRestore") $StudentInpt = GUICtrlCreateInput(" ", 88, 24, 65, 21, BitOR($ES_AUTOHSCROLL,$ES_NUMBER)) GUICtrlSetOnEvent(-1, "StudentInptChange") $StudentsAgeLbl = GUICtrlCreateLabel("Student's Age", 16, 24, 70, 17) $NextAgeBtn = GUICtrlCreateButton("Next Age", 168, 24, 76, 22, $BS_FLAT) GUICtrlSetOnEvent(-1, "NextAgeBtnClick") $CalculateAgeBtn = GUICtrlCreateButton("Calculate Age", 24, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CalculateAgeBtnClick") $CloseBtn = GUICtrlCreateButton("Close", 288, 224, 100, 30, $BS_FLAT) GUICtrlSetOnEvent(-1, "CloseBtnClick") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func NextAgeBtnClick() ;If Input box is blank then do not add a number to the variable. if GUICtrlRead($StudentInpt) <> " " Then ;Read the input box and store the interger to the current array $StudentAgeArray[$StudentCountVar] = GUICtrlRead($StudentInpt) ;Reset the input box to blank GUICtrlSetData ( $StudentInpt, " ", "" ) ;Add one to count $StudentCountVar = $StudentCountVar + 1 EndIf ; Set the control focus to the input box to allow another number to be entered into the array. ControlFocus ( "Student Age", " ", 3 ) EndFunc Func CalculateAgeBtnClick() ;test the function to see if the rest of the function should be started. if $StudentAgeArray[0] > 0 Then ;if $StudentCountVar is less then 2 then do not subtract from the count. if $StudentCountVar >= 2 then $StudentCountVar = $StudentCountVar - 1 ;start a loop to test each number to find the oldest and youngest student. for $CurrentAgeLoop = 0 to $StudentCountVar for $MaxAgeLoop = 0 to $StudentCountVar ;if a number is greater then the other numbers in the array then store the number in a variable called $OldestStudentVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;if a number is lesser then the other numbers in the array then store the number in a variable called $YoungestStudentVar if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next ;add the currnet number being tested to a variable called $AverageStudentAgeVar which will be used to find the average age $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] next if $StudentCountVar >= 2 then $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int( $AverageStudentAgeVar ) ;reset $StudentCountVar to 0 for a new count of oldest and youngest. $StudentCountVar = 0 ;Create a pop up box to show the oldest, youngest, and average age of all the students. MsgBox(0,"Oldest Student is " , $OldestStudentVar) MsgBox(0,"Youngest Student is " , $YoungestStudentVar) MsgBox(0,"Average Student is " , $AverageStudentAgeVar) ;reset $AverageStudentAgeVar to 0 for a new count of oldest and youngest. $AverageStudentAgeVar = 0 EndIf EndFunc ;function to have the program exit when the close button is pressed. Func CloseBtnClick() exit EndFunc ;function to have the program exit when the close button is pressed. Func StudentAgeClose() exit EndFunc ;Built in function of AUTOIT Func StudentAgeMaximize() EndFunc ;Built in function of AUTOIT Func StudentAgeMinimize() EndFunc ;Built in function of AUTOIT Func StudentAgeRestore() EndFunc ;Built in function of AUTOIT Func StudentInptChange() EndFunc ;EXTRA Function to test without user input. Func WarmUp() $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentAgeArray[3] = 15 $StudentAgeArray[4] = 99 $StudentAgeArray[5] = 8 $StudentCountVar = 5 if $StudentAgeArray[0] > 0 Then if $StudentCountVar >= 2 then $StudentCountVar = $StudentCountVar - 1 for $CurrentAgeLoop = 0 to $StudentCountVar for $MaxAgeLoop = 0 to $StudentCountVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next #CS for $MinAgeLoop = 0 to $StudentCountVar MsgBox(0,"MinAgeLoop", "$CurrentAgeLoop " & $StudentAgeArray[$CurrentAgeLoop] & " < " & " $MinAgeLoop " & $StudentAgeArray[$MinAgeLoop]) MsgBox(0,"$YoungestStudentVar", "$YoungestStudentVar = " & $YoungestStudentVar) Next #CE $AverageStudentAgeVar = $AverageStudentAgeVar + $StudentAgeArray[$CurrentAgeLoop] next if $StudentCountVar >= 2 then $AverageStudentAgeVar = $AverageStudentAgeVar / ($StudentCountVar + 1) Else $AverageStudentAgeVar = $AverageStudentAgeVar / 2 EndIf ;Converts $AverageStudentAgeVar to an interger if $AverageStudentAgeVar is a float. $AverageStudentAgeVar = Int( $AverageStudentAgeVar ) $StudentCountVar = 0 MsgBox(0,"Oldest Student is " , $OldestStudentVar) MsgBox(0,"Youngest Student is " , $YoungestStudentVar) MsgBox(0,"Average Student is " , $AverageStudentAgeVar) $AverageStudentAgeVar = 0 EndIf $StudentAgeArray[0] = 11 $StudentAgeArray[1] = 12 $StudentCountVar = 0 EndFunc EDIT Fixed format
nitekram Posted May 15, 2007 Posted May 15, 2007 I don't understand what you mean?I changed the following code - changed the greater than and less than and it works for 2 numbers only, I did not go any further, and figured it had to do with math, as I was able to get the right output for 2 students, but not 3 students. Look at either of the other examples given - they might be better. for $MaxAgeLoop = 0 to $StudentCountVar ;if a number is greater then the other numbers in the array then store the number in a variable called $OldestStudentVar if $StudentAgeArray[$CurrentAgeLoop] < $StudentAgeArray[$MaxAgeLoop] then $OldestStudentVar = $StudentAgeArray[$CurrentAgeLoop] ;if a number is lesser then the other numbers in the array then store the number in a variable called $YoungestStudentVar if $StudentAgeArray[$CurrentAgeLoop] > $StudentAgeArray[$MaxAgeLoop] then $YoungestStudentVar = $StudentAgeArray[$CurrentAgeLoop] Next 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow." WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator
nigma_x Posted May 15, 2007 Author Posted May 15, 2007 I agree with you martin. The more complicated the code the more that can go wrong. The reason I did this program was for an assignment for class. There was no limit on what compiler I used. One of the requirements was a nested loop. Which is why I had 2 for-next loops. I just wanted to know why the program was not working properly. I figured it should have.
PsaltyDS Posted May 15, 2007 Posted May 15, 2007 The reason I did this program was for an assignment for class. There was no limit on what compiler I used. One of the requirements was a nested loop. Which is why I had 2 for-next loops.Ten points off your grade for getting the forum to do your homework!!! 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
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