MHB Will the Command in the Algorithm Execute if A[1]==5?

  • Thread starter Thread starter evinda
  • Start date Start date
AI Thread Summary
The discussion revolves around the execution of a command within a recursive algorithm that checks if the first element of an array equals 5. When the function is called with updated indices, the value of A[1] remains unchanged, as the algorithm operates on the indices rather than modifying the array itself. Consequently, the command associated with the if-statement will be executed based on the original value of A[1], which does not change regardless of the recursive calls. The key takeaway is that the logical result of the if-statement depends solely on the initial state of the array, not on the subarray created during recursion.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

Consider that we have an algorithm of the form:

Code:
Algorithm(A[1...n], low, high){
    mid=low+floor((high-low)/2);
    if (A[1]==5) { command }
    commands
    Algorithm(A,mid+1,high)
}

When we call
Code:
Algorithm(A,mid+1,high)
will the command of the if statement (if A[1]==5) be executed? (Thinking)
 
Technology news on Phys.org
evinda said:
Hello! (Wave)

Consider that we have an algorithm of the form:

Code:
Algorithm(A[1...n], low, high){
    mid=low+floor((high-low)/2);
    if (A[1]==5) { command }
    commands
    Algorithm(A,mid+1,high)
}

When we call
Code:
Algorithm(A,mid+1,high)
will the command of the if statement (if A[1]==5) be executed? (Thinking)

It will be executed but the logical result of the if statement depends on the array it self (Is the first value equal to 5?) .

Am I misunderstanding your question ?
 
Last edited:
ZaidAlyafey said:
It will be executed but the logical result of the if statement depends on the array it self (Is the first value equal to 4 ?) .

Am I misunderstanding your question ?

If we call the function
Code:
 Algorithm(A,mid+1,high)
will the first element of the subarray be equal to $A[1]$ or to $A[mid+1]$?

So, will the command of the if-statement be executed or not? (Thinking)
 
evinda said:
If we call the function
Code:
 Algorithm(A,mid+1,high)

will the first element of the subarray be equal to $A[1]$ or to $A[mid+1]$?

So, will the command of the if-statement be executed or not? (Thinking)

Code:
Let the following 
A = {4 , 5 , 6 , 2 , 3 , 1} 
If we call Alogrithm(A , 1 , 6);
mid = 3;
A[1] = 4 

in the second call 

Alogrithm (A , 4 , 6)
mid = 5
A[1] = 4 // no change in the value of the array.
 
ZaidAlyafey said:
Code:
Let the following 
A = {4 , 5 , 6 , 2 , 3 , 1} 
If we call Alogrithm(A , 1 , 6);
mid = 3;
A[1] = 4 

in the second call 

Alogrithm (A , 4 , 6)
mid = 5
A[1] = 4 // no change in the value of the array.

So, the value of A[1] will not change, right? (Thinking)

Thanks a lot! (Smile)
 
evinda said:
So, the value of A[1] will not change, right? (Thinking)

Thanks a lot! (Smile)

Yes , because we are not changing the value we are changing the indices.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top