Assignment 3 - Loop Complexity - C15
Assignment 3 - Loop Complexity - C15
Name: ID:
CLO 1 The students will be able to apply fundamental concepts of sets, functions, C3 PLO 2
relations, loops, time complexities, and combinatorics.
CLO 2 The students will be able to apply quantifiers, proofs, logical reasoning, and C3 PLO 2
models of computation to real problems in computer science and data
networking.
CLO 3 The students will be able to illustrate the application of mathematical ideas C3 PLO 2
like graphs and trees to computer science problems.
Instructions:
1. Attempt all questions.
2. Write your answer showing all steps required to perform the task.
3. Assignment must be solved on A4 sheets or Assignment sheets only.
a. Violation will result to deduction of 1 mark from the scored marks.
b. Submissions will be made either via portal or in the class.
4. Due Date for Assignment is: 28th Nov 2024
5. Late submission will result in 10% deduction in marks.
6. No request for late submissions will be considered after two working days of the deadline.
7. No request to review assignment will be considered after 3 working days of the review in
class.
Department of Computer Science
Faculty of Faculty of Information Technology and Computer Science (FOIT)
University of Central Punjab
• For each loop provided, identify the time complexity. Draw a table for each loop. Include the
following columns in your table:
• Calculate the total number of iterations and determine the time complexity using O-notation.
Sum++;
T(n) =
5) int Sum=0; 6)
for(int i=1; i<N; i*=2)
Sum++; for(int i=1; i<=N*N; i+=2)
for(int j=1; j<N; j*=2) for(int j=1; j<N*N; j*=2)
Sum++; Sum++;
T(n) = T(n) =
11)What will be the asymptotic bound on the value of 12)What is the time complexity of the algorithm:
Sum: int Sum = 0;
for(int i=1; i<=n; i+=1)
int Sum = 0; {
for(int i=1; i<=n; i+=1) for(int j=1; j<=i; j++)
{ {
Sum+=i; Sum++;
} }
cout<<Sum<<endl; }
cout<<Sum<<endl;
T(n) = T(n) =
Sum++;
}
}
cout<<Sum<<endl;
}
T(n) =
15) 16)
for (i=1;i<n;i=i*4) for (i=1; i<=n*n; ++i)
{ {
cout << i; cout << i;
Sum=0;
for (j=0;j<n;j=j+2) for (j=1; j<=i; ++j)
{ {
cout << j; Sum++;
sum++ cout << i;
} }
cout << sum; cout << Sum;
} }
T(n) = T(n) =
17) 18)
for (i=1; i<=n*n*n; i*=2) for (i=1; i<=n*n*n; i*=2)
{ {
cout << i; cout << i;
Sum=0;
Sum=0; for (j=1; j<=i; j++)
for (j=1;j<=i; j++) {
Sum++;
{ cout << i;
}
Sum++; for (j=1; j<=n; j*=2)
{
cout << i;
Sum++;
} cout << i;
}
cout << Sum; cout << Sum;
} }
T(n) = T(n) =
Department of Computer Science
Faculty of Faculty of Information Technology and Computer Science (FOIT)
University of Central Punjab
19) for (int i=1; i <= n ; i = i * 2) 20) for (int i=1; i <= n ; i = i * 4)
{ for ( j = 1 ; j <= i ; j = j * 2)
for ( j = 1 ; j <= i ; j = j * 2) cout<<”*”;
cout<<”*”;
} T(n) =
T(n) =
T(n) =
23)
for (i=0; i<n; i=i+3)
{
cout << i;
for (j=1; j<n; j=j*3)
{
cout << j;
sum++
}
for (k=1; k<n; k=k*3)
{
cout << j;
sum++
}
cout << sum;
}
T(n) =