bobamatics
asked on
Two loops and some calculation.
I need to calculate the control number in a Swedish identification number. As in 19640823-3234. I need to calculate the last integer in this string as it is now.
For i = 1 To Len(strPartial)
intNumbers(j) = Mid(strPartial, i, 1): j = j + 1
Next i
For l = 0 To UBound(intNumbers)
If k Mod 2 > 0 Then
intDivBy = 1
Else
intDivBy = 2
End If
intResult = intNumbers(k) * intDivBy
If intResult > 9 Then
intParts(0) = Left(intResult, 1)
intParts(1) = Right(intResult, 1)
intResult = intResult + intParts(0) + intParts(1)
Else
intResult = intResult + intResult
End If
k = k + 1
Next l
strPartial contains 640823323 since the last integer, which is the control number, can be calculated through the stuff that is in strPartial. Each character (integer) in strPartial needs to be multiplied with either 1 or 2, depending on its position. The first int is multiplied with 2, the next with 1, the next with 2 and so on.
If the product is 16 (8*2) the added result is 1+6, not 16. That's why I add the array to keep hold of my results.
The code up above does give me a number, but it's rarely the correct one. Does anyone understand my problem or have a clue on what is the problem with my code. I'm soon going insane over this...answers appreciated. Please ask additional questions if you don't understand what I'm trying to calculate.
For i = 1 To Len(strPartial)
intNumbers(j) = Mid(strPartial, i, 1): j = j + 1
Next i
For l = 0 To UBound(intNumbers)
If k Mod 2 > 0 Then
intDivBy = 1
Else
intDivBy = 2
End If
intResult = intNumbers(k) * intDivBy
If intResult > 9 Then
intParts(0) = Left(intResult, 1)
intParts(1) = Right(intResult, 1)
intResult = intResult + intParts(0) + intParts(1)
Else
intResult = intResult + intResult
End If
k = k + 1
Next l
strPartial contains 640823323 since the last integer, which is the control number, can be calculated through the stuff that is in strPartial. Each character (integer) in strPartial needs to be multiplied with either 1 or 2, depending on its position. The first int is multiplied with 2, the next with 1, the next with 2 and so on.
If the product is 16 (8*2) the added result is 1+6, not 16. That's why I add the array to keep hold of my results.
The code up above does give me a number, but it's rarely the correct one. Does anyone understand my problem or have a clue on what is the problem with my code. I'm soon going insane over this...answers appreciated. Please ask additional questions if you don't understand what I'm trying to calculate.
give me a number and what the correct answer should be and i'll see if i can find the problem...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Pl give what input given, exactly what output expected?!?!?!
And your else part is wrong...
intresult = intresult+intresult
will give you intresult = 32 if input intresult value is 16.
so modify it to
remainder = intresult mod 10
intresult = remainder + ((intresult - remainder)/10)
And your else part is wrong...
intresult = intresult+intresult
will give you intresult = 32 if input intresult value is 16.
so modify it to
remainder = intresult mod 10
intresult = remainder + ((intresult - remainder)/10)
Pl ignore my previous post
I just now understood a little more of your program
1. REMOVE THE BIG LETTERS IN THE FOLLOWING CODE
ACTUALLY THERE IS NO NEED FOR ELSE PART
intResult = INTRESULT+ intParts(0) + intParts(1)
Else
intResult = INTRESULT+ intResult
End If
2. yu have not initialized k
give
k = 1 before the following line
For l = 0 To UBound(intNumbers)
I just now understood a little more of your program
1. REMOVE THE BIG LETTERS IN THE FOLLOWING CODE
ACTUALLY THERE IS NO NEED FOR ELSE PART
intResult = INTRESULT+ intParts(0) + intParts(1)
Else
intResult = INTRESULT+ intResult
End If
2. yu have not initialized k
give
k = 1 before the following line
For l = 0 To UBound(intNumbers)
k needs to be 0 at first, so he's fine there...
ASKER
I solved it myself before I read here, but as you say I messed it up with not storing whatever came out of the loop each time. I merely added a new variable storing the results continueously. Thanks.