-
Hi,
I don't know why my code isn't working. I've been at it all day but it doesn't work.
I generate 6 numbers at random, called a,b,c,d,e and f.
Another number, x, will equal to (c*e - b*f) divided by (b*d - e*a).
x has to be an integer - no decimal points are allowed.
Anyway, I thought I'd do this by (and logically it should work) using a Do...Loop Until.
Here's what my code should do:
Generate random numbers for a, b, c, d, e and f. Keep generating them until (c*e - b*f) divided by (b*d - e*a) is an integer. Then print a, b, c, d, e, and f, and x, in my picture box.
But this isn't the case! I mean the numbers printed in the picture box don't work out to equal x, when I use the above formula. In fact, nothing seems to work.
I'd appreciate if anybody could tell me what's wrong with my code. Thanks!
_________________
'A command button and picture box
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Private Sub Command1_Click()
Picture1.Cls
Do
Randomize
a = -25 + Int(Rnd * 50)
If a = 0 Then
a = a + 1
End If
b = -25 + Int(Rnd * 50)
If b = 0 Then
b = b + 1
End If
c = -25 + Int(Rnd * 50)
If c = 0 Then
c = c + 1
End If
d = -25 + Int(Rnd * 50)
If d = 0 Then
d = d + 1
End If
e = -25 + Int(Rnd * 50)
If e = 0 Then
e = e + 1
End If
f = -25 + Int(Rnd * 50)
If f = 0 Then
f = f + 1
End If
Loop Until c * e - b * f Mod b * d - e * a = 0
Picture1.Print a & " " & b & " " & c & " " & d & " " & e & " " & f & vbCr & _
c * e - b * f / b * d - e * a
End Sub
-
I can't really help much.... but there is no point of rounding becuase you wrote dim intA as interger
if you need decimals then you should write:
did intA as single
-
Dimava,
Hello? The opposite is the case.
Quote:
'x has to be an integer - no decimal points are allowed'
-
I'm sorry, I just woke up and I dont think that I'm fully awake, I dont think that I can help becuase I'm not good with looping
-
I think you problem is araising from your use of brackets (or lack of them as the case may be) ;)
Code:
Option Explicit
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Private Sub Command1_Click()
Picture1.Cls
Do
Randomize
a = calcualteRand
b = calcualteRand
c = calcualteRand
d = calcualteRand
e = calcualteRand
f = calcualteRand
Loop Until ((c * e) - (b * f)) Mod ((b * d) - (e * a)) = 0
Picture1.Print a & " " & b & " " & c & " " & d & " " & e & " " & f & vbCr & _
((c * e) - (b * f)) / ((b * d) - (e * a))
End Sub
Private Function calcualteRand() As Integer
calcualteRand = -25 + Int(Rnd * 50)
If calcualteRand = 0 Then
calcualteRand = 1
End If
End Function
-
Thats right Mod takes precedence over the '-' operator
so your function calculatess the '*' part correctly then it applied the 'MOD' then the '-' afterwards. The brackets should fix that problem.
-
Thanks,
brackets sorts out the problem, alright.
Iain17 - nice touch, I like that calculaterand method.
Do you know how to solve this error that comes up
sometimes:
'Division by zero'
It must be getting stuff like 12/0 which, as you know, is infinity.
Anyway, I thought this would sort it out, but it doesn't:
Loop Until (b * d) <> (e * a) And ((c * e) - (b * f)) Mod ((b * d) - (e * a)) = 0
Any ideas?
Thanks.
-
Ian,
VB is a funny old beast. When you do an if statement, VB calculates the entire result. Even if the first part of the "If" is false, and you are using and "And", it will calcualte the rest of it.
So you need to do the If in two stages. Otherwise, even if you find that the first bit = 0, it will still try and calculate the next expresion, using an invalid value.
This cant be done in the loop until statement, so add a boolean.
Code:
Option Explicit
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Private Sub Command1_Click()
Dim blDone As Boolean
Picture1.Cls
Do
Randomize
a = calcualteRand
b = calcualteRand
c = calcualteRand
d = calcualteRand
e = calcualteRand
f = calcualteRand
If ((b * d) - (e * a)) <> 0 Then
If ((c * e) - (b * f)) Mod ((b * d) - (e * a)) = 0 Then
blDone = True
End If
End If
Loop Until blDone
Picture1.Print a & " " & b & " " & c & " " & d & " " & e & " " & f & vbCr & _
((c * e) - (b * f)) / ((b * d) - (e * a))
End Sub
Private Function calcualteRand() As Integer
calcualteRand = -25 + Int(Rnd * 50)
If calcualteRand = 0 Then
calcualteRand = 1
End If
End Function
-