J
jugaaru
How to generate fibonacci mubers in C ?
jugaaru said:How to generate fibonacci mubers in C ?
How to generate fibonacci mubers in C ?
jugaaru said:How to generate fibonacci mubers in C ?
jugaaru said:How to generate fibonacci mubers in C ?
jugaaru said:How to generate fibonacci mubers in C ?
> On 22 Feb 2004 07:59:08 -0800, (e-mail address removed) (jugaaru) wrote:
>
>
> At least two ways: iteratively (create an array, and populate it
> on-the-fly), or recursively (have a function return 0 and 1 for the first
> two Fibonacci numbers, and then have it calculate any other by calling
> itself to acquire the previous two.
Papadopoulos Giannis said:Since it is definately a hw, I do not think that anyone should give you
an answer - the only way to learn is to get your feet wet..
But consider the following:
1) In the recursive solution, the resulting code is really trivial and
small (hint: the smallest possible is 3 lines).. Perfomance is missing
though...
2) Prefer the iterative solution, although it may seem a bit more
complicated..
When I was given the exercise, I had also to optimize my code for many
calls to the fibonacci procedure..
I used a static array to hold already calculated fibonacci numbers, to
speed up execution...
That is, the first time you calculate the 10th number, the array was
filled with some of the previous numbers.. Now, if you have to calculate
the 20th number, you don't need to do it all over again... Calculating
the 5th number is tricky though ...
3) I think I said enough already...
--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little)(Big);return 0;}
Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
jugaaru said:Yeah you are right, this is a Homework. I have just started learning
C, I am having trouble learning it. First 2-3 chapters, i can
understand variables,equation, somethings like maths. But when the
faculty start giving assignment. You have no clue how to do it.
Please advise how do i learn C programming.
Julian V. Noble said: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.
Yeah you are right, this is a Homework. I have just started learning
C, I am having trouble learning it. First 2-3 chapters, i can
understand variables,equation, somethings like maths. But when the
faculty start giving assignment. You have no clue how to do it.
Please advise how do i learn C programming.
CBFalconer said:[...]
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.
[...]
n = strtoul(argv[1], NULL, 10);
Eric said:CBFalconer said:[...]
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.
[...]
n = strtoul(argv[1], NULL, 10);
If you mean that strtoul("-1", NULL, 10) returns ULONG_MAX,
then there's no bug: that's what it is supposed to do. See
the Standard, section 7.20.1.4 paragraph 3, and note the text
"optionally preceded by a plus or minus sign."
CBFalconer said:On re-reading the standard, I can see that out. However this
implementation actually returns ULONG_MAX - 1 for input of -2,
That's exactly what it's supposed to do. The negation happens
in the return type, which means that it's *unsigned* negation in
this case and there is no overflow. You only get overflow if
the input string represents a value greater than ULONG_MAX.
CBFalconer said:That makes no sense to me. If -2 is handled with modulo
arithmetic, so should a string representing ULONG_MAX + 2. All of
which makes a mockery of overflow detection. ...
Neg args should yield 4294967295 and error
CBFalconer said:That's exactly what it's supposed to do. The negation happens
in the return type, which means that it's *unsigned* negation in
this case and there is no overflow. You only get overflow if
the input string represents a value greater than ULONG_MAX.
That makes no sense to me. If -2 is handled with modulo
arithmetic, so should a string representing ULONG_MAX + 2. All of
which makes a mockery of overflow detection. At any rate here is
a baby demo I put together to show what we are talking about.
Output for "strbug 3" follows, under djgpp/gcc. Note that it
never sets errno.
I have added a crosspost to comp.std.c.
Neg args should yield 4294967295 and error
strtoul("-3junk") = 4294967293 remnant "junk"
strtoul("-2junk") = 4294967294 remnant "junk"
strtoul("-1junk") = 4294967295 remnant "junk"
strtoul("0junk") = 0 remnant "junk"
strtoul("1junk") = 1 remnant "junk"
strtoul("2junk") = 2 remnant "junk"
strtoul("3junk") = 3 remnant "junk"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char **argv)
{
unsigned int n;
int i;
char s[25];
char *rem;
if (2 != argc) puts("Usage: strbug N");
else {
n = strtoul(argv[1], NULL, 10);
if (n > 10) puts("Max. N is 10");
else {
printf("Neg args should yield %lu and error\n",
(unsigned long)-1);
for (i = -n; i <= (int)n; i++) {
sprintf(s, "%djunk", i);
errno = 0;
printf("strtoul(\"%s\") = %lu",
s, strtoul(s, &rem, 10));
Douglas A. Gwyn said:.... snip ...
I don't understand your argument. ULONG_MAX+2 is not
representable in the type, so any sort of dealing with
such a string internally would have to enter a special
case, for which we require overflow to be reported.
There is no special case needed for the implementation
to negate (modulo wordsize).
Frankly, I don't like the idea of allowing sign on a
textual representation of an unsigned integer value,
but that's the legacy we're stuck with.
Why?
In comp.std.c CBFalconer said:That makes no sense to me. If -2 is handled with modulo
arithmetic, so should a string representing ULONG_MAX + 2.
CBFalconer said:Because the value -1 is outside the range of an unsigned.
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.