0% found this document useful (0 votes)
9 views5 pages

Assignment 3 - Loop Complexity - C15

Uploaded by

saadamjad77777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Assignment 3 - Loop Complexity - C15

Uploaded by

saadamjad77777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer Science

Faculty of Faculty of Information Technology and Computer Science (FOIT)


University of Central Punjab

Spring 2024 Assignment 3 CSAL-1213: Discrete Structures

Name: ID:

Date: Section:C15 Total Marks: 25

CLO # Course Learning Outcome (CLO) Taxonomy Mapping to


Level PLO

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

Problem 1 ____ Marks: 25

• For each loop provided, identify the time complexity. Draw a table for each loop. Include the
following columns in your table:

• Iteration #: The iteration count or step number.


• Control Variable Value: The value of the control variable (e.g., i, j) at each iteration.
• Condition Check: The result of the condition check (e.g., i<n, j>0) for each iteration.

• Calculate the total number of iterations and determine the time complexity using O-notation.

1) int Sum=0;---------------------- 2) int Sum=0;-------------------------


for(int i=0; i<N; i++)------------- for(int i=0; i<10; i++) --------------
for(int j=0; j<20; j++) -------------
for(int j=0; j<N; j++) ---------- for(int k=0; k<30; k++) ---------
Sum++;------------------------- Sum++;---------------------------
T(n) = T(n) =

3) int Sum=0;-------------------------- 4) int Sum=0;


for(int i=0; i<N; i++) --------------- for(int i=0; i<N; i++)
for(int j=0; j<i; j++)--------------- for(int j=0; j<N; j++)
for(int k=0; k<j; k++) ------------ for(int k=0; k<N; k++)
Sum++; Sum++;
T(n) = for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
for(int k=0; k<N; k++)

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) =

7) for(int i=1; i<=N*N; i*=2) 8) int Sum=0;


for(int j=1; j<N*N; j*=2) for(int i=1; i<=N; i*=2)
Sum++; for(int j=1; j<=N; j*=2)
for(int k=1; k<=N; k*=2)
T(n) = Sum++;
T(n) =
Department of Computer Science
Faculty of Faculty of Information Technology and Computer Science (FOIT)
University of Central Punjab

9) 10)BE CAREFUL GEOMETRIC SERIES


int sum, i, j; int sum,i,j;
sum = 0;
sum = 0; for (i=1; i<n; i=i*5)
for (i=1; i<n; i=i*2) {
{ for (j=0; j<i; j+=2)
for (j=0; j<n;++j) {
{ 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) =

13)What is the time complexity of the algorithm: 14)


void f1(int n) What is the time complexity of the algorithm:
{
for(int j=0; j*j<=n*n; j++) void f1(int n)
K++; {
return K;
for(int j=1; j*j<=n; j++)
}
int main() K++;
{
int Sum = 0;
return K*K;
int n;
cin>>n; }
for(int i=1; i<=f1(n); i+=1) int main()
{ {
for(int j=1; j<=i; j++) int Sum = 0;
{ int n;
Sum++; cin>>n;
}
} int Terminator = f1(n);
cout<<Sum<<endl; for(int i=1; i<= Terminator; i+=1)
} {
for(int j=1; j<=i; j++)
T(n) =
{
Department of Computer Science
Faculty of Faculty of Information Technology and Computer Science (FOIT)
University of Central Punjab

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) =

21 22 Complexity of primeNumber function.


for (int i=1; i <= n ; i = i * 2) int sqrt(int N)
{ {
for ( j = 1 ; j <= i ; j = j * 2) int d;
{ for(d=0; d*d<=N; d++) { }
return d-1;
cout<<”*”;
}
}
bool primeNumber(int n)
} {
for(int i=0; i<=N; i++) bool isPrime = true;
{
Sum++; int lmt = (sqrt(n));
}
for (int d=2; d <=lmt ;++d)
{
T(n) =
if (n%d==0)
return false;
}
return true; }

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) =

You might also like