CSC 205 Worksheet on Run-Time
Total Page:16
File Type:pdf, Size:1020Kb
CSC 245 Lab #1 Introductory Algorithm Analysis and C++
Complete exercises 2-5 on a separate sheet of paper. Show all work. Staple a printout of your code for exercise #1 to the back of your paper. Submit on Wednesday, August 30th. This lab is worth 30 lab points.
1. Create a directory named csc245 in your account, and a subdirectory Lab1 within it. Create a program named temperature.cpp as described below. Print out a copy of your program when you’re done.
Write a C++ program that has main() call a user-defined function that takes a Celsius temperature value as an argument and then returns the equivalent Fahrenheit value. The program should request the Celsius value as input from the user and display the result, as shown in the following code:
Please enter a Celsius value: 20 20 degrees Celsius is 68 degrees Fahrenheit.
For reference, here is the formula for making the conversion:
Fahrenheit = 1.8 * Celsius + 32.0
Here is a general framework for your program.
#include
double convert(double value);
int main () {
}
double convert(double tmp) {
} 2. What is the execution time, T(n), and its magnitude, O(n), for the following C++ functions, when n is the size of the input?
a) int main() { int a, b, c, k;
cin >> a >> b; cout << a << b;
while (cin) { cin >> a >> b; cin >> c >> k; } return 0; }
b) int sum (int n) { int a=2, b=3, c=1, k=0;
cout << a << b;
if (a > b) c = a - b; else { c = b - a; cout << c; }
for (int i = 1; i <= n; i++) cout << c << k;
return 0; }
3. Order the following functions by growth rate in order from smallest to largest :
N N log N N 2/N N1.5 2N N2 2N/2 N log (N2) 37 N2logN N3 4. For each of the following, describe the algorithmic execution time using the Big-O Notation. Assume sum is an integer variable that has been initialized to 0.
a. for (int i = 1; i <= 50; i++) sum++;
b. for (int i = 1; i <= n; i++) sum++;
c. for (int i = 1; i <= n; i++) sum++; for (int j = n*n; j >=1; j--) sum++;
d. for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) sum++;
e. for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) sum++;
f. for (int i = 2; i <= n; i *= 2) sum++;
g. for (int i = 1; i < n; i++) for (int j = 0; j < i * i; j++) for (int k = 0; k < j; k++) sum++;
5. 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? a. linear b. quadratic c. cubic
6. An algorithm takes 0.5 ms for input size 100. How large a problem can be solved in 1 minute if the running time is the following? a. linear b. quadratic c. cubic