Re: Print numbers
On Jul 8, 5:49 am, red floyd <no.spam.h...@example.com> wrote:
James Kanze wrote:
On Jul 7, 12:46 pm, arnuld <sunr...@invalid.address> wrote:
On Mon, 07 Jul 2008 02:40:44 -0700, James Kanze wrote:
[...]
I suspect it's homework, but you probably knew that, James.
Exactly. Thus, a few "exotic" suggestions. I wonder what his
prof would say if he turned in the one with the floating point.
Of course, as he originally stated the problem, my original
solution (std::cout << "1,1,2,3,..") is both the simplest and
the most efficient---and so, the correct solution. I rather
doubt, however, that that was what the prof was looking for.
I wish I could remember one of the IOCCC fibonacci programs.
But I can't. So here's one that will hopefully confuse the
OP.
#include <iostream>
#include <ostream>
#include <algorithm>
#include <iterator>
template <int N> struct fib {
enum { value = fib<N-1>::value + fib<N-2>::value };
};
template<> struct fib<0> {
enum {value = 1 };
};
template<> struct fib<1> {
enum {value = 1};
};
int main()
{
int const fv[] = { fib<1>::value, fib<2>::value, fib<3>::value,
fib<4>::value, fib<5>::value, fib<6>::value,
fib<7>::value };
std::copy(fv, fv+7, std::output_iterator<int>(std::cout,","));
}
Yes. And if you want the number of values to be variable, you
ouput the code to a temporary file, looping over the
initialization of fv, and then use system to compile and run
that.
I like it!
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34