Learn to Hand Trace C++ Program Execution | CECS 174, Assignments of Computer Science

Material Type: Assignment; Class: Programming & Problem Solving I; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Fall 2003;

Typology: Assignments

Pre 2010

Uploaded on 08/17/2009

koofers-user-iyl-1
koofers-user-iyl-1 🇺🇸

3

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exercise 10/23/03
Objective: Learn to hand trace C++ program execution.
For each of the function calls in the following program, construct a table that shows the changes of every
variable during the execution of the function, and determine the function’s return value. Finally, show the final
output of the program. Do all these without using the computer.
#include <iostream>
using namespace std;
int mystery1(int n, int k);
int mystery2(int m, int n);
int mystery3(int n);
int mystery4(int n, int d);
double mystery5(double x, int n);
int main()
{
cout << "mystery1(10, 4) = " << mystery1(10, 4) << endl;
cout << "mystery2(315, 756) = " << mystery2(315, 756) << endl;
cout << "mystery3(123456) = " << mystery3(123456) << endl;
cout << "mystery4(123, 12) = " << mystery4(123, 12) << endl;
cout << "mystery5(3.0, 5) = " << mystery5(3.0, 5) << endl;
return 0;
}
int mystery1(int n, int k)
{ assert(0 <= k && k <= n && n <= 33);
int y = 1;
for (int i=1; i<=k; i++, n--)
y = y*n/i;
return y;
}
int mystery2(int m, int n)
{ assert(m > 0 && n > 0);
do {
while (m <= n)
n = n - m;
int temp = m;
m = n;
n = temp;
} while (m > 0);
return n;
}
pf2

Partial preview of the text

Download Learn to Hand Trace C++ Program Execution | CECS 174 and more Assignments Computer Science in PDF only on Docsity!

Exercise 10/23/

Objective: Learn to hand trace C++ program execution. For each of the function calls in the following program, construct a table that shows the changes of every variable during the execution of the function, and determine the function’s return value. Finally, show the final output of the program. Do all these without using the computer. #include using namespace std; int mystery1(int n, int k); int mystery2(int m, int n); int mystery3(int n); int mystery4(int n, int d); double mystery5(double x, int n); int main() { cout << "mystery1(10, 4) = " << mystery1(10, 4) << endl; cout << "mystery2(315, 756) = " << mystery2(315, 756) << endl; cout << "mystery3(123456) = " << mystery3(123456) << endl; cout << "mystery4(123, 12) = " << mystery4(123, 12) << endl; cout << "mystery5(3.0, 5) = " << mystery5(3.0, 5) << endl; return 0; } int mystery1(int n, int k) { assert(0 <= k && k <= n && n <= 33); int y = 1; for (int i=1; i<=k; i++, n--) y = y*n/i; return y; } int mystery2(int m, int n) { assert(m > 0 && n > 0); do { while (m <= n) n = n - m; int temp = m; m = n; n = temp; } while (m > 0); return n; }

int mystery3(int n) { assert(n >= 100000 && n <= 999999); int temp = n; n = temp%10; temp /= 10; n = 10n + temp%10; temp /= 10; n = 10n + temp%10; temp /= 10; n = 10n + temp%10; temp /= 10; n = 10n + temp%10; temp /= 10; n = 10n + temp%10; return n; } int mystery4(int n, int d) { assert(n >= d && d > 0); while (n > d) n = n - d; return n; } double mystery5(double x, int n) { assert(x > 0.0); double y = 1.0; if (n < 0) { x = 1.0/x; n = -n; } while (n > 0) { if (n%2 == 0) { x = xx; n = n/2; } else { y = y*x; --n; } } return y;