Skip to main content
Loading...
20% Off your first order         +1 309 992 1394

Time Complexity of Loops Worksheet

Time Complexity of Loops Worksheet

Time Complexity of Loops Worksheet

 

Paper Instructions

(Q3 g: t =0; t = t-1) printf("B"); for-t: TC1iter(___) = _________________________ dependent / independent of loop variable Change of var: __________________________________________________________ ∑ / repetitions ______________________________________________________ Closed form: _______________________________ Θ(_______________) for-k: TC1iter(___) = _________________________ dependent / independent of loop variable Change of var: __________________________________________________________ ∑ / repetitions ______________________________________________________ Closed form: _______________________________ Θ(_______________) e) (10 points) Assume that void some_fct(int X); has time complexity Θ(X) for(k = 0; k <= N; k = k+1) some_fct(k); TCsome_fct(k) = Θ( ________ ) (time complexity of the function call) for-k: TC1iter(___) = _________________________ dependent / independent of loop variable Change of var: __________________________________________________________ ∑ / repetitions ______________________________________________________ Closed form: _______________________________ Θ(_______________) 4 f) (10 points) Assume that void some_fct2(int N, int k); has time complexity Θ(N2) for(k = 1; k <= M; k++) { some_fct2(k,N); } TCsome_fct2(k,N) = Θ( ________ ) (time complexity of the function call) for-k: TC1iter(___) = _________________________ dependent / independent of loop variable Change of var: __________________________________________________________ ∑ / repetitions ______________________________________________________ Closed form: _______________________________ Θ(_______________) g) (10 points) Assume that void some_fct3(int N); has time complexity Θ(N3) for(k = 1; k <= M; k=k+1) { some_fct3(S); for(t = 0; t < M; t = t+1) printf("X"); } TCsome_fct3(S) = Θ( ________ ) (time complexity of the function call) for-t: TC1iter(___) = _________________________ dependent / independent of loop variable Change of var: __________________________________________________________ ∑ / repetitions ______________________________________________________ Closed form: _______________________________ Θ(_______________) for-k: TC1iter(___) = _________________________ dependent / independent of loop variable Change of var: __________________________________________________________ ∑ / repetitions ______________________________________________________ 5 Closed form: _______________________________ Θ(_______________) Q4. (21 points) a) (16 points) Use the code below and run one function at a time with the required values for N and record the actual time (e.g. seconds) it takes. You can simply look at your watch to see the how long it takes. Use a desktop or laptop. DO NOT use an online complier. Answer the questions below. 1. runtime_print has the same code as runtime_increment except that instruction ‘res = res+1’ was replaced with ‘print…’ instruction. Why does runtime_print take more time to run? Answer: _______________________________________________________________________________ 2. runtime_print_long has the same code as runtime_print except that it prints the string given as argument instead of ‘A’. Why does runtime_print_long take more time to run? Answer: _______________________________________________________________________________ 3. After you record the time for runtime_increment, pay attention to how the performance gets worse as N gets larger. Do you think a program with such time complexity would be feasible for an application where N is a million? Answer: ____________ 4. After you record the time for runtime_pow, notice how much faster the performance deteriorates (i.e. it takes too long to run even for small values of N such as 20). Compare that with runtime_increment and runtime_print (compare both the actual time, and the time complexity). In the table below the code fill in the time complexity (as Θ) and the approximate “clock time” each function takes to run. You do NOT need to show your derivations for computing Θ. You also do not need to report the exact time. You can say: “≤ 1 sec”, “a few seconds”, “a few minutes”, “more than 15 minutes” #include void runtime_increment(int N); void runtime_print(int N); void runtime_print_long(int N, char* long_str); int main(void){ // call the functions here and record the time they take. int L = 1000, i; char str[L+1]; for(i=0; i #include #include void runtime_rec(int N, char * str){ if (N==0) { //printf("%s\n", str); return; } str[N-1] = 'L'; runtime_rec(N-1, str); str[N-1] = 'R'; runtime_rec(N-1, str); } int main(int argc, char** argv) { int N = 0; char ch; char str[100]; printf("run for: N = "); scanf("%d", &N); str[N] = '\0'; //to use it as a string of length N. printf("runtime_rec(%d)\n", N); runtime_rec(N, str); } You do NOT need to run any of this code on omega or the VM. You can run it on your machine. Write all your answers in a pdf called 3318_H1.pdf. Make sure you use this exact name. Put your NAME and section at the top of the first page. You can handwrite the answer on paper and scan the paper as pdf, but make sure your handwriting is neat and can be read. If the scan is not legible, or did not capture all of your answers on a page, or it is missing a page, you will NOT have a chance to resubmit after the deadline. DOWNLOAD it back from CANVAS and CHECK that the SUBMITTED PDF is good. You can write your answers on white paper, but they must match the required format (e.g. the tables and the components for the time complexity of loops calculation). You do not need to ‘fit’ your answer in the given spacing. You can use as much space as your need.  Answer what the question is asking for. For example if a question is asking for the number of loop repetitions as a function of input N, you will not get any points for answering the detailed instruction count. 8  Points will be deducted for not submitting in the required format. 9 ...
Purchase answer to see full attachment

Do you have a similar assignment and would want someone to complete it for you? Click on the ORDER NOW option to get instant services at essayloop.com

Our Advantages

  • Quality Work
  • Unlimited Revisions
  • Affordable Pricing
  • 24/7 Support
  • Fast Delivery

Order Now

Get 20% Discount

Get 20% off on your first order