Yes
The alogorithm terminates if x=9
while(9!=1) which is true
so it iterates
if(9%2==0) it was not true so goes to else block
than x=3*9+1 which is 28
again go to while block
while(28!=1) which is true so it enter into while block
if(28%2==0) it was true than
x=28/2 which is 14
now again goes to while loop
while(14!=1) which is true so enter into while loop than
if(14%2==0) which it true than it goes
x=14/2 which is 7
again it goes to while loop
than while(7!=1) which is true than
if(7%2==0) which is not true so it goes to else block
than
x=3*7+1 which is 22
now again it goes to while block
while(22!=1) true than
if(22%2==0) true than
x=x/2 which is 11
than again goes to while
while(11!=1) true
if(11%2==0) not true
so it goes else block
x=11*3+1 which is 34
now
while(34!=1) which true
than if(34%2==0) which is true
x=34/2 which is 17
than again goes to while
if 17%2==0 not true
x=17*3+1 which is 52
than again goes while than
if 52%2==0 true than
x=52%2 which is 26
than again goes to while than
if 26%2==0 true
than x=26/2 which is 13
than again goes to while loop than
if 13%2==0 not true
so x=3*13+1 which is 40 than again goes to while loop
if 40%2==0 which is true
than x=40/2 which is 20
than again goes to while loop
if 20%2==10 true
than x=20/2 which is 10
if 10%2==0 true
than x=10/2 which is 5
5%2==0 not true
than x=3*5+1 which is 16
16%2==0 true
than x=16/2 which is 8
8%2==0 true
than x=8/2 which is 4
4%2==0 true
than 4/2 which is 2
2%2==0 true
than 2/2 which is 1 it goes again while loop and check
while(1!=1) which is not true so while loop is terminated
#if you have any doubt or more information needed comment below.. i will respond as possible as soon..if you like give thumbs up..thanks..
(10 points) Does the following algorithm terminate when x = 9? Show your work. def mystery...
(10 points) Translate the following algorithm into assembly language. IF 65 < X THEN X = 9+(X^2) ELSE X = - Y 3. (10 points) Translate the following algorithm into assembly language. X=0 FOR K = I TO 5 X=X+5 END_FOR 4. (10 points) Translate the following algorithm into assembly language. X=0 Y=5 Z=5 WHILE Z = 5 X=X+1 Y=Y-1 Z=X+Y 2. (10 points) Translate the following algorithm into assembly language. IF65 < X THEN X = 9t(X^2) ELSE X...
Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is a sorted list index mystery (index low, index high, const Array S[], number x) if low S high then mid = (low + high) / 2 if x = Smid] then return mid elsif x < s[mid] then return mystery (low, mid-1, s, x) else return mystery (mid+1, high, s, x) else return 0 end What does the recursive algorithm above compute? Explain why?
pleas answer asap 3. (20 points) Algorithm Analysis and Recurrence There is a mystery function called Mystery(n) and the pseudocode of the algorithm own as below. Assume that n 3* for some positive integer k21. Mystery (n) if n<4 3 for i1 to 9 5 for i-1 to n 2 return 1 Mystery (n/3) Print "hello" 6 (1) (5 points) Please analyze the worst-case asymptotic execution time of this algorithm. Express the execution time as a function of the input...
2. Measure the complexity of the following algorithm: SHOW your work. (15 points) a=1 b 3: for (i = 0, i<= n, i++) d-d-1 for (j:0, j <= n, j++) c=a+b;
a. Argue that the algorithm always terminates, that is, that the algorithm does not call itself recursively until all eternity. (Hint: Prove that the value of y in Line 9 is less than the value of x at the beginning of the procedure and draw the right conclusions.) b. Prove that the algorithm always gives the correct answer. (Hint: Use induction. For the inductive step, argue that y is divisible by 9 if and only if this is true for...
Consider the following code segment: int[][] mystery = new int[3][3]; int counter = 0; while(counter < mystery.length) { for(int col = 0; col < mystery[counter].length; col++) { if(counter == 0) { mystery[col][counter] = 1; } else if(counter == 1) { mystery[counter][col] = 2; } else { mystery[counter][counter] = 3; } } counter++; } What will the value of each element in mystery be after the execution of the code segment? a) {{1, 0, 0} {2, 2, 2} {1, 0, 3}}...
5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns igned int a, unsigned int b): // function prototype 5 int main(void) printf("%s". "Enter two positive integers: unsigned int x: I/ first integer unsigned int y: // second integer scanf("Su%u". &x, &y); "); 12 13 14 15 II Parameter b must be a positive integer 16 to prevent infinite recursion 7 unsigned int mystery Cuns igned int a, unsigned int b) 18 printf("The result...
Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...
Show the steps of the binary search algorithm (pseudocode is given below); low = index of first item in list high = index of last item in list while low is less than or equal to high mid = index halfway between low and high if item is less than list[mid] high = mid - 1 else if item is greater than list[mid] low = mid + 1 else return mid end while return not found For each step of...
9. (5 points) Please describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. 10. (5 points) Please devise an algorithm that finds all modes. (Recall that a list of integers is nondecreasing if each term of the list is at least as large as the preceding term.) 11. (5 points) Please find the least integer n such that f() is 0(3") for each of these functions f()...