We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# help with functions

on
Medium Priority
437 Views
how do I construct mulitple functions (one for substration and division) and print the numbers based on Fibonacci series.
Ihave included the basic code for the first part of the program...I need to add a function to subtract and display the difference between succeeding numbers and also a function to calculate and display the quotients of the succeeding series in a third colum.
the display might look like this:
0
1           0     1.000000
2           1     2.000000
3           2     1.500000
5           3     1.666667
8           5     1.600000
13          8     1.625000
and so on...

/* Fibonacci series */
/* Assignment 7 */

int a = 0, b = 1, c = 0;

main()

{

int sum();
int subtract();
printf("Fibonacci\n");
printf("%5d \n", a);
printf("%5d \n", b);

do
{
c = sum(a, b);
printf("%5d\n", c);
a = b;
b = c;
}
while (c < 10000);
}
int sum(x, y)
int x, y;
{
return x + y;
}

I' ve complied this portion and it works but everything I try fro the next function is a bust...any HELP would be appreciated.  Thanks
Comment
Watch Question

## View Solution Only

Commented:
It is not at all clear what function you are trying to write. Do you want a function that prints the Fibonacci series? If so, what's the stuff about the three columns? Please try to be more specific in the future. Remember, we do not know what project you are in the middle of, so tell us all the relevant details.

Commented:
Just like slarti said, we need more details.

Commented:
Edited text of question

Commented:
Hello patricd,

It's a fairly inefficient code but it should serve
I will leave it to you to improverise and implement these functions

regards

NewGuy
---------------------

/* f here == fibanocci */
int f(int n)
{
if (n == 0 || n == 1)
return 1;
else return (f(n-1) + f(n-2));
}

int difference(int n)
{
return (f(n) - f(n-1));
}

double quotient(int n)
{
return ((double) f(n)/f(n-1));
}

Commented:
You defined a function 'sum' to add two integers?  That seems a little on the ridiculous side.

Commented:
Here is your program modified as you require.  I hope you grade me as well as you hope to be graded yourself.  It was pretty easy to do, as you have c = current and b = previous term - so you just need to divide them (make sure they cast to a double so you don't just get an int result)

/* Fibonacci series */
/* Assignment 7 */

main()
{
int a = 0, b = 1, c = 0; /* no need to make these global - its bad programming practice */

/* get rid of int sum();, what's the point? */
/* same for int subtract(); */
printf("Fibonacci\n");
printf("%5d \n", a);
printf("%5d \n", b);

do
{
c = sum(a, b);
printf("%5d %5d %8.6f\n", c, b, (double)c/b);
a = b;
b = c;
}
while (c < 10000);
}
/* get rid of this ...
int sum(x, y)
int x, y;
{
return x + y;
}
*/

Roger

Commented:
I wonder why I was rejected...

Commented:
I have not rejected any one I just haven't been able to get the various solutions to work...I really new to C..only been at it a week...I sure your suggestions are fine...it's just me.
Commented:
Here's the program:
/* Top of File */

#include <stdio.h>

int main(void)
{
puts( "Fibonacci Numbers."
" Fn   | dif Fn |  Quot"
"------+--------+---------"
);
for(int a = 0, b = 1, c = 0; c < 10000; c = a + b, a = b, b = c)
printf("%5d | %5d  | %6f\n", b, a, (float)b / a );
return 0;
}
/* End of File */

Actually I wanted it as a one liner but added the puts() to give the tabel a nicer look.
.luc.

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Oops a few characters (\n) are gone in the puts, it should read
puts( "Fibonacci Numbers.\n"
" Fn | dif Fn | Quot\n"
"------+--------+---------"
);
.luc.

Commented:
Ok, Ok there's a bug in this version. A divide by zero occurs wich will generate an exception (and crash the program under WIN32). I SHOULD always test before I submit :(
Here is the correct version. (spot the difference)

/* Top of File */

#include <stdio.h>

int main(void)
{
puts( "Fibonacci Numbers.\n"
" Fn | dif Fn | Quot\n"
"------+--------+---------"
);
for(int a = 1, b = 1, c = 0; c < 10000; c = a + b, a = b, b = c)
printf("%5d | %5d | %6f\n", b, a, (float)b / a );
return 0;
}

/* End of File */

.luc.

Commented:
thanks so very much had to modify just a bit but it works and is very compact.

Commented:
I'm not happy with the rejects here.

I provided you with an answer and it was just rejected without any explanation as to why or what was wrong.

I don't know if I'll answer anymore questions from you if you don't have the courtesy to explain WHY you are rejecting and to give the person who answered a chance to correct !!!

:-)

Please be more considerate to the experts next time

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile