Fibonacci number

W

Wojtek Lerch

Richard Bos said:
Not words, exactly, but it does seem to imply that. All four of
strtol(), strtoul(), strtoll() and strtoull() share a paragraph, and
that paragraph mentions "the" subject sequence, singular. The only
difference that is explicitly mentioned is that between the type to
which these functions convert the subject sequence.

OK, the suffix is recognized by strtoul(), too, but doesn't affect its
value. The rule could be that the presence of the suffix causes the value
to be negated if the return type is signed. Does that sound good enough?...
 
R

Richard Bos

Wojtek Lerch said:
OK, the suffix is recognized by strtoul(), too, but doesn't affect its
value. The rule could be that the presence of the suffix causes the value
to be negated if the return type is signed. Does that sound good enough?...

Have you actually _read_ that part of the Standard?

Richard
 
W

Wojtek Lerch

Richard Bos said:
enough?...

Have you actually _read_ that part of the Standard?

The one that says, 'In other than the "C" locale, additional locale-specific
subject sequence forms may be accepted.'? Sure, several times. It doesn't
say much about how the values of strtol() and strtoul() are related to each
other, does it?
 
Joined
Jun 4, 2011
Messages
6
Reaction score
0
FIBONOCI IN C

"Julian V. Noble" wrote:
> jugaaru wrote:
> >
> > How to generate fibonacci mubers in C ?

>
> The algorithm is
>
> F(n+1) = F(n) + F(n-1) , F(0) = 0, F(1) = 1
>
> You can do this recursively (very stupid and slow--see my article
> "Recurses!" in Computing in Science and Engineering) or iteratively
> (much better).
>
> I won't tell you how to do it since it is evidently homework.


But I will for two reasons: 1. Enough time has passed so that the
homework should have been passed in. 2. If the OP can rework the
following into something the instructor will believe is his, he
will have learned something.

BTW the following shows up a glitch in DJGPP 2.03 system. Calling
the program with an argument of -1 returns the overflow condition,
because strtoul returns ULONG_MAX rather than 0. Cross-posted to
comp.os.msdos.djgpp for this.

#include <stdio.h>
#include <stdlib.h>

/* ------------------ */

/* deliberately written to upset some style mavens */
/* returns ULONG_MAX for overflow */
static unsigned long fibo(unsigned int n)
{
unsigned long pprev, prev, value;

if ((pprev = 0) == n) value = pprev;
else if ((prev = 1) == n) value = prev;
else do {
value = pprev + prev; pprev = prev; prev = value;
if (value < pprev) {
value = -1; /* ULONG_MAX, overflow */
goto x;
}
} while (2 <= --n);
x: return value;
} /* fibo */

/* ------------------ */

int main(int argc, char **argv)
{
unsigned int n;

if (2 != argc) puts("Usage: fibo N");
else {
n = strtoul(argv[1], NULL, 10);
printf("fibonacci(%u) = %lu\n", n, fibo(n));
}
return 0;
} /* main */

--
Chuck F ([email protected]) ([email protected])
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////:congrats:
#include <stdio.h>
#include<iostream>
int main()
{
int top, Alpha=0, Beta=1, Total=1;
printf("Please input a top number: ");
scanf("%d",&top);
printf("0\n");

for(int i = 0; i <= top; i++)
{
Total = Alpha + Beta;
printf("%d\n",Total);
Alpha = Beta;
Beta = Total;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top