Thread: The "For" loop

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    The "For" loop

    I have a homework assignment where I'm supposed to calculate the values for theta when given the value of pi and I'm sure I have to use the "for" loop however I don't know how the code for this would work.

    Here is what I have so far:

    Code:
    double theta (int limit);
    const double pi = 3.141592653589793;
    
    int main (void)
    {
       //Local Declaration
       int limit;
    
       //Statements
       printf("pi = %lf\n", pi);
       limit=5;
       printf ("\nNumber of terms = %d\nTheta = %lf\nDifference between theta and pi = %lf\n\n",
       limit, theta(limit), theta(limit) - pi);
       limit=10;
       printf ("\nNumber of terms = %d\nTheta = %lf\nDifference between theta and pi = %lf\n\n",
       limit, theta(limit), theta(limit) - pi);
    Here's the problem if it helps:

    In this problem you will build a main function, that acts as a test driver for your theta (Θ) function.
    In the figure in the book for problem 50, theta (Θ) is used for the approximation of pi (Π).

    Make the return type from the theta function a double, the value of theta.
    Make the parameter of the theta function an int, the number of terms to be used in the approximation.

    Call the theta function from main two times.
    The first time, pass the integer 5 to request 5 terms in the approximation.
    The second time, pass the integer 10 to request 10 terms in the approximation.
    In the main function print a neat listing of the results.
    First print the value of pi (which is approximatly 3.141592653589793),
    Then print the following for each of the two passes:

    * The number of terms
    * The values calculated for theta
    * The difference between the values of theta and pi
    Another thing, when I run what I have so far into the compiler, an error comes up saying that my printf statement is undefined. I think this is an error with my compiler, but I'm not positive. If anyone could verify this for me it would be greatly appreciated. Using Bloodshed Dev C++

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to hang out with this guy.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    Quote Originally Posted by PaperBag View Post

    Code:
       printf ("\nNumber of terms = %d\nTheta = %lf\nDifference between theta and pi = %lf\n\n",
       limit, theta(limit), theta(limit) - pi);
    
    
       printf ("\nNumber of terms = %d\nTheta = %lf\nDifference between theta and pi = %lf\n\n",
       limit, theta(limit), theta(limit) - pi);
    for your printf statements, unlike with some functions, you can't separate it into multiple lines, and that's probably what your compiler is complaining about.

    try it like this:
    Code:
    /*your first printf*/
     printf ("\nNumber of terms = %d \n",limit);
    
     printf ("Theta = %lf\n",theta(limit));
    
    printf ("Difference between theta and pi = %lf \n\n",theta(limit) - pi);
    
    /*this also makes it more readable*/
    this is all assuming what you're putting in for your %?'s are correctly done. hope i could help =P

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by synhyborex View Post
    for your printf statements, unlike with some functions, you can't separate it into multiple lines, and that's probably what your compiler is complaining about.

    try it like this:
    Code:
    /*your first printf*/
     printf ("\nNumber of terms = %d \n",limit);
    
     printf ("Theta = %lf\n",theta(limit));
    
    printf ("Difference between theta and pi = %lf \n\n",theta(limit) - pi);
    
    /*this also makes it more readable*/
    this is all assuming what you're putting in for your %?'s are correctly done. hope i could help =P
    Sure you can. I'm guessing the error is stemming from the fact that the OP doesn't have #include <stdio.h> at the top of the file.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Oh wow... sorry about that guys. I'm rather new at this as Quzah indirectly pointed out

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Does anyone still know how this "for" loop is supposed to look? I'm reading the lessons on this site right now, but unfortunately I'm still confused.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Didn't you read quzah's post?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM