Print numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew.smith.cpp@gmail.com

    Print numbers

    Hello
    how Can i get this kind of output?

    1,2,3,5,8,13,21
    i try it with loop
    but could print it.


    Thanks
  • arnuld

    #2
    Re: Print numbers

    On Sun, 06 Jul 2008 23:26:52 -0700, andrew.smith.cp p wrote:
    Hello
    welcome
    how Can i get this kind of output?
    >
    1,2,3,5,8,13,21
    yes, I solved it. you can find it here:
    This is the Welcome Message of comp.lang.c++ Newsgroup. I am posting it here as a community service, since comp.lang.c++ community have helped me a lot :-)   Welcome to comp.lang.c++! Read thi…



    i try it with loop
    but could print it.
    I thin you meant "but could not print it". BTW, AAMOF, IMVHO, you can use
    std::cout to print something to standard output.



    --

    my email is @ the above blog
    check the "About Myself" page

    Comment

    • Linlin Yan

      #3
      Re: Print numbers

      On Jul 7, 2:26 pm, andrew.smith... .@gmail.com wrote:
      Hello
      how Can i get this kind of output?
      >
      1,2,3,5,8,13,21
      i try it with loop
      but could print it.
      >
      Thanks
      It's a Fibonacci series, so it is easy to print or get with loop.
      But why did you post the question in this C++ newsgroup?

      Comment

      • James Kanze

        #4
        Re: Print numbers

        On Jul 7, 8:26 am, andrew.smith... .@gmail.com wrote:
        Hello
        how Can i get this kind of output?
        >
        1,2,3,5,8,13,21
        i try it with loop
        but could print it.
        If printing it is a problem:
        std::cout << "1,2,3,5,8,13,2 1\n" ;
        should do the trick. But that looks like the start of a
        Fibonacci sequence. If so, and you want to output an arbitrary
        number of elements, you'll need something like:

        std::cout.setf( std::ios::fixed , std::ios::float field ) ;
        std::cout.preci sion( 0 ) ;
        double sqrt5( sqrt( 5.0 ) ) ;
        double psi( (1.0 + sqrt5) / 2.0 ) ;
        for ( int i = 1 ; i <= count ; ++ i ) {
        if ( i != 1 ) {
        std::cout << ',' ;
        }
        std::cout << (pow( psi, i ) - pow( -psi, -i )) / sqrt5 ;
        }
        std::cout << '\n' ;

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • arnuld

          #5
          Re: Print numbers

          On Mon, 07 Jul 2008 02:40:44 -0700, James Kanze wrote:
          If printing it is a problem:
          std::cout << "1,2,3,5,8,13,2 1\n" ;
          should do the trick.
          :D

          But that looks like the start of a
          Fibonacci sequence. If so, and you want to output an arbitrary
          number of elements, you'll need something like:
          >
          std::cout.setf( std::ios::fixed , std::ios::float field ) ;
          std::cout.preci sion( 0 ) ;
          double sqrt5( sqrt( 5.0 ) ) ;
          double psi( (1.0 + sqrt5) / 2.0 ) ;
          for ( int i = 1 ; i <= count ; ++ i ) {
          if ( i != 1 ) {
          std::cout << ',' ;
          }
          std::cout << (pow( psi, i ) - pow( -psi, -i )) / sqrt5 ;
          }
          std::cout << '\n' ;

          I did not know that OP was looking for a Fibonacci sequence. I just did it
          this way:


          /* a program to print the sum of last 2 numbers.

          OUTPUT should be: 0,1,1,2,3,5,8,1 3,21

          0 + 1 = 1
          1 + 1 = 2
          1 + 2 = 3
          2 + 3 = 5
          3 + 5 = 8
          5 + 8 = 13
          .........
          .........

          */


          #include <iostream>


          int main()
          {
          int x = 0;
          int y = 1;
          int sum = 0;
          const int max_num = 10;


          for( int i = 0; i != max_num; ++i )
          {
          sum = x + y;

          /* if statement purely exists to print zero */
          if( !x )
          {
          std::cout << x << ", "
          << sum << ", ";
          }
          else
          {
          std::cout << sum << ", ";
          }

          x = y;
          y = sum;

          }

          std::cout << std::endl;


          return 0;
          }


          and it prints fine, except that it puts a comma at the end and I have out
          a limit of 10 numbers:

          [arnuld@dune programs]$ g++ -ansi -pedantic -Wall -Wextra test.cpp
          [arnuld@dune programs]$ ./a.out
          0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
          [arnuld@dune programs]$


          --

          my email is @ the above blog
          check the "About Myself" page

          Comment

          • James Kanze

            #6
            Re: Print numbers

            On Jul 7, 12:46 pm, arnuld <sunr...@invali d.addresswrote:
            On Mon, 07 Jul 2008 02:40:44 -0700, James Kanze wrote:
            But that looks like the start of a Fibonacci sequence. If
            so, and you want to output an arbitrary number of elements,
            you'll need something like:
            std::cout.setf( std::ios::fixed , std::ios::float field ) ;
            std::cout.preci sion( 0 ) ;
            double sqrt5( sqrt( 5.0 ) ) ;
            double psi( (1.0 + sqrt5) / 2.0 ) ;
            for ( int i = 1 ; i <= count ; ++ i ) {
            if ( i != 1 ) {
            std::cout << ',' ;
            }
            std::cout << (pow( psi, i ) - pow( -psi, -i )) / sqrt5 ;
            }
            std::cout << '\n' ;
            I did not know that OP was looking for a Fibonacci sequence. I
            just did it this way:
            /* a program to print the sum of last 2 numbers.
            Which is the definition of a Fibonacci sequence.

            The "classical" implementation is:

            int
            fib( int n )
            {
            return n <= 0 ? 1 : fib( n - 1 ) + fib( n - 2 ) ;
            }

            It's sometimes used as a good example of when not to use
            recursion:-); if you just want a few specific values, and add a
            cache, however, it's not that bad:

            int
            fib( int n )
            {
            static std::vector< int cache( 2, 1 ) ;
            if ( n >= static_cast< int >( cache.size() ) ) {
            cache.push_back ( fib( n - 1 ) + fib( n - 2 ) ) ;
            }
            return n < 0 ? 1 : cache[ n ] ;
            }

            Both such solutions suffer from the fact that int's overflow for
            very small values of n, however. My solution above doesn't.
            and it prints fine, except that it puts a comma at the end
            Which is a separate (and general) problem: how to format
            sequences of data.
            and I have out a limit of 10 numbers:
            Try outputting 100 values, and see what happens.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • red floyd

              #7
              Re: Print numbers

              James Kanze wrote:
              On Jul 7, 12:46 pm, arnuld <sunr...@invali d.addresswrote:
              >>On Mon, 07 Jul 2008 02:40:44 -0700, James Kanze wrote:
              >
              >>But that looks like the start of a Fibonacci sequence. If
              >>so, and you want to output an arbitrary number of elements,
              >>you'll need something like:
              >
              >> std::cout.setf( std::ios::fixed , std::ios::float field ) ;
              >> std::cout.preci sion( 0 ) ;
              >> double sqrt5( sqrt( 5.0 ) ) ;
              >> double psi( (1.0 + sqrt5) / 2.0 ) ;
              >> for ( int i = 1 ; i <= count ; ++ i ) {
              >> if ( i != 1 ) {
              >> std::cout << ',' ;
              >> }
              >> std::cout << (pow( psi, i ) - pow( -psi, -i )) / sqrt5 ;
              >> }
              >> std::cout << '\n' ;
              >
              >I did not know that OP was looking for a Fibonacci sequence. I
              >just did it this way:
              >
              >/* a program to print the sum of last 2 numbers.
              >
              Which is the definition of a Fibonacci sequence.
              >
              The "classical" implementation is:
              >
              int
              fib( int n )
              {
              return n <= 0 ? 1 : fib( n - 1 ) + fib( n - 2 ) ;
              }
              >
              It's sometimes used as a good example of when not to use
              recursion:-); if you just want a few specific values, and add a
              cache, however, it's not that bad:
              >
              int
              fib( int n )
              {
              static std::vector< int cache( 2, 1 ) ;
              if ( n >= static_cast< int >( cache.size() ) ) {
              cache.push_back ( fib( n - 1 ) + fib( n - 2 ) ) ;
              }
              return n < 0 ? 1 : cache[ n ] ;
              }
              >
              Both such solutions suffer from the fact that int's overflow for
              very small values of n, however. My solution above doesn't.
              >
              >and it prints fine, except that it puts a comma at the end
              >
              Which is a separate (and general) problem: how to format
              sequences of data.
              >
              >and I have out a limit of 10 numbers:
              >
              Try outputting 100 values, and see what happens.
              >
              I suspect it's homework, but you probably knew that, James.

              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 Nstruct 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_ite rator<int>(std: :cout,","));
              }

              Comment

              • James Kanze

                #8
                Re: Print numbers

                On Jul 8, 5:49 am, red floyd <no.spam.h...@e xample.comwrote :
                James Kanze wrote:
                On Jul 7, 12:46 pm, arnuld <sunr...@invali d.addresswrote:
                >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 Nstruct 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_ite rator<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.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • arnuld

                  #9
                  Re: Print numbers

                  On Mon, 07 Jul 2008 02:40:44 -0700, James Kanze wrote:
                  If printing it is a problem:
                  std::cout << "1,2,3,5,8,13,2 1\n" ;
                  should do the trick.
                  :D

                  But that looks like the start of a
                  Fibonacci sequence. If so, and you want to output an arbitrary
                  number of elements, you'll need something like:
                  >
                  std::cout.setf( std::ios::fixed , std::ios::float field ) ;
                  std::cout.preci sion( 0 ) ;
                  double sqrt5( sqrt( 5.0 ) ) ;
                  double psi( (1.0 + sqrt5) / 2.0 ) ;
                  for ( int i = 1 ; i <= count ; ++ i ) {
                  if ( i != 1 ) {
                  std::cout << ',' ;
                  }
                  std::cout << (pow( psi, i ) - pow( -psi, -i )) / sqrt5 ;
                  }
                  std::cout << '\n' ;

                  I did not know that OP was looking for a Fibonacci sequence. I just did it
                  this way:


                  /* a program to print the sum of last 2 numbers.

                  OUTPUT should be: 0,1,1,2,3,5,8,1 3,21

                  0 + 1 = 1
                  1 + 1 = 2
                  1 + 2 = 3
                  2 + 3 = 5
                  3 + 5 = 8
                  5 + 8 = 13
                  .........
                  .........

                  */


                  #include <iostream>


                  int main()
                  {
                  int x = 0;
                  int y = 1;
                  int sum = 0;
                  const int max_num = 10;


                  for( int i = 0; i != max_num; ++i )
                  {
                  sum = x + y;

                  /* if statement purely exists to print zero */
                  if( !x )
                  {
                  std::cout << x << ", "
                  << sum << ", ";
                  }
                  else
                  {
                  std::cout << sum << ", ";
                  }

                  x = y;
                  y = sum;

                  }

                  std::cout << std::endl;


                  return 0;
                  }


                  and it prints fine, except that it puts a comma at the end and I have out
                  a limit of 10 numbers:

                  [arnuld@dune programs]$ g++ -ansi -pedantic -Wall -Wextra test.cpp
                  [arnuld@dune programs]$ ./a.out
                  0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
                  [arnuld@dune programs]$


                  --

                  my email is @ the above blog
                  check the "About Myself" page

                  Comment

                  Working...