Math C Programming problem

This is a discussion on Math C Programming problem within the C Programming forums, part of the General Programming Boards category; Hey, first off good effort on this site and message boards i've found the site particularly helpful as a resource ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    Math C Programming problem

    Hey, first off good effort on this site and message boards i've found the site particularly helpful as a resource for C Programming, and it looks like you have a respectable community here.

    So down to my problem. I've been working on a C Programming assignment due in tomorrow, I've hopefully successfully attached a txt file that gives you the background to the assignment for reference.

    This is the code i have so far:

    Code:
       /*Preprosessor directives*/
    
    #include <math.h>
    #include <stdio.h>
    
    
    void main()
    
    {
       /* Declarations*/
    
     	int pow3 = 1;
       float estimate = 1;
       float root;
       int k =1, N;  /*where N = number of iterations*/
       float sign;
    
    
       /*Code for iterations*/
    
       N <= 80; /*number of iterations is less than or equal to 80*/
    
       printf("Input number of iterations: ");
       scanf("%d",&N);
       printf("N = %d\n", N);
       printf("root = %f\n", root);
    
    }
    
        /*If, else and for statements for k with respect to sign and number of iterations(N)*/
    
    {
       for (k = 1, k<N; k++)
       if (k%2 == 0)
      {   sign = 1;   }
       else
      {   sign = -1;  }
    
    }
    
       /*Calculations for estimate output from increasing powers of k*/
    My problems start at step 4 in the pseudo code (included in the text file) mainly because i don't especially understand what it means. Any help with the "estimate" variable and "pow3" would be greatly appreciated (i.e. how to actually make the calculation)

    I've also attached an image of the formulae I have to get my C Program to calculate, (wouldn't copy into txt document)

    Basically i'm seeking help for code for "Calculations for estimate output from increasing powers of k" and clarification that the code before is correct. Thanks.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Don't use void main() -- use int main().

    Assignment.txt:
    Code:
    ASSIGNMENT 1
    
    Emanuele Trucco  .
    
    
    1.	THE PROBLEM
    Calculating the value of ?, the ratio between circumference and diameter, 
    has been a classic problem of mathematics throughout history. 
    One of the many formulae proposed in modern times is an infinite series created in 1699 by the 
    English mathematician Abraham Sharp:
     
    
    Write a program which computes and prints Sharpís approximation of pi using a number of terms to be 
    input from the keyboard (via  scanf ). The program must also print the difference between the 
    approximation and the value of pi stored in the macro constant M_PI, defined in <math.h>.
    To help you in this first assignment, here is a sketchy pseudo-code from which you can develop your 
    code. The variable estimate is an accumulator, that is, a variable to which you keep adding iteratively.
    We also use a variable pow3 in which we build increasing powers of 3 through the iterations, and a 
    variable sign  which takes care of the alternating plus and minus.
    
    	1. Initialize: estimate = 1; pow3 = 1;
    	2. Input number of iterations, N
    	3. For (k=1 to N, step 1) {
    	4.	if k is even, sign = 1; else sign = -1;
    	5.	pow3 = pow3*3;
    	6.	add k-th term to estimate, using sign and pow3;
    	7.	} 
    	8. Print estimate and (PI-estimate)
    
    To decide whether k is even or odd, use the remainder function of C. Its operator is the percentage 
    sign, %: for example,  11%3 evaluates to 2, as 3*3=9 and 11-9 = 2. So, if val%2  is null, then  val 
    is even.
    You made a good start.
    Code:
       /*Code for iterations*/
    
       N <= 80; /*number of iterations is less than or equal to 80*/
    
       printf("Input number of iterations: ");
       scanf("%d",&N);
       printf("N = %d\n", N);
       printf("root = %f\n", root);
    
    }
    That closing brace ends main(). You have code following that. You should put all of your code in main or other functions.
    Code:
       /*Code for iterations*/
    
       N <= 80; /*number of iterations is less than or equal to 80*/
    
       printf("Input number of iterations: ");
       scanf("%d",&N);
       printf("N = %d\n", N);
       printf("root = %f\n", root);
    
    }   /*Code for iterations*/
    
       N <= 80; /*number of iterations is less than or equal to 80*/
    
       printf("Input number of iterations: ");
       scanf("%d",&N);
       printf("N = %d\n", N);
       printf("root = %f\n", root);
    This doesn't do anything. If you're trying to make sure N is <= 80, use an if statement and put it after you read the number in with scanf(). Something like this:
    Code:
    if(N > 80) exit(1);
    root is uninitialized. You need to assign a value to it before you do anything like printing it.

    My problems start at step 4 in the pseudo code
    Code:
    4.	if k is even, sign = 1; else sign = -1;
    You have a good start:
    Code:
    for (k = 1, k<N; k++)
       if (k%2 == 0)
      {   sign = 1;   }
       else
      {   sign = -1;  }
    Your code does the same thing as the pseudo-code.

    The next step is 5:
    Code:
    	1. Initialize: estimate = 1; pow3 = 1;
    	2. Input number of iterations, N
    	3. For (k=1 to N, step 1) {
    	4.	if k is even, sign = 1; else sign = -1;
    	5.	pow3 = pow3*3;
    	6.	add k-th term to estimate, using sign and pow3;
    	7.	}
    	8. Print estimate and (PI-estimate)
    That is C code. You can stick it right into your program:
    Code:
       for (k = 1, k<N; k++)
       if (k%2 == 0)
      {   sign = 1;   }
       else
      {   sign = -1;  }
    ->
    Code:
        for (k = 1, k<N; k++) {
            if (k%2 == 0) {
                sign = 1;
            }
            else {
                sign = -1;
            }
    
            pow3 = pow3 * 3;
        }
    Step 6:
    Code:
    6.	add k-th term to estimate, using sign and pow3;
    Hint (I think):
    Code:
    estimate = k*sign*pow3;
    Step 7 isn't a step. And step 8 you might be able to figure out.

    Good luck!
    Last edited by dwks; 02-09-2006 at 03:36 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Help!!c problem in math
    By feelsunny in forum Tech Board
    Replies: 2
    Last Post: 10-06-2007, 04:35 AM
  3. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  4. math problem
    By unixOZ in forum Linux Programming
    Replies: 4
    Last Post: 10-19-2002, 01:17 AM
  5. Little math problem
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-27-2001, 08:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21