fig06_28.cpp
#include<iostream>
#include<iomanip>
using namespace std;
unsigned long factorial(unsigned long);
int main()
{
for (unsigned int counter = 1; counter <= 10; ++counter)
cout << setw(2) << counter << "!= " << factorial(counter)
<< endl;
system("pause");
return 0;
}
unsigned long factorial(unsigned long number)
{
if (number <= 1)
return 1;
else
return number*factorial(number - 1);
}
fig06_29.cpp
#include<iostream>
using namespace std;
unsigned long fibonacci(unsigned long);
int main()
{
for (unsigned int counter = 0; counter <= 10; ++counter)
cout << "fibonacci(" << counter << ")="
<< fibonacci(counter) << endl;
cout << "\nfibonacci(20) = " << fibonacci(20) << endl;
cout << "fibonacci(30) = " << fibonacci(30) << endl;
cout << "fibonacci(35) = " << fibonacci(35) << endl;
system("pause");
return 0;
}
unsigned long fibonacci(unsigned long number)
{
if ((0 == number) || (1 == number))
return number;
else
return fibonacci(number - 1) + fibonacci(number - 2);
}
fig06_21.cpp
#include<iostream>
#include<iomanip>
#include<array>
using namespace std;
int main()
{
array<int, 5> n;
for (size_t i = 0; i < n.size(); ++i)
n[i] = 0;
cout << "Element" << setw(13) << "Value" << endl;
for (size_t j = 0; j < n.size(); ++j)
cout << setw(7) << j << setw(13) << n[j] << endl;
system("pause");
return 0;
}
fig7.4.cpp
#include<iostream>
#include<iomanip>
#include<array>
using namespace std;
int main()
{
array<int, 5> n = { 32, 27, 64, 18, 95 };
cout << "Element" << setw(13) << "Value" << endl;
for (size_t i = 0; i < n.size(); ++i)
cout << setw(7) << i << setw(13) << n[i] << endl;
system("pause");
return 0;
}