Question

What to do Write a C program that computes Pi with the approximation algorithm that I...

What to do

Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways:

  • The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void);
  • The second version will break out of a while loop depending on whether a pair of adjacent elements (remember that we always consider a positive element and its adjacent negative right neighbor) is smaller than a certain threshold. I ask you to use the threshold 0.00000000000001. Please put the code of this version into a function with the signature float get_pi_while(void);

Put both functions into a file called pi.c. Also write a corresponding header file that has symbolic constants for both the number of LOOPS to be run as well as for the THRESHOLD value.

Finally, write a file main.c that contains the driver, which will print out pi twice calling the two different functions. I want you to call get_pi_for() first, then call  get_pi_while().

What to submit

  • All three code files
  • A screen shot of a run of your driver with the exact numbers for LOOPS and THRESHOLD given above

//pi.h

# define threshold 0.00000000000001
float get_pi_for();
float get_pi_while();


//pi.c

# include <stdlib.h>
# include <stdio.h>
# include <limits.h>

#include"pi.h"

//# define threshold 0.00000000000001


float get_pi_for()

{
   double pi = 0;
unsigned long index = 1;

double temp = 0;

while (temp - threshold)
{

temp += ((4.0/index) - (4.0/index+2));

index += 4;
}
pi = temp;
return pi;
}

float get_pi_while()
{
   double pi = 0;
unsigned long index = 1;

int sign = 1;

while (index < 28284277)
{

pi += 4.0/index * sign;
sign *= -1;
index += 2;
}
return pi;
}

//main.c

#include"pi.c"
#include"pi.h"

int main()

{
   printf("Value of pi using for loop is %f\n",get_pi_for());
   printf("Value of pi using while loop is %f\n",get_pi_while());
   return EXIT_SUCCESS;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// pi.h

#ifndef PI_H_

#define PI_H_

# define THRESHOLD 0.00000000000001

# define LOOPS 28284277

float get_pi_for();

float get_pi_while();

#endif /* PI_H_ */

//end of pi.h

// pi.c

#include "pi.h"

float get_pi_for()

{

               float pi = 0;

               int index = 1;

               int sign = 1;

               for(int i=0;i<LOOPS;i++)

               {

                              pi += sign*4*(((float)1)/index);

                              sign = -1*sign;

                              index += 2;

               }

               return pi;

}

float get_pi_while()

{

               float curr_pi = 4, prev_pi = 0;

               int index=3;

               int sign = -1;

               float eps = curr_pi-prev_pi;

               if(eps < 0)

                              eps = -1*eps;

               while(eps > THRESHOLD)

               {

                              prev_pi = curr_pi;

                              curr_pi += sign*4*(((float)1)/index);

                              eps = curr_pi-prev_pi;

                              if(eps < 0)

                                             eps = -1*eps;

                              index += 2;

                              sign = -1*sign;

               }

               return curr_pi;

}

//end of pi.c

// main.c

#include "pi.h"

# include <stdlib.h>

# include <stdio.h>

int main()

{

               printf("Value of pi using for loop is %f\n",get_pi_for());

               printf("Value of pi using while loop is %f\n",get_pi_while());

               return EXIT_SUCCESS;

}

//end of main.c

Output:

Add a comment
Know the answer?
Add Answer to:
What to do Write a C program that computes Pi with the approximation algorithm that I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that computes Pi with the approximation algorithm that I introduced in class....

    Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of adjacent elements (remember...

  • Need help in C (a) Write a C program to read in a line of text...

    Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...

  • I have the problem with the code, when I input 0xef1e4 0x5 3, it doesn't execute...

    I have the problem with the code, when I input 0xef1e4 0x5 3, it doesn't execute anything. I get the code from https://www.chegg.com/homework-help/programming-in-c-4th-edition-chapter-11-problem-6e-solution-9780321776419 but it seems error #include <stdio.h> int intSize(void) { unsigned int n= ~0; int counter = 4; while(n>0) { n=n>>1; counter++; } return counter-1;//account for sign bit } //function to calculate the min number of bits needed to represent a value int numberOfBits(unsigned int x) { int sizeOfInt=intSize(); int counter = 0; unsigned int temp = 1<<(sizeOfInt...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. The program must be in one file. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • program in C. please paste code, thanks! CS 153 Program 4 - Stirling Approximation (separate compilation)...

    program in C. please paste code, thanks! CS 153 Program 4 - Stirling Approximation (separate compilation) 1. Write a pure function double factoriallint Ni that computes N! N 1023°4 ... N-1)'N Do this the obvious way by initializing a product to one and then multiplying the integers together using a loop. Zero factorial is defined to be one. Now write a pure function double atining in Ni that uses the Stirling approximation to compute: approx[e!) - V22) In this formula,...

  • Write a c++ program to calculate the approximate value of pi using this series. The program...

    Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough. You will try it with numbers read in from a file. to see the accuracy increase. Use a for loop...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT