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. 12 1913
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.
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.
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.
Santosh Krisnan <zz***********@ zznansirk.hsotn aszzwrote:
# 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
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'.
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
#
Martin Ambuhl <ma*****@earthl ink.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_Keit h) 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.
On Wednesday 06 December 2006 03:06 pm Santosh Krisnan
<zz***********@ zznansirk.hsotn aszzwrote:
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.
On Wednesday 06 December 2006 06:49 am SM Ryan
<wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org>
wrote:
Santosh Krisnan <zz***********@ zznansirk.hsotn aszz>
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(unsign ed 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(unsign ed 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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, and
when the numbers get too large it maxes out the int and I can't count
any higher. I am trying to use extremely large numbers, I would like
to use up to 10^50 or so. So my question is how do I do this? I'm just
learning the language and I...
|
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 of the code produced by various compilers.
===========================================================
Windows 2000 Professional Ver 5.0 Build 2195 Service Pack 2
Intel(R) Celeron(R) CPU 1.70 GHz
GNU time 1.7 (to get the real time used)
|
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 newsgroup there is an ongoing discussion about the famous
fibonacci sequences, and a fine program written by a poster got my
attention so just for fun I wrote one myself and put it on the web just
about two days ago. It can be found on the site
...
|
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;
std::cout << a/b << std::endl;
|
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 example if the user gives 7 terms of the series to be displayed,
then the display of the fibonacci series is 0, 1,1, 2, 3, 5, 8.
But the natural numbers not involved are 4, 6 and 7. That's what my teacher wants.
But I am struggling to write a...
| |
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
|
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 creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and...
|
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 creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and...
|
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 program should prompt the user to enter a limit and indicate whether the last number in the sequence printed is either even or odd.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |