473,395 Members | 1,647 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,395 software developers and data experts.

trouble compiling fibonacci program

hello all,

I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?

thanks.

#include <stdio.h>
long counter;

void main(void)
{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
scanf("%ld", &fibn);
fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);
return;
}

double calc_fib(long fibn)
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1) return (double) 0;
else if(fibn == 2) return (double)1;
for(counter = 3; counter <= fibn; counter++) {
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
return fibn_value;
}

# gcc fib00.c
fib00.c: In function 'main':
fib00.c:6: warning: return type of 'main' is not 'int'
fib00.c: At top level:
fib00.c:17: error: conflicting types for 'calc_fib'
fib00.c:11: error: previous implicit declaration
of 'calc_fib' was here
#

--
email: remove z's and reverse the rest.
Dec 6 '06 #1
12 1877
Santosh Krisnan wrote:
hello all,

I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?

# gcc fib00.c
fib00.c: In function 'main':
fib00.c:6: warning: return type of 'main' is not 'int'
int main(void)
fib00.c: At top level:
fib00.c:17: error: conflicting types for 'calc_fib'
fib00.c:11: error: previous implicit declaration
of 'calc_fib' was here
There isn't a prototype for calc_fib before main, so the compiler
assumes the function returns int. Either provide a prototype, or swap
main and calc_fib.

--
Ian Collins.
Dec 6 '06 #2
Santosh Krisnan wrote:
hello all,

I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?

thanks.

#include <stdio.h>
long counter;
Use a static local variable instead of a global. It reduces inadvertant
modifications by other parts of the program, (not necessary for such a
trivial example, but it'll become useful when your programs get
bigger).
void main(void)
Standard C defines main() as either int main(void) or int main(int
argc, char **argv). Any other signature is not portable. Of course you
can use your own names instead of argc and argv, but they're canonical.
Also **argv can be written as *argv[].
{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
Unless output is terminated by a newline it's not guaranteed to appear
on stdout, (typically the screen), immediatly. Alternatively call
fflush(stdout).
scanf("%ld", &fibn);
fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);
Don't use %lf for doubles. It's non-standard. Use %f.
return;
And return an int here.
}

double calc_fib(long fibn)
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1) return (double) 0;
else if(fibn == 2) return (double)1;
for(counter = 3; counter <= fibn; counter++) {
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
return fibn_value;
}

# gcc fib00.c
fib00.c: In function 'main':
fib00.c:6: warning: return type of 'main' is not 'int'
fib00.c: At top level:
fib00.c:17: error: conflicting types for 'calc_fib'
fib00.c:11: error: previous implicit declaration
of 'calc_fib' was here
#
The compiler needs to find the prototype of a function if it's called
before it's definition. Here you call calc_fib() in main() but it's
defined only later, hence the compiler assumes a default return of int,
which conflicts with it's definition.

Place the prototype before any function definitions, right after the
header includes.

Dec 6 '06 #3
santosh said:
Santosh Krisnan wrote:
<snip>
>>
#include <stdio.h>
long counter;

Use a static local variable instead of a global.
Look again. It doesn't need to be static.

<snip>
>
>{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");

Unless output is terminated by a newline it's not guaranteed to appear
on stdout, (typically the screen), immediatly. Alternatively call
fflush(stdout).
Right.
>
> scanf("%ld", &fibn);
To the OP: check the return value. scanf can fail.
> fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);

Don't use %lf for doubles. It's non-standard. Use %f.
To be strictly accurate, it *is* now standard, since C99 codifies it. It is,
however, non-portable to C90, which is what the world and his dog actually
uses.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 6 '06 #4
Santosh Krisnan <zz***********@zznansirk.hsotnaszzwrote:
# hello all,
#
# I fiddled with BASIC in the early 90s but left it at
# that. Now I am trying to learn C. I tried to solve an
# exercise in my book, but it failes to compile. Can
# anyone tell me what the error messages mean & what I
# should do?

I would remember my combinatorial math and look up the formula
for a noniterative computation of Fn.

F(n) = ( ((1+sqrt(5))/2)^n - ((1-sqrt(5))/2)^n ) / sqrt(5)

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Dec 6 '06 #5
Richard Heathfield wrote:
santosh said:
Santosh Krisnan wrote:
<snip>
>
#include <stdio.h>
long counter;
Use a static local variable instead of a global.

Look again. It doesn't need to be static.
<snip>

Yes, sorry.

To the OP: 'counter' can be made into a local variable. Just put it
inside calc_fib() itself. You might also consider using an unsigned
type for 'fibn'.

Dec 6 '06 #6
Santosh Krisnan wrote:
hello all,

I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?

thanks.

#include <stdio.h>
long counter;

void main(void)
^^^^
main returns an int. If your book is using void as a return type for
main, its author knows nothing about even the most trivial aspects of C.
Burn the book. Do *not* give to some other unsuspecting person.
{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
scanf("%ld", &fibn);
fibn_value = calc_fib(fibn);
^^^^^^^^^^^^^^
This is your first mention of the function calc_fib(). In all
versions of C before 2001, this involves an implicit declaration of
calc_fib() as having a return type of int. From 2001 on, this "implicit
int" has disappeared and calc_fib is incorrectly used without a prior
declaration. Before use, you should have a declaration, preferably a
full prototype. A good place for it is before main, and it might look like
double calc_fib(long);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);
return;
}

double calc_fib(long fibn)
^^^^^^
The earlier implicit declaration of calc_fib was that it returned an
int. This redeclaration is obviously in conflict with that and is an
error.
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1) return (double) 0;
else if(fibn == 2) return (double)1;
for(counter = 3; counter <= fibn; counter++) {
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
return fibn_value;
}
If you had bothered to read these diagnostics, you would not have had to
ask your question. For goodness sake, what do you think diagnostic
messages are for? Read them!
# gcc fib00.c
fib00.c: In function 'main':
fib00.c:6: warning: return type of 'main' is not 'int'
fib00.c: At top level:
fib00.c:17: error: conflicting types for 'calc_fib'
fib00.c:11: error: previous implicit declaration
of 'calc_fib' was here
#
Dec 6 '06 #7
Martin Ambuhl <ma*****@earthlink.netwrites:
Santosh Krisnan wrote:
[...]
> fibn_value = calc_fib(fibn);
^^^^^^^^^^^^^^
This is your first mention of the function calc_fib(). In all
versions of C before 2001, this involves an implicit declaration of
calc_fib() as having a return type of int. From 2001 on, this
"implicit int" has disappeared and calc_fib is incorrectly used
without a prior declaration.
[...]

I think you mean 1999.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 6 '06 #8
On Wednesday 06 December 2006 03:06 pm Santosh Krisnan
<zz***********@zznansirk.hsotnaszzwrote:
hello all,

I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?
big thanks to everyone who respondad. I followed all
your suggestions and the prog. is now working
properly.

--
email: remove z's and reverse the rest.
Dec 7 '06 #9
On Wednesday 06 December 2006 06:49 am SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org>
wrote:
Santosh Krisnan <zz***********@zznansirk.hsotnaszz>
wrote:
# hello all,
#
# I fiddled with BASIC in the early 90s but left it
# at that. Now I am trying to learn C. I tried to
# solve an exercise in my book, but it failes to
# compile. Can anyone tell me what the error messages
# mean & what I should do?

I would remember my combinatorial math and look up
the formula for a noniterative computation of Fn.

F(n) = ( ((1+sqrt(5))/2)^n - ((1-sqrt(5))/2)^n ) /
sqrt(5)
Thanks. to be frank I'm not very tht strong in math,
but I implementad your formula and it's giving
different results to the earlier method, for large fib
values. I'm giving the src and sample output below.
did I do both correctly? If so which of the two is
correct? I'd be greatful if you can respond.

/* straightforward method */
#include <stdio.h>

double calc_fib(unsigned long);

int main(void)
{
int n;
unsigned long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
n = scanf("%lu", &fibn);
if(n != 1) return 1;
fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %f\n", fibn,
fibn_value);
return 0;
}

double calc_fib(unsigned long fibn)
{
unsigned long counter;
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1) return (double) 0;
else if(fibn == 2) return (double)1;
for(counter = 3; counter <= fibn; counter++) {
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
return fibn_value;
}

/* SM Ryan's method */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>

long double fibnld(unsigned long);

int main(int argc, char **argv) {
long double fib;
unsigned long nfib;

if(argc < 2) return EXIT_FAILURE;
else {
errno = 0;
nfib = strtoul(argv[1], NULL, 0);
if(errno == ERANGE || nfib == 0) {
puts("range error.");
return EXIT_FAILURE;
}
else {
fib = fibnld(nfib);
printf("The %lu fibonacci number is: %Lf\n",
nfib, fib);
}
}
return 0;
}

long double fibnld(unsigned long nfib) {
const long double sqr_five = sqrtl(5);
long double tmp1 = (1 + sqr_five) / 2;
long double tmp2 = (1 - sqr_five) / 2;

tmp1 = powl(tmp1, nfib);
tmp2 = powl(tmp2, nfib);
tmp1 -= tmp2;
tmp1 /= sqr_five;
return tmp1;
}

--
email: remove z's and reverse the rest.
Dec 7 '06 #10
Santosh Krisnan <zz***********@zznansirk.hsotnaszzwrote:
# On Wednesday 06 December 2006 06:49 am SM Ryan
# <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org>
# wrote:
# Santosh Krisnan <zz***********@zznansirk.hsotnaszz>
# wrote:
# # hello all,
# #
# # I fiddled with BASIC in the early 90s but left it
# # at that. Now I am trying to learn C. I tried to
# # solve an exercise in my book, but it failes to
# # compile. Can anyone tell me what the error messages
# # mean & what I should do?
# >
# I would remember my combinatorial math and look up
# the formula for a noniterative computation of Fn.
# >
# F(n) = ( ((1+sqrt(5))/2)^n - ((1-sqrt(5))/2)^n ) /
# sqrt(5)
#
# Thanks. to be frank I'm not very tht strong in math,
# but I implementad your formula and it's giving

Actually it's not my formula but a well known formula
http://en.wikipedia.org/wiki/Fibonacci_number

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Dec 7 '06 #11
In article <12*************@corp.supernews.comSM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
Santosh Krisnan <zz***********@zznansirk.hsotnaszzwrote:
....
# F(n) = ( ((1+sqrt(5))/2)^n - ((1-sqrt(5))/2)^n ) /
# sqrt(5)
#
# Thanks. to be frank I'm not very tht strong in math,
# but I implementad your formula and it's giving

Actually it's not my formula but a well known formula
http://en.wikipedia.org/wiki/Fibonacci_number
When using that formula you should round the final result, not truncate.
Moreover, the last part can be ignored when you do rounding. So:
f(n) = floor(pow((sqrt(5) + 1)/2, n) / sqrt(5) + 0.5);
This starts with f(1) = 1 and f(2) = 1.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Dec 7 '06 #12
Santosh Krisnan wrote:
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
>Santosh Krisnan <zz***********@zznansirk.hsotnaszzwrote:
#
# I fiddled with BASIC in the early 90s but left it
# at that. Now I am trying to learn C. I tried to
# solve an exercise in my book, but it failes to
# compile. Can anyone tell me what the error messages
# mean & what I should do?

I would remember my combinatorial math and look up
the formula for a noniterative computation of Fn.

F(n) = ( ((1+sqrt(5))/2)^n - ((1-sqrt(5))/2)^n ) / sqrt(5)
Piggybacking here, because Ryan is plonked for refusal to use
proper quotation characters.

The formula has to be faulty. I see no validity to exclusive-oring
a double with an integer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 8 '06 #13

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

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
0
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed...
12
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another...
14
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b;...
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
3
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.