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

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the execution of a command within a recursive algorithm, specifically focusing on whether the command associated with the condition "if A[1] == 5" will execute during subsequent calls of the algorithm. The scope includes algorithmic reasoning and understanding of array indexing in recursive functions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants suggest that the command will execute, but the outcome depends on the value of A[1] at the time of the check.
  • There is a question about whether A[1] refers to the first element of the original array or the first element of the subarray in the recursive call.
  • One participant provides an example with an array A = {4, 5, 6, 2, 3, 1} to illustrate that A[1] remains unchanged during the recursive calls.
  • Another participant confirms that the value of A[1] does not change because the algorithm modifies indices rather than the array itself.

Areas of Agreement / Disagreement

Participants generally agree that A[1] does not change during the recursive calls, but there is uncertainty regarding the execution of the command based on the condition of A[1] and its implications in the context of the algorithm.

Contextual Notes

The discussion highlights the dependence on the initial state of the array and the implications of recursive calls on indexing, which may lead to confusion about the values being referenced.

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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
Replies
9
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K