Lab 02 - Complexity Analysis
Lab 02 - Complexity Analysis
Fall 2024
LAB-02 Issue Date: February 14, 2025
ALERT!
1. Consider the following code snippets. Derive the growth function for each of the following codes.
int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n * n; j++)
1. {
sum++;
}
}
int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
3.
{
sum++;
}
}
void display(char* s)
{
for (int i = strlen(s) - 1; i >= 0; i++)
4.
{
cout << s[i] << endl;
}
}
Faculty of Computing and Information Technology Page 1 of 3
Dr. Madiha Khalid
CC-213L – Data Structures and Algorithms
Fall 2024
LAB-02 Issue Date: February 14, 2025
2. Now that you have derived the growth functions for all of the code snippets given in Part 1, do the
following tasks:
i. From each growth function derived in Part 1, pick the most dominant term (the term that
grows the fastest as n increases e.g., n², n³, n log n, etc). This term represents how the
algorithm scales as n increases.
ii. In Microsoft Excel, create a dataset for different values of n (e.g., n = 10, 50, 100, 200, 500,
1000, 2000, 5000, 10000, 20000, 30000, 50000 etc).
iii. For each dominant term, compute its value for each n. Example: If the dominant term is n²,
compute 10², 50², 100², 5002... and so on.
iv. Use Excel’s graph plotting tool (line graph feature) to plot the dominant terms. Plot all terms
(e.g., n, n log n, n², n³) on the same graph to compare their growth.
v. Label the axes as:
X-axis: values of n
Y-axis: dominant terms
vi. Observe the plot curves and see how different dominant terms scale as n increases. Compare
how slower-growing functions behave compared to faster-growing ones. On the basis of your
comparison write the dominant terms in the increasing order of their growth rate.
Bermeja Island in Mexico has surrounded by the pirates. A truck enters the Island. The driver claims the load
is food and medicine. Captain Swann is one of the soldiers in Bermeja Island. He doubts about the truck,
maybe it's from the pirates. Every truck has a tag displayed on it. Captain Swann is instructed to allow the
vehicles to enter the Island only if they have a valid tag on them. He knows that a tag is valid if the sum of
every two consecutive digits of it is even and its letter is not a vowel. He needs to determine whether the tag
of the truck is valid or not. We consider the letters "A", "E", "I", "O", "U", "Y" to be vowels for this problem.
The format of the tag is "DDXDDD-DD", where D stands for a digit (non-zero) and X is an uppercase English
letter. Your task is to write a program to help Captain Swann by determining whether the tag is valid or not.
Sample Execution 1:
Input: 12C345-67
Output: Invalid
Explanation: The tag is invalid because the sum of first and second digit of it is odd (also the sum
of 4th and 5th, 5th and 6th and 8th and 9th are odd). Also, the letter in not a vowel.
Sample Execution 2:
Input: 31Y573-42
Output: Valid
Explanation: The tag is valid because the letter “Y” is a vowel and the sum every two consective degts
are even i.e. sum of first and second digit is even, also the sum of 4th and 5th, 5th and 6th and 8th and 9th
are even).
Write a driver program as well to check the correctness of your function. You have to derive the growth
function of your program to analyze the time complexity.
Dr. Bruce Banner hates his enemies. As we all know, he can barely talk when he turns into the incredible
Hulk. That's why he asked you to help him to express his feelings. Hulk likes the Inception so much, and like
that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is
hate and so on... For example, if 𝑛 = 1, then his feeling is "I hate it" or if 𝑛 = 2, it's "I hate that I love it",
and if 𝑛 = 3 it's "I hate that I love that I hate it" and so on. Please help Dr. Banner by writing a program to
express his feelings.
Sample Execution 1:
Input: 3
Output: I hate that I love that I hate it.
Sample Execution 2:
Input: 4
Output: I hate that I love that I hate that I love it.