Download Programming Exercises for Tutorial Week 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Tutorial Week 2 Notes:
Mathematical Preparations:
Logarithms
In computer science, all logarithms are to the base 2 unless specified otherwise.
2A=B if and only if A=log B
logAB=logA+logB
logA/B=logA-logB
log(AB)=BlogA
(Optional) Modular Arithmetic:
We say that A is congruent to B modulo N, and write A≡B (mod N), if N divides A-B.
e.g. 10≡2 (mod 4)
if A≡B (mod N), then AC≡BC (mod N), A+D≡B+D (mod N)
(Optional) Proof Methods:
To prove correctness: Proof by Induction
Base case (initialization): Property holds when n=1
Induction Step (maintenance): if Property holds for n=k, then Property holds for n=k+1 (k>1)
Example: In Insertion Sort, when we are looking at the kth number, the first k-1 numbers are sorted
Base case : when we are looking at the 2nd number, the first number is sorted
Induction Step: if the first k-1 numbers are sorted, when we are dealing with the kth number,
we will move it to the correct place so that the first k numbers will be sorted. Therefore, when
we are looking at the (k+1)th number, the first k numbers will be sorted.
Termination: when we are looking at the (n+1)th number which does not exist, the first n
numbers are sorted.
To prove incorrectness: Proof by Counter Example
Example: Prove that n<logn is wrong
Let n=4, the above inequality becomes 4<2. It is a contradiction.
Written Exercises:
1. An algorithm takes 0.5s for input size 10. How long will it take for input size 100 if the running
time is the following function:
a. T(n)=n (linear)
b. T(n)=n2 (quadratic)
c. T(n)=n3 (cubic)
d. T(n)=nlogn
e. T(n)=2n
Programming Review:
Create the class library “Myclass” containing the class you designed in tutorial week 1 or a new class
you can now think of. Develop a console application that uses your class in assembly Myclass.dll.
Steps:
1. Select File>New>Projects… In the New Project dialog, ensure that Visual C++ Projects is selected
in Project Types section and click Class Library(.NET). Name the project Myclass and choose the
directory to store the project.
2. Write your class code in Myclass.h and Myclass.cpp
3. Define a console application TestLibrary to use the library: Add a reference
3.1 Right click on project TestLibrary in Solution Explorer and select Add Reference
3.2 Add the code using namespace Myclass to use the reference
(The programming work this semester will follow this style)