Solved

Using a do-while loop and function  calls

Posted on 2000-03-14
2
1,790 Views
Last Modified: 2010-08-05
main()
{
    int a = 0, b = 1, c = 0               /* initialize the series to 0, 1 */
    int sum();                              /* declare function to calc sums */

    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) /* the parameters x and y receive the values of */
int x, y;          /* the two arguments passed by sum(a, b) in main() */

{

    return x + y;

}


Question: I need to use a subtract function to calculate and display the differences between the suceeding numbers in the fibonacci series.
I need to calculate and display thr quotients of the succeding fibonacci series in a third data column, right next to the differences.  For the divisions I need a division function named divide to perform this calculation.  To preserve the decimal accuracy of thequotients I will need to declare and use a float type function with two arguments  
0
Comment
Question by:mvjohn
2 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2615611
When the new number is calculated, the old number remains in b, so the difference could be obtained from c - b, which is probably equal to a, provided that sum(a,b) calculates a + b ... The quotient is double(c) / b. I don't see why you would need functions for these simple expressions
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 10 total points
ID: 2619912
KangaRoo: Because it is a school/college/uni assignment? :-)

mvjohn: for accuracy, use a double, not a float type.  float types are not much use (except to save memeory when you are using a LOT of them).

Also, you are using yucky old-style C declarations.  This is bad programming practice.  You should declare your sum function as:

  int sum (int x, int y);

and define it as:

  int sum (int x, int y) {
    return x+y;
  }

I think, without giving too much away, your subtract and divide functions would be

  int subtract (int x, int y) {
    return x-y;
  }

  double divide (double x, double y) {
    return x/y;
  }

then you'd change
  printf("%5d\n", c);
into
  printf("%5d %5d %8.5f\n", c, subtract(c,b), divide(c,b));

PS: you'll probaby want to put some error checking in the divide routine to avoid division by zero.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now