Question

What is the value of n and counter when the code below is executed? n =...

What is the value of n and counter when the code below is executed?

n = 256; 
counter = 1; 
while n ~= 1 
 if rem(counter,2) == 0 
 n = n/2; 
 else 
 n = n/4; 
 end 
counter = counter + 1 ;
end

a.

n=1;

counter = 6

b.

n=5;

counter = 6;

c.

n=6;

counter = 256;

d.

n=256

counter = 6

e.

n=128;

counter=64;

Homework Answers

Answer #1

counter = 1
n = 256

while 256 != 1
   1%2 =1, so
       n = n/4 = 64
       counter = 2
   2%2 = 0
       n = n/2 = 32
       counter = 3
   3%2 = 1
       n = n/4 = 8
       counter = 4
   4%2 = 0
       n = n/2 = 4
       counter = 5
   5%2 = 1
       n = n/4 = 1
       counter = 6

breaks the loop because condition fails for n is 1

So, n = 1
counter = 6

Answer : A

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What is the value of n and counter when the code below is executed? n =...
What is the value of n and counter when the code below is executed? n = 256; counter = 1; while n ~= 1 if rem(counter,2) == 0 n = n/2; else n = n/4; end counter = counter + 1 ; end a. n=1; counter = 6 b. n=5; counter = 6; c. n=6; counter = 256; d. n=256 counter = 6 e. n=128; counter=64;
What is the value of n and counter when the code below is executed? n =...
What is the value of n and counter when the code below is executed? n = 256; counter = 1; while n ~= 1 if rem(counter,2) == 0 n = n/2; else n = n/4; end counter = counter + 1 ; end a. n=1; counter = 6 b. n=5; counter = 6; c. n=6; counter = 256; d. n=256 counter = 6 e. n=128; counter=64;
Below is C code and Python code for an algorithm. C code: void foo( int n,...
Below is C code and Python code for an algorithm. C code: void foo( int n, int A, int B, int C ) { if( n==1 ) { printf("%d to %d\n",A,B); return; } foo( n-1, A, C, B ); printf("%d to %d\n",A,B); foo( n-1, B, C, A ); Python code: def foo(n , A, B, C): if n==1: print A, "to", B return foo(n-1, A, C, B) print A, "to", B foo(n-1, B, C, A) Let Hn be the number...
Take a look at the code below. What is the problem with the code? Change the...
Take a look at the code below. What is the problem with the code? Change the code so that it will run properly. (10 points) i = -6 # Code the while loop while i != 0 :     print("Output 1")     if i>0 :           print("Output 2")     else :           print("Output 3")    print(offset)
1. How many times the loop below will be executed? What is the value of variable...
1. How many times the loop below will be executed? What is the value of variable counter after the loop? counter = 1 while counter < 7: counter+=2 print(counter) 2. An integer is stored in a variable MyInt. Write the Python if statement to test if MyInt is even number or odd number, and print a message to show “It is even number.” or “It is an odd number”. 3. Complete the python below that asks user to enter the...
Machine Language - 1. Which of the following assembly code represents the high-level Java code below?...
Machine Language - 1. Which of the following assembly code represents the high-level Java code below? int x; int i = 5; if (i < 0) x = -1; else x = 1; @5 D=M @i M=D @BRANCH M;JLT @x M=1 @END 0;JMP (BRANCH) @x M=-1 (END) @END 0;JMP @5 D=M @i M=D @BRANCH M;JGE @x M=1 @END 0;JMP (BRANCH) @x M=-1 (END) @END 0;JMP @5 D=M @i M=D @BRANCH M;JLT @x M=1 @END 0;JMP (BRANCH) @x M=-1 (END) @END...
In Java 1. What will be the value of x after the following section of code...
In Java 1. What will be the value of x after the following section of code executes: int x = 3; if (x > 3)     x = x – 2; else     x = x + 2; A. 1 B.3    C.5              D.7 2. What will be the value of y after the following section of code executes: int y = 5, z = 3; if (y > 4){      z = 2;      y = y – 1;...
(C++ program) Use the code segment below to answer the two questions that follow. … int...
(C++ program) Use the code segment below to answer the two questions that follow. … int size = 0; cout << “Enter size: “; cin >> size; int sizeCode = size / 10; if (sizeCode == 0)    cout << “extra small”; else if(sizeCode == 1 )    cout << “small”; else if (sizeCode == 2)    cout << “medium”; else if (sizeCode == 3)    cout << “large”; else    cout << “extra large”; //end if … What would...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
What would be the result after the following code is executed? final int SIZE = 25;...
What would be the result after the following code is executed? final int SIZE = 25; int[] array1 = new int[SIZE]; // Code that will put values in array1 int value = 1; for (int a = 0; a < array1.length; a++) { array1[a] = value; value = value + array1[a]; }