
-----------------------------------
kk
Tue Jan 03, 2006 1:38 am

Program Crash
-----------------------------------
Hi, I am wondering whether you could help me out.  This program generates a math equation with three numbers; the first two numbers are multiplied, and the answer of the equation is the division of this product with the last number.  I am using 1 digit numbers to test the program.  The equation that is generated is put in a counted loop but the program crashes(it is NOT RESPONDING) only after the counted loop is finished running.(not shown)  Here is a section of code that I am running.  Thanks in advance. :)

I am new to visual basic, so I am not aware of any MEMORY MANAGEMENT issues, so feel free to add any coding optimizations.  

I've only included comments and not the code . If they are needed, feel free to ask for them.   :)





' .. portion of code in CreateQuestion function
 ' Put two random teams on the stack
    Factor1 = NotPrime()
    Teams.Push (Factor1)
    Answer = Teams.Peek
    Factor2 = (Rnd * 100000) Mod ((10 ^ Int(cboPlusMinDigits.Text) - 1)) + 1
    Teams.Push (Factor2)
    Answer = Answer * Teams.Peek
    
    ' Push the / sign on the stack
    Signs.Push SIGN_DIVIDE
    
    ' Put the division team on the team stack
    Teams.Push FactorOf(Answer, Factor1, Factor2)
    Answer = Answer / Teams.Peek
' .. portion of code in CreateQuestion function





'-----------------------------------------
' Name: FactorOf
' Recieves:
'      Dividend (Long) : Factors are generated from this number(not a prime)
'      Exclud1, Exclud2 (Integers) : Return value will exclude the possibility of returning the specified integers
'                                    (factors from multiplication).  For any unused parameters, argument should be 1.
' Returns: A Long that is a factor of the Dividend.
' Purpose: To return any factor of the Dividend that is not equal to the specified parameters.
'-----------------------------------------
Private Function FactorOf(ByVal Dividend As Long, ByVal Exclud1 As Long, ByVal Exclud2 As Long) As Long
    Dim FactorList() As Long
    Dim Num As Long
    Dim i As Long            ' Counter
    
    'FIND FACTORS OF DIVIDEND
    ' First in finding factors is that you only have to go half way to find all
    ' the factors of a given number
    ' divisor mod i = 0 then this is a factor

    'ADD FACTORS TO FACTORLIST ARRAY    
        
    ' In the case of a prime number send the Dividend Back (covers 1 digit case: 2 x 1 / ?)
    
    ' Pick a random factor and send it back. This factor cannot be any of the existing used in multiplication
    Do
        Num = FactorList(Int((Rnd * 1000) Mod UBound(FactorList)) + 1)
    Loop While (Num = Exclud1 Or Num = Exclud2) Or Num > 10 ^ cboDivDigits.Text
    FactorOf = Num
End Function


-----------------------------------
HazySmoke)345
Tue Jan 03, 2006 5:55 pm


-----------------------------------
Your lines are very confusing indeed... It sounds like a pretty simple program but the script is so long. Since I don't know the whole script, I don't know if the length necessary or not. I'd just simply do something like this:
Private Sub Form_Load()
    Dim a
    Dim b
    Dim c
    
    Randomize
    a = Int(Rnd * 10)
    b = Int(Rnd * 10)
    c = Int(Rnd * 10)
    
    Msgbox a * b / c
End Sub

-----------------------------------
cool dude
Tue Jan 03, 2006 6:40 pm


-----------------------------------
    
    Msgbox a * b / c


Shouldn't the a*b be in brackets since it is bedmass

-----------------------------------
[Gandalf]
Tue Jan 03, 2006 8:18 pm


-----------------------------------
The correct acronym is BEDMAS, and it doesn't matter since multiplication and division just go left to right.  Test it out and see for yourself.

-----------------------------------
cool dude
Tue Jan 03, 2006 11:38 pm


-----------------------------------
"]The correct acronym is BEDMAS, and it doesn't matter since multiplication and division just go left to right.  Test it out and see for yourself.

u r right it is BEDMAS and i did notice it but unfortunately nobody made an edit button for visual basic forums so i couldn't edit it. and yes it doesn't matter about the brackets but i couldn't test it at the moment since i didn't have VB on the computer i was using and thats why i said "shouldn't it be" instead of "it has to be"

-----------------------------------
[Gandalf]
Wed Jan 04, 2006 2:16 am


-----------------------------------
There's no edit button in any help forum.

What grade are you in?  Even if you're not really good in math, you shouldn't really need to check, just think:
a = 6
b = 2
c = 4
a*b/c = 3
(a*b)/c = 3
a*(b/c) = 3

:)

-----------------------------------
cool dude
Wed Jan 04, 2006 10:31 am


-----------------------------------
"]There's no edit button in any help forum.
:)

actually i believe there is an edit button in the turing help forum because i posted there and then i edited my posts. secondly yes i know bedmas so u don't have to teach me it but thanks anyways  :)

-----------------------------------
[Gandalf]
Wed Jan 04, 2006 4:52 pm


-----------------------------------
Actually, there is no edit button there either.
Now let's try to keep this topic on topic. :)

-----------------------------------
Brightguy
Wed Jan 04, 2006 10:36 pm

Re: Program Crash
-----------------------------------
What you posted looks ok, if the loop isn't ending make sure cboDivDigits.Text > 0 and that Dividend has more factors than the excluded ones.
