• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
patricdAuthor Commented:
Edited text of question
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.