Lab2 - task and get same value in t1 3 Using the functions...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 284 Algorithms Summer semester 2010 Lab 2 Lab objectives: - Understand how the recurrence relations work. - Understand the recurrence relations having order ( n log(n) ) solutions. Related chapter(s): 4. Lab instructions (steps): 1. Write the following program and run it. #include <time.h> #include <iostream> #include <stdio.h> using namespace std; int t0( int n); int t1( int n); int t2( int n); int t3( int n); int t4( int n); void main() { cout << "comparing the recursive and iterative functions representing a recurence relation:" <<endl; cout << "n\t" << "t0(n)\t" << "t1(n)\t" <<endl; cout << "--------------------" << endl; for ( int n=2; n<=256; n*=2){ cout << n << "\t" <<t0(n)<< "\t" <<t1(n)<<endl; } } int t0( int n){ int res=0; // your code here return res; } int t1( int n){ if (n<=0) return 0; return 6 + t1(n-3); } int t2( int n){ if (n<=1) return 1; return 2*n + t2(n-1); } int t3( int n){ // your code here } int t4( int n){ // your code here } 2. Implement the function t0 using iterative loop (for or while) to do the same
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: task and get same value in t1. 3. Using the functions t1 and t2 as guidelines, implement the function t3 corresponding to a divide-and-conquer algorithm where you have 2 subproblems, each of size n/2 (using linear time for dividing and combining solutions). Implement the function t4 similarly, but with 4 subproblems each of size n/4. 4. Give the recurrence relation for each of t1, t2, t3 and t4. 5. What is the solution of each recurrence relation in part 4 (you may use asymptotic notation)? Function t1 t2 t3 t4 Solution 6. Give print out of the results. Tasks: 1) Hand in the function t0. 2) Hand in the function t3. 3) Hand in the function t4. 4) Hand in the output of step 6. 5) Hand in recurrence relation from step 4. 6) Hand in the table of step 5....
View Full Document

This note was uploaded on 04/15/2011 for the course CS 284 taught by Professor Jamess. during the Spring '10 term at Jordan University of Science & Tech.

Page1 / 2

Lab2 - task and get same value in t1 3 Using the functions...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online