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

  • MHB
  • Thread starter evinda
  • Start date
In summary, the Algorithm will call itself recursively, and the command of the if statement will be executed if the first element of the subarray is equal to 5. The value of A[1] will not change during the process.
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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:
  • #3
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)
 
  • #4
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.
 
  • #5
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)
 
  • #6
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.
 

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

1. Will the command be executed immediately?

The answer to this question depends on the specific command being executed. Some commands may be executed immediately, while others may require additional steps and may not be executed immediately.

2. Can the command be canceled once it has been executed?

Again, this depends on the command being executed. Some commands may be able to be canceled, while others may not have the option to be reversed once they have been executed.

3. What happens if the command is not executed successfully?

If the command is not executed successfully, it may result in an error message or the command may not have any effect. It is important to carefully consider the potential consequences before executing any command.

4. Is there a way to undo the command once it has been executed?

This depends on the command and the specific application or program being used. Some commands may have an undo function, while others may not be reversible.

5. How can I ensure that the command will be executed successfully?

The best way to ensure successful execution of a command is to carefully read and follow any instructions or guidelines provided by the application or program. Additionally, checking for any potential errors or conflicts before executing the command can also increase the chances of success.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
10
Views
878
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
6K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top