• C

help with functions

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
patricdAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
LucHoltkampConnect With a Mentor 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.
0
 
SlartiCommented:
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.
0
 
kellyjjCommented:
Just like slarti said, we need more details.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
patricdAuthor Commented:
Edited text of question
0
 
NewGuyCommented:
Hello patricd,

It's a fairly inefficient code but it should serve
I will leave it to you to improverise and implement these functions
please contact me if you have difficulties

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));
}
0
 
RONSLOWCommented:
You defined a function 'sum' to add two integers?  That seems a little on the ridiculous side.

0
 
RONSLOWCommented:
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
0
 
NewGuyCommented:
I wonder why I was rejected...
0
 
patricdAuthor 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.
0
 
LucHoltkampCommented:
Oops a few characters (\n) are gone in the puts, it should read
puts( "Fibonacci Numbers.\n"
      " Fn | dif Fn | Quot\n"
      "------+--------+---------"
    );
.luc.
0
 
LucHoltkampCommented:
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.
0
 
patricdAuthor Commented:
thanks so very much had to modify just a bit but it works and is very compact.
0
 
RONSLOWCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.