Question

Find the exact number of times (in terms of n) the innermost statement (X = X...

  1. Find the exact number of times (in terms of n) the innermost statement (X = X + 1) is executed in the following code. That is, find the final value of X. Then express the total running time in terms of O( ), Ω( ), or Θ( ) as appropriate.

X = 0;

for k = 1 to n

    for j = 1 to n – k

        X = X + 1;

  1. The following program computes and returns (log2n), assuming the input n is an integer power of 2. That is, n=2j for some integer j ≥ 0.

int LOG (int n){

int m, k;

m = n;

k = 0;

while (m > 1) {

    m = m/2;

    k = k + 1; }

ruturn (k)

}

  1. First, trace the execution of this program for a specific input value, n = 16. Tabulate the values of m and k at the beginning, just before the first execution of the while loop, and after each execution of the while loop.

  1. Prove by induction that at the end of each execution of the while loop, the following relation holds between variables m and k. (This relation between the variables is called the loop invariant.)

m=n/2k

  1. Then conclude that at the end, after the last iteration of the while loop, the program returns k=log2n

Homework Answers

Answer #1

SOLUTION -


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

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
*********I need question 6 answered which is from question 5 which is ********* Question 5 :...
*********I need question 6 answered which is from question 5 which is ********* Question 5 : Program Correctness I (1 point) Use the loop invariant (I) to show that the code below correctly computes n P−1 k=0 2k (this sum represents the sum of the first n even integers where n ≥ 1). Algorithm 1 evenSum(int n) 1: p = 2(n − 1) 2: i = n − 1 3: while i > 0 do 4: //(I) p = nP−1...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
int main() { while (1) { float d; scanf("%f", &d); float x; for (x = d;...
int main() { while (1) { float d; scanf("%f", &d); float x; for (x = d; x <= d + 1000.0; x = x + 1000.0) { } printf("loop exited with x = %.14g\n", x); } return 0; } If you run the program, you will see. What number should I use as an input to make this an infinite loop?
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...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A): A is a list of n integers 1 for i = 1 to n-1 do 2   x=aix=ai 3 j = i − 1 4 while (j ≥≥ 0) do 5 if x≥ajx≥aj then 6 break 7 end if 8   aj+1=ajaj+1=aj 9 j = j − 1 a end while b   aj+1=xaj+1=x c end for d return A The complexity of this algorithm is O(n2)O(n2)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT