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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top