If statement to choose a value - excel

Hi I have the following code:
If Numcolyo = Even Then
.StartPoint.y = BuWidth / 2 + ccFacoy
Else
.StartPoint.y = BuWidth / 2
End If
But it doesn't do what I expect it to do. The code works though. Say for example if Numcolyo=4 then I want the first statement to be true: StartPoint.y = BuWidth / 2 + ccFacoy
If Numcolyo=3 then I want the second statement to be true: StartPoint.y = BuWidth / 2
Have I written anything wrong? Thankful for any kind of help :)

Don't think there is an EVEN keyword in VBA. Try it with the modulus operator which returns the remainder of a division operation:
If Numcolyo Mod 2 = 0 Then
.StartPoint.y = BuWidth / 2 + ccFacoy
Else
.StartPoint.y = BuWidth / 2
End If
When something divided by 2 has no remainder, it is even.

Related

Why Does This While Loop With A Condition Hang?

Important Updates: Code line 12 changed from "universalTracker + 2" to "universalTracker += 2"
I'm doing the adjacentElementsProduct challenge on CodeSignal, and it's asking me to create a function that shows the highest product from each adjacent pair of numbers in the list. I wrote this, and I can't seem to find what's wrong with it. Once I press run in VSCode, it just hangs. I have to force quit the program.
def adjacentElementsProduct(inputArray):
highestProduct = 0
universalTracker = 0
firstNum = inputArray[universalTracker]
secondNum = inputArray[universalTracker + 1]
arrayLength = len(inputArray)
while universalTracker != (arrayLength - 1):
currentProduct = firstNum * secondNum
if currentProduct > highestProduct:
highestProduct = currentProduct
universalTracker += 2
adjacentElementsProduct([3, 6, -2, -5, 7, 3])
If anyone could give me an explanation as to what I did wrong, a working solution, and why it works, I would greatly appreciate it.
It's simple
Just change while loop condition to
while universalTracker >= (arrayLength - 1):
Because, let's say your array length is 5, and universalTracker is updating by 2, it won't be equal to 5
It will be 2,4,6,8,10,12 ... and so on.
Let me know if this makes sense..
If you notice, you are doing the following inside the while loop.
universalTracker + 2
This isn't really updating the variable that you intended to update.
To make it work, you should do
universalTracker = universalTracker+2
Or equivalently
universalTracker += 2

A logic error within a quick binary search solution to find square root but fail to capure with enormous testings

I have such a binary search to find square root of a positive integer
In [95]: find_square_root??
Signature: find_square_root(x)
Docstring: <no docstring>
Source:
def find_square_root(x):
if x < 2:
return x
lo = 0
hi = x
while lo < hi:
mid = (lo + hi) // 2 #
if mid ** 2 == x:
lo = mid
return lo
if mid ** 2 < x:
lo = mid + 1
if mid ** 2 > x:
hi = mid
print(f"mid={mid}, lo={lo}, hi={hi}")
return lo -1
File: /tmp/ipython_edit_um5dfgck/ipython_edit_sdk9u57g.py
Type: function
I tested up to 50**5 cases which works properly
for i in range(50, 50**5):
res = find_square_root(i**2)
assert res == i, f"res={res}, i={i}"
However, there exist a logic bug there roughly.
Suppose only two numbers left finally, lo and hi which are adjacent to each other surely and they are not tested yet.
According to the algorithms, mid = (lo + hi) // 2, since it floor division, mid is actually equals to lo, so one of the left two number is tested,
additionally if mid ** 2 > x:, then hi = mid = lo,
this way, the function quit safely.
However, if if mid ** 2 < x:, then lo = mid + 1 which means lo = hi and the loop quit with hi left untested.
It seems like a solid logic bug.
but I am not sure because it passed mass of testings.
However, if mid ** 2 < x:, then lo = mid + 1 which means lo = hi and the loop quit with hi left untested.
It seems like a solid logic bug.
There is no bug here. hi is not left untested, as hi is the value that mid had in a previous iteration, when hi = mid was executed, and so it was already tested there.
If there was never such previous iteration where hi was modified, then this means hi equals x, and that in each iteration mid ** 2 < x is true, until and including when mid == x - 2 (which would assign li = x - 1). But this can only happen when x < 4. In those few cases the solution is 1. As we know x > 1 after the first if, we have hi > 1, so no problem for those cases either. You could even change the first if to if x < 4...
The way to think about it is that hi is one past the end of the range you're searching. So the numbers being searched are the range lo to hi-1, and there is never any need to test hi. Once lo == hi, the range is empty so the number wasn't found, and there is no need to continue testing. So there is no logic error in this code.

Visual Basic create loop for counting even numbers

The program requires input of positive numbers, and counts each even number using a loop function that doesn't count odds and ends if a O is input.
I'm not sure how to go about creating a loop or if I can use an if function within the loop.
Dim Count = 0
While (number mod 2 = 0) do
Count + 1 = Count
I actually didn't understand the question very well but as far as concerned, if you dont want odd numbers to be included I suggest on count add 2 not one , since the count variable starts with zero do:
Dim Count+2
Btw when do you want the count to stop? At 2 And goes back to 0?
If so then use the if statement
var Dim_count = 0;
if(Dim_count == 0){Dim_count+2}
else if(Dim_count ==2){Dim_Count =0;}
It would help if you provide a sample input so we can work with actual code and guide you to the right solution.
If you receive the input as, let's say, array of numbers, you can simply loop trough it using for or foreach and add additional condition to check for 0 if you want to preliminary exit:
For Each number As Integer In numbers
If (number mod 2 = 0) Then
Count = Count + 1
End If
If (number = 0) Then
Exit For
End If
Next
If you have existing code in which somehow number is reinitialized/redefined on each iteration already, then what you have is pretty close to what you need:
While (number <> 0)
If (number mod 2 = 0) Then
Count = Count + 1
End If
End While
Function counts even numbers:
REM function gets input, exits at 0 and adds positive even numbers.
DO
INPUT X
IF X = 0 THEN PRINT Y; " even numbers": END
IF X > 0 THEN
IF X / 2 = X \ 2 THEN Y = Y + 1
END IF
LOOP
I am completely unsure about this. just a guess...
Dim j as Integer
Dim i As integer
j = 0
i = 2
For i = 1 to 100
j = j+i
Print j
Loop
End Sub
Assuming you are getting numbers from some input, this is how you can do it. Have an infinite loop with While True, then for every number given from your input, check if its even using number mod 2 = 0. This will go on forever so you need to add some condition (another if statement) for it to stop the while loop. More information about while loops here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/while-end-while-statement
Dim Count = 0
While True do
If (number mod 2 = 0) Then
Count + 1 = Count
End If
End While

How do I convert a 4 digit number into individual digits?

I need to write logic to break down a 4 digit number into individual digits.
On a reply here at SO to a question regarding 3 digits, someone gave the math below:
int first = 321/100;
int second = (321/10)-first*10;
int third = (321/1)-first*100-second*10;
Can someone help me?
Thank you in advance!
Well, using the sample you found, we can quite easily infer a code for you.
The first line says int first = 321/100;, which returns 3 (integer division is the euclidian one). 3 is indeed the first integer in 321 so that's a good thing. However, we have a 4 digit number, let's try replacing 100 with 1000:
int first = 4321/1000;
This does return 4 !
Let's try adapting the rest of your code (plus I put your four digit number in the variable entry).
int entry = 4321;
int first = entry/1000;
int second = entry/100 - first*10;
int third = entry/10 - first*100 - second*10;
int fourth = entry - first*1000 - second*100 - third*10;
second will be entry/100 (43) minus first*10 (40), so we're okay.
third is then 432 - 400 - 30 which turns to 2. This also works till fourth.
For more-than-four digits, you may want to use a for-loop and maybe some modulos though.
This snip of code counts the number of digits input from the user
then breaks down the digits one by one:
PRINT "Enter value";
INPUT V#
X# = V#
DO
IF V# < 1 THEN
EXIT DO
END IF
D = D + 1
V# = INT(V#) / 10
LOOP
PRINT "Digits:"; D
FOR L = D - 1 TO 0 STEP -1
M = INT(X# / 10 ^ L)
PRINT M;
X# = X# - M * 10 ^ L
NEXT
END

Simple recursive function in VBA with Excel not returning expected result

I have a simple recursive function to write in VBA that does the following : It must count the number of times we must take the log of a parameter 'x' to find log(x) < 1
Examples :
logcount(5) : log(5) = 0,6... so the function should return 1
logcount(89) : log(89) = 1,9... and log(log(89)) = 0,28... so the function should return 2
logcount(0,4) should return 1
etc...
So I wrote it and it doesn't work as expected ! It always adds +1 to the result ! It looks like the last 'Else' block is always interpreted. Any help will be really appreciated
Function logcount(x As Double) As Integer
If x <= 0 Then
MsgBox "You must enter a positive value"
Exit Function
ElseIf Log(x) < 1 Then
logcount = 1
Else
logcount = 1 + logcount(Log(x))
End If
End Function
Log in VBA is the natural logarithm.
Apparently you meant a base-10 logarithm:
Log10 = Log(X) / Log(10#)

Resources