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...
(Great Circle Distance) Write a program great_circle.py that takes four floats x 1 , y 1...
(Great Circle Distance) Write a program great_circle.py that takes four floats x 1 , y 1 , x 2 , and y 2 (latitude and longitude in degrees of two points on Earth) as command-line arguments and writes the great-circle distance (in km) between them, given by the equation d = 111 arccos(sin(x 1 ) sin(x 2 ) + cos(x 1 ) cos(x 2 ) cos(y 1 − y 2 )). $python3 great_circle .py 48.87 -2.33 37.8 -122.4 8701.389543238289
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...
Write a C++ code that keeps reading integer numbers (x) until a negative number is entered....
Write a C++ code that keeps reading integer numbers (x) until a negative number is entered. Every time x is entered, the program does the following: calculates and prints the product of odd numbers. calculates and prints the count of numbers that ends with 19. Example, 19, 3219, 50619,....etc calculates and prints the percentage of the numbers that contain 3 digits exactly, such as, 301, 500, 206,...etc.
Write a pseudo code program for a goal-based agent. The goal of the agent is to...
Write a pseudo code program for a goal-based agent. The goal of the agent is to find the exit of a labyrinth. The agent is not omniscient The agent can sense if it is next to a wall (in front, left or right) The agent can turn 90 degrees to the right or left The agent can drive 1unit forward The maze is constructed of paths that are 1 unit across (wide) Show a maze of your choosing and illustrate...
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...
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, …
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT