Question

In: Computer Science

1. Here is a pseudo-code of a program that takes two non-negative numbers X and Y...

1. Here is a pseudo-code of a program that takes two non-negative numbers X and Y as its inputs and outputs a number Z. Write this program in C. What does this program do? Explain how it computes the output Z from its inputs X and Y. Determine the big-Oh of this program in terms of its input sizes.                                                                

f (int x, int y)

{

     z = 0; k = y; w = x;

     while (k != 0)

{

          if (k % 2 == 1)

             z = z + w;

             k = k/ 2;

           w = 2 * w;

     }

}

Solutions

Expert Solution

let x=4,y=7

k=7; z=0; w=4;

while(k!=0)

k=7 so,k%2==1 =>z=0+4=4;

k becomes 3

w becomes w = 4*2=8

so,again 3%2==1 => so, z=4+8=12;

k becomes 1

w becomes w=8*2=16

again 1%2==1 so, z=12+16=28;

k becomes 0 and stops and the final z is 28

Timecomplexity for this program is

The lines z=0,k=y,w=x takes O(1) for each assignment so it becomes O(1)+O(1)+O(1)

The while(k!=0) takes O(log n) becauese the k value is changing in terms of k/2 so,the time complexity for this series will be log n.

The if statement takes comparision of one value so,it is O(1)

The addition also takes constant time.

division and multiplication statements takes O(1) for each

so for the entire loop the time complexity will be

O(log n) *(O(1)+O(1)+O(1)+O(1))= O(4 log n)

the entire time complexity wil be O(4 log n)+4

The constant 4 will become less and less significant to the final result so,we ignore that.It again takes O(4*log n)

In this the coefficient 4 becomes insignificant as log n gets large.

So,we the time complexity for this is O(log n)


Related Solutions

Problem 1: Utility maximization There two goods, X and Y , available in arbitrary non-negative quantities...
Problem 1: Utility maximization There two goods, X and Y , available in arbitrary non-negative quantities (so the consumption set is R2+). A consumer has preferences over consump- tion bundles that are strongly monotone, strictly convex, and represented by the following (differentiable) utility function: u(x, y) = x + xy + y, where x is the quantity of good X and y is the quantity of good Y . The consumer has wealth w > 0. The price for good...
Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print...
Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red....
Write a MIPS assembly language program that implements the following pseudo-code operation: result = x +...
Write a MIPS assembly language program that implements the following pseudo-code operation: result = x + y – z + A[j] x and y should be in reserved memory words using the .word directive and labeled as x and y. Initialize x=10 and y=200. Read in z from the console. Input the value -8. This is the value for z, not for –z. Store this value in memory with the label z. To begin, you could just initialize z to...
Write a Python program that takes as input two numbers, the height, and width of a...
Write a Python program that takes as input two numbers, the height, and width of a rectangle. It then prints a rectangle with height lines and width characters in each line. The rectangle should contain information regarding its dimensions and area inside (centered vertically but not horizontally). If any part of the information (including two stars on each end and a space before and after the line) does not fit in the rectangle, then print the complete information after the...
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
1. Let X and Y be non-linear spaces and T : X -->Y. Prove that if   ...
1. Let X and Y be non-linear spaces and T : X -->Y. Prove that if    T is One-to-one then T-1 exist on R(T) and T-1 : R(T) à X is also a linear map. 2. Let X, Y and Z be linear spaces over the scalar field F, and let T1 ϵ B (X, Y) and T2 ϵ B (Y, Z). let T1T2(x) = T2(T1x) ∀ x ϵ X. (i) Prove that T1T2 ϵ B (X,Y) is also a...
Two numbers x and y that are not machine numbers are read into a 32-bit word-length...
Two numbers x and y that are not machine numbers are read into a 32-bit word-length computer. The machine computes to xy2. what sort of relative error can be expected? Assume no underflow or overflow
y'=y-x^2 ; y(1)= -4 Write a MATLAB program that makes two plots of the solution to...
y'=y-x^2 ; y(1)= -4 Write a MATLAB program that makes two plots of the solution to the equation using the following values. Suggest you use nested loops instead of two different loops. Be sure to label your plots. a. x0 = 1.0, step size h = .2, number of steps n = 20. b. x0 = 1.0, step size h = .05, number of steps n = 80.
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT