- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Function Prototype
#include <iostream>
using std::endl;
using std::cout;
void print_stars(int n); // Function Prototype
int main()
{
for (int j=1; j<=5; j++)
print_stars(j);
return 0;
}
int print_stars(int n)
{
for (int i=0; i<n; i++)
cout << '*';
cout << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Recursive Function Call
#include <iostream>
using std::cout;
using std::endl;
void oct(int n)
{
if (n >= 8)
oct( n / 8); // recursive call
cout << n % 8;
}
int main()
{
oct(23); cout << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Static Variable
#include <iostream>
using std::cout;
using std::endl;
void oct(int n)
{
static int nCount = 0;
++nCount;
if (n >= 8)
oct( n / 8);
else
cout << "Depth: " << nCount << endl;
cout << n % 8;
}
int main()
{
oct(23); cout << endl;
return 0;
}