473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 7 '08 #1
8 2566
On Sun, 06 Jul 2008 23:26:52 -0700, andrew.smith.cpp 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:
http://lispmachine.wordpress.com/com...lcome-message/
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.

--
www.lispmachine.wordpress.com
my email is @ the above blog
check the "About Myself" page

Jul 7 '08 #2
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?
Jul 7 '08 #3
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,21\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::floatfield ) ;
std::cout.precision( 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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 7 '08 #4
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,21\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::floatfield ) ;
std::cout.precision( 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,13,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]$
--
www.lispmachine.wordpress.com
my email is @ the above blog
check the "About Myself" page

Jul 7 '08 #5
On Jul 7, 12:46 pm, arnuld <sunr...@invalid.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::floatfield ) ;
std::cout.precision( 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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 7 '08 #6
James Kanze wrote:
On Jul 7, 12:46 pm, arnuld <sunr...@invalid.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::floatfield ) ;
std::cout.precision( 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_iterator<int>(std::cout,","));
}
Jul 8 '08 #7
On Jul 8, 5:49 am, red floyd <no.spam.h...@example.comwrote:
James Kanze wrote:
On Jul 7, 12:46 pm, arnuld <sunr...@invalid.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_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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 9 '08 #8
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,21\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::floatfield ) ;
std::cout.precision( 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,13,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]$
--
www.lispmachine.wordpress.com
my email is @ the above blog
check the "About Myself" page

Aug 18 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phuff | last post by:
I need to be able to capture events thrown by the Amyuni print driver( a 3rd parter pdf conversion product), and it has specified numbers which I believe I understand to be thw wParam of the print...
4
by: ai lian | last post by:
When I use printf to print a large double number, the result is not the same as the original input number. For example: double num=899999999999.894400; printf("%lf\n",num); The output is:...
0
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html ...
0
by: frack78 | last post by:
I have written a binary search algorithm in java. I have a statement that when the search key is found it is printed to a terminal window. This statement when when called just keeps printing over and...
4
by: Ju Hui | last post by:
I want to print 3 numbers without blank. >>> for x in range(3): .... print x .... 0 1 2 >>> for x in range(3): .... print x, ....
1
by: td0g03 | last post by:
This is the code I have so far. I want it to print it in different order like you can probably see below. But when I actually see the results it gives me different numbers. It gives me this after I...
29
by: bobby | last post by:
write a online c expression to print 1 -100 numbers with out using control statements/structures, jumps-conditional/unconditional.given the program dont modify and solve int i=1; int main()...
4
by: someone124 | last post by:
Hi, everyone. I'm trying to find the lowest and the highest numbers that a user has had punched in. The code includes a user punching in 4 different numbers, each on a different line. I need help...
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.