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

Here the answer is solved witth the following steps.

Step 1: At first the answer the given with correct option.

Step 2: After that the explanation of the answer is given point wise in details.

Stpe 3: A complete matlab code with screen shot and output is given. From the output the explanation is verified.

Correct option: a) n=1;

                                          counter = 6

   

Explanation:

i. n=256, counter=1

As n is not equal to 1

Here, mod (counter, 2) is not equal to 0, because mod (1, 2)=1, so else condition satisfies,

Then, n=n/4

           n=256/4=64

counter=counter+1 or counter=1+1=2

ii. n=64 , counter=2

now again n is not equal to 1

As, rem(counter,2) is equal to 0, because mod(2,2)=0, so if condition satisfies,

Then n=n/2

          n=64/2=32

counter=counter+1 or counter=2+1=3

iii. n=32, counter=3

As n is not equal to 1

Here, mod (counter, 2) is not equal to 0, because mod (3, 2)=1, so else condition satisfies,

Then, n=32/4

           n=32/4=8

counter=counter+1 or counter=3+1=4

iv. n=8 , counter=4

now again n is not equal to 1

As, rem(counter,2) is equal to 0, because mod(4,2)=0, so if condition satisfies,

Then n=n/2

          n=8/2=4

counter=counter+1 or counter=4+1=5

v. n=4, counter=5

As n is not equal to 1

Here, mod (counter, 2) is not equal to 0, because mod (5, 2)=1, so else condition satisfies,

Then, n=n/4

           n=4/4=1

counter=counter+1 or counter=5+1=6

vi. Now n =1, so, while loop will not be executed and loop will be broken.

So, finally, n=1 and counter=6

Answer: n=1, counter=6

HERE THE ABOVE EXPLAINED PROGRAM IS DONE WITH COMPLETE MATLAB PROGRAMMING LANGUAGE.

n = 256;
counter = 1;
while n ~= 1
if rem(counter,2) == 0
n = n/2;
else
n = n/4;
end
counter = counter + 1 ;
end
fprintf('n=%d\ncounter=%d\n',n,counter);

SCREEN SHOT

OUTPUT

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
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)
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]; }
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b,...
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b, and c is stored in registers X19, X20, and X21 respectively. Base address of d is stored in register X22. Comment your assembly code. (24 Points) a. if (a > b) d[a] = b + 8; else d[a] = b - 16; b. for (i=0;i<a; i++) a = d[i] + c; c. i = 0; while ( d[i] == b) if(( a - i...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 What is the output after the code executes? Assume there are no syntax errors and the code will compile. int x = 10; do { System.out.println ( x ); x -= 3; } while (x > 0); A. 10 7 4 1...
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Provide specific implications for Marketing – explain exactly how a marketer of any product can reach...
    asked 2 minutes ago
  • Explain the similarities and differences between: (a) repeating sequences, (b) arithmetic sequences, and (c) geometric sequences....
    asked 7 minutes ago
  • Because many passengers who make reservations do not show​ up, airlines often overbook flights​ (sell more...
    asked 49 minutes ago
  • Some gang members have survived being shot multiple times, whereas others die from the first, not...
    asked 1 hour ago
  • One surface of a thick aluminum block (α = 97.1 × 10−6 m2/s, k= 237 W/m...
    asked 1 hour ago
  • 1. Measurements from a single-lane roadway reveal that the average spacing and headway between passing vehicles...
    asked 1 hour ago
  • an Astronomy question, please answer it comprehensively. Thank you Astronomers cannot map all of the material...
    asked 2 hours ago
  • the Organizational Theory there are four theoretical contributions which are central to the understanding of today's...
    asked 2 hours ago
  • Give an xbox code of length 10 snake Any kind of spam answers will be reported...
    asked 2 hours ago
  • find the sun of the series sigma n=0 to infinity 2(3^n/2 -2)/7^n+1 + sin ( (n+1)pi...
    asked 2 hours ago
  • In Econoland in 2005, people with incomes between $20,000 and $30,000 paid 12 percent of their...
    asked 3 hours ago
  • Required information [The following information applies to the questions displayed below.] Warnerwoods Company uses a perpetual...
    asked 3 hours ago