Solved

help with functions

Posted on 1997-11-12
13
364 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:patricd
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 2

Expert Comment

by:Slarti
ID: 1256257
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
 
LVL 2

Expert Comment

by:kellyjj
ID: 1256258
Just like slarti said, we need more details.
0
 

Author Comment

by:patricd
ID: 1256259
Edited text of question
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Expert Comment

by:NewGuy
ID: 1256260
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
 
LVL 10

Expert Comment

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

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256262
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
 

Expert Comment

by:NewGuy
ID: 1256263
I wonder why I was rejected...
0
 

Author Comment

by:patricd
ID: 1256264
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
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 100 total points
ID: 1256265
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1256266
Oops a few characters (\n) are gone in the puts, it should read
puts( "Fibonacci Numbers.\n"
      " Fn | dif Fn | Quot\n"
      "------+--------+---------"
    );
.luc.
0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1256267
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
 

Author Comment

by:patricd
ID: 1256268
thanks so very much had to modify just a bit but it works and is very compact.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256269
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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question