1 Written Part (30pts) : CS 340 Assignment 1 Total: 100pts + 10pts (Bonus)
1 Written Part (30pts) : CS 340 Assignment 1 Total: 100pts + 10pts (Bonus)
CS 340
Assignment 1
Total : 100pts + 10pts (Bonus)
Give an analysis of the running time (Big-Oh notation) for each of the following 6 program frag-
ments. Note that the running time corresponds here to the number of times the operation sum++
is executed.
(a) sum = 0;
for(i=0;i<n;i++)
sum++;
(b) sum = 0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum++;
(c) sum = 0;
for(i=0;i<n;i++)
for(j=0;j<n*n;j++)
sum++;
(d) sum = 0;
for(i=0;i<n;i++)
for(j=0;j<i;j++)
sum++;
(e) sum = 0;
for(i=0;i<n;i++)
for(j=0;j<i*i;j++)
for(k=0;k<j;k++)
sum++;
(f) sum = 0;
for(i=1;i<n;i++)
for(j=1;j<i*i;j++)
if (j % i == 0)
for(k=0;k<j;k++)
sum++;
Assignment #1 Dr. Malek Mouhoub 2/4
(a) linear
(b) O(N log N )
(c) quadratic
(d) cubic
F1 = 2, Fn = (Fn−1 )2 n ≥ 2
(a) What is the complexity of the algorithm that computes Fn using the recursive definition given
above.
(b) Describe a more efficient algorithm to calculate Fn and give its running time.
13 + 24 * 35 / 46 --> 13 24 35 * 46 / +
( 4 + 8 ) * ( 6 - 5 ) / ( 3 - 2 ) * ( 2 + 2 ) --> 4 8 + 6 5 - * 3 2 - /
2 2 + *
( ( ( ( 1 * ( 2 + 3 ) ) - 3 ) + 4 ) * 5 ) --> 1 2 3 + * 3 - 4 + 5 *
2.5 Marking scheme of the programming part: total = 70pts + 10pts (Bonus)
1. Readability (program style) : 10pts
• Program easy to read,
• well commented,
• good structured (layout, indentation, whitespace, . . .) and designed(following the top-down
approach).
2. Compiling and execution process : 10pts
Assignment #1 Dr. Malek Mouhoub 4/4
3 Hand in
3.1 Written Part
• Submit electronic version : pdf(preferred) or word via UR Courses. Your file should be named
assign1username.pdf (or assign1username.doc). “username” is your uregina.ca username.