0% found this document useful (0 votes)
85 views

1 Written Part (30pts) : CS 340 Assignment 1 Total: 100pts + 10pts (Bonus)

This document describes Assignment 1 for the CS 340 course. The assignment has two parts: a written part worth 30 points and a programming part worth 70 points plus 10 bonus points. The written part involves solving algorithm analysis problems involving Big-O notation and solving recurrence relations. The programming part involves writing a C++ program to: 1) Convert infix arithmetic expressions to postfix notation, 2) Construct an expression tree from a postfix expression, 3) Print expressions in various notations from the expression tree, and 4) Evaluate an expression represented by the tree. The program will be graded based on readability, correctness, and bonus features.

Uploaded by

jenl chaklasiya
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)
85 views

1 Written Part (30pts) : CS 340 Assignment 1 Total: 100pts + 10pts (Bonus)

This document describes Assignment 1 for the CS 340 course. The assignment has two parts: a written part worth 30 points and a programming part worth 70 points plus 10 bonus points. The written part involves solving algorithm analysis problems involving Big-O notation and solving recurrence relations. The programming part involves writing a C++ program to: 1) Convert infix arithmetic expressions to postfix notation, 2) Construct an expression tree from a postfix expression, 3) Print expressions in various notations from the expression tree, and 4) Evaluate an expression represented by the tree. The program will be graded based on readability, correctness, and bonus features.

Uploaded by

jenl chaklasiya
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/ 4

Assignment #1 Dr.

Malek Mouhoub 1/4

CS 340
Assignment 1
Total : 100pts + 10pts (Bonus)

1 Written Part [30pts]


1. Exercice 2.1 page 64 (4pts)
Order
√ the following functions by growth rate:
N, N , N 1.5 , N 2 , N log N, N log log N, N log2 N, N log N 2 , 2/N, 2N , 2N/2 , 37, N 2 log N, N 3 .
Indicate which functions grow at the same rate.
2. Exercise 2.6 page 64 (4pts)
In a recent court case, a judge cited a city for contempt and ordered a fine of $2 for the first day.
Each subsequent day, until the city followed the judge’s order, the fine was squared (that is, the
fine progressed as follows: $2, $4, $16, $256, $64,536, . . .).

(a) What would be the fine on day N ?


(b) How many days would it take for the fine to reach D dollars (a Big-Oh answer will do) ?
3. Exercise 2.7 (question a) page 64 (6pts)

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

4. Exercise 2.11 page 66 (4pts)


An algorithm takes 0.5 ms for input size 100. How long will it take for input size 500 if the running
time is the following (assume low-order terms are negligible)?

(a) linear
(b) O(N log N )
(c) quadratic
(d) cubic

5. Exercise 2.12 page 66 (4pts)


An algorithm takes 0.5 ms for input size 100. How large a problem can be solved in 1 min if the
running time is the following (assume low-order terms are negligible)?
(a) linear
(b) O(N log N )
(c) quadratic
(d) cubic
6. Consider the following function : (4pts)

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.

7. Exercise 4.45 page 180 (4pts)


Two binary trees are similar if they are both empty or both nonempty and have similar left and
right subtrees.
(a) Write a function to decide whether two binary trees are similar.
(b) What is the running time of your function ?

2 Programming Part [70pts+10pts(Bonus)]


Given an arithmetic expression in infix or postfix notation, the goal of this assignment is to :
1. Write a program that converts the expression to another notation using expression trees.

2. Evaluate the expression.


The main idea consists of the following steps :
1. Convert the given (input) expression into a postfix notation (if it is not the case).
2. Construct the expression tree from the postfix representation.

3. Evaluate the expression tree or print it into another notation.


The expression contains only the arithmetic operators +, *, - and /, integers and (, ) if the expression
is in infix form. * and / have more priority than + and -. You should leave at least 1 space between
operators, operands and symbols (the minus sign(“-”) for negative numbers should however be attached
to the number, -23 for instance).
Assignment #1 Dr. Malek Mouhoub 3/4

2.1 Infix to Postfix Conversion


Implement the algorithm that converts an expression from infix to postfix (a description of this algorithm
is provided in subsection 3.3.3 of the textbook). Below are some examples for conversion from infix to
postfix.

-12 + 13 --> -12 13 +

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.2 Constructing an expression tree from a postfix notation


Implement the algorithm that converts a postfix expression into an expression tree (a description of this
algorithm is provided in subsection 4.2.2 of the textbook) . You may reuse the array implementation of the
ADT Stack provided by the author (TestStackAr.cpp, StackAr.cpp and StackAr.h are available
in the textbook homepage).

2.3 Printing an arithmetic expression from the expression tree


Using the following tree traversal algorithms, write a program that prints an arithmetic expression in a
given notation (prefix, infix or postfix) from an expression tree.

2.3.1 Inorder traversal


An overly parenthesized infix expression can be produced by recursively producing parenthesized left
expression, then printing out the operator at the root, and finally recursively producing parenthesized
right expression. This general strategy (left,node,right) is known as an inorder traversal.

2.3.2 Postorder traversal


The algorithm consists in recursively print out the left subtree, the right subtree, and then the operator.

2.3.3 Preorder traversal


This method consists in printing out the operator first and then recursively print out the left and the
right subtrees.

2.4 Evaluating an arithmetic expression from the expression tree


Write a program that evaluates an arithmetic expression represented by an expression tree.

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

• program compiles w/o errors and warnings


• robustness : execution w/o run time errors
3. Correctness : 50pts
• code produces correct results (output) :
(a) infix → postfix notation 8pts
(b) infix → prefix notation 8pts
(c) postfix → infix notation 8pts
(d) postfix → prefix notation 8pts
(e) evaluate the expression tree 6pts
(f) Handling numbers with more than 1 digit 6pts
(g) Detecting the type of expression in input(prefix, infix or postfix) 6pts
4. Bonus : 10pts
• Handling expressions in prefix form in input 5pts
• Detecting the type of errors (missing operator or operand, unknown symbol . . . etc) 3pts
• Other features that increase functionality and/or presentation 2pts

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.

3.2 Programming Part


3.2.1 Single file submission
Using URCourses, submit the file containing the C++ code of the programming part assign1username.cpp.
At the top of this file add the following comments :
1. Your first name, last name and ID #,
2. the compiling command you have used,
3. an example on how to execute the program,
4. and other comments describing the program

3.2.2 Multiple files submission


Using UR Courses, submit all files required by the programming part :
1. README (file including your name and ID #; and explaining the compilation and execution of
your program including the format of input and other details)
2. headers (.h)
3. implementations (.cpp)
4. the Makefile :
• should be named ”makefile”. In the makefile, the generated executable should be named :
”assign1username”
You can give any name to your source files. The marker will only have to run ”make” to compile
your program and ”assign1username” to execute it.
All your source files should be submitted in one single zip file named: assign1username.zip

You might also like