Hey friends,

Started c++ not to logn ago and took right to it, up until now I have had no problems whatsoever. Now, with my specific assignment I have absolutely no idea what do to. I dont even know where to begin. I feel foolish and embarrassed and I'll i ask is that you folks can help point me in the right direction, Im not askign for the program code but maybe a fews hints at where to get started or an algorithm to help me along the way. Any help at all will be greatly appreciated.

The following is a snipped of the program retirements and I guess my first and biggest problem is that I have no idea what the program is supposed to do. Also keep in mind I am TERRIBLE at math so the thought of pi throws me for a total frenzy.
------------------------------------------------------

Here are 2 series that can be used to approximate the value of π :

π/4 = 1/1 - 1/3 + 1/5 - 1/7 + . . .

π2/24 = 1/22 + 1/42 + 1/62 + 1/82 + . . .

Design a C++ program that will use these 2 series to approximate the value of π. The number of terms to be used in the approximation will be read from a file. The number of terms must be an int, but use double type variables when performing arithmetic related to generating approximations.

Input File - This is the first program that will be designed to run in batch mode (read from a file, no prompts necessary). The data file will consist of a series of int values. Each value will be separated by whitespace and will be greater than zero. The first value in the data file will indicate the number of data sets to be processed. Each data set will consist of a single int value. This value represents the number of terms to be used in computing the approximation of π. See sample compilation and execution below.

Required Output - As output your program must generate a table (see sample below) with 4 columns. The estimates of π will be displayed in the first 2 columns with 12 digits to the right of the decimal. The 3rd column will be used to display the absolute value of the difference (also with 12 digits to the right of decimal) between the 2 estimates (see fabs, page 1504 in text). Display the number of terms used in the approximations in column 4 (maximum number of terms will be 100,000,000). Each column must have an appropriate heading and the headings and values must be right justified.

Running a program in batch mode using Linux redirection

1. When designing your program, omit the prompts. If you expect data to be read from a file, messages to the user are not necessary.
2. Create data file(s) to test your program. Use emacs to create a file. Include only the data values that you want the program to read. Make sure that the data is in the proper order and formatted as specified in the assignment. Save the file (its name should not end with .cpp).
3. Compile your program in the normal manner.
4. To run your program and use the data file you created as input, type in the command: ./a.out < datafilename

Sample compilation and execution
[l***@bobby]$ more data06
2
15
100
[***@bobby]$ g++ hw06.cpp
[***@bobby]$ ./a.out < data06
Estimate 1 Estimate 2 Difference # Terms
3.208185652262 3.079389826032 0.128795826230 15
3.131592903559 3.132076531809 0.000483628251 100
--------------------------------------------------------------------

Again thanks for the help and I look forward to joinign the community!

update: been working for the past hour and got this much. any ideas where i should go from here?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const double one = 1;
const double two = 2;

int main()
{

  double i;
  double sum = 0;
  int term;


  cin >> term; 

       for (i =1; i <= term; i++)
     {
       sum = sum + 4 * (1/(2*i-1))*pow(-1,(i - one));
     }
       for (i =1; i <= term; i++)
     {
       sum = sum +sqrt (24*(1 / pow(two * i,two)));
     }


    return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If you are terrible at math, that'll make things more difficult. The second estimation formula doesn't ring a bell, but the first does and is here. You should probably read the whole article and the links.

http://en.wikipedia.org/wiki/Pi#Classical_period

It's a mathematical series. It gets closer and closer to an estimate of PI. You are given the number of terms to go through, so you need to set up a running total variable, initialize it to 0, then set up a for-loop. Within the loop, add a term to the running total. For example, if some variable a was given by the formula:

a = 1 + (1 / 2^3) + (1 / 2^6) + (1 / 2 ^9) + ...

and you had to calculate to the 5th term you could do something like this:

double a = 0;
int numTerms = 5;

for (int i = 0; i < numTerms; i++)
{
    double term = pow (2.0, i * -3);
    a = a + term;
}

Now substitute the formula for the PI estimate.

update: been working for the past hour and got this much. any ideas where i should go from here?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const double one = 1;
const double two = 2;

int main()
{

double i;
double sum = 0;
int term;


cin >> term;

for (i =1; i <= term; i++)
{
sum = sum + 4 * (1/(2*i-1))*pow(-1,(i - one));
}
for (i =1; i <= term; i++)
{
sum = sum +sqrt (24*(1 / pow(two * i,two)));
}


return 0;
}

Didn't see this when I posted. Use code tags please. You're on the right track in some ways. Look at my example. I think using term as the counter variable is misleading. the computer doesn't care, but it'll confuse the reader. The term is added to the running total.

I don't see the need for these variables. Just use 1.0 and 2.0 if you need to use a double.

const double one = 1;
const double two = 2;

These are two separate estimates of PI, right? So shouldn't sum be reinitialized to 0 before the second loop?

for (i =1; i <= term; i++)
{
sum = sum + 4 * (1/(2*i-1))*pow(-1,(i - one));
}
for (i =1; i <= term; i++)
{
sum = sum +sqrt (24*(1 / pow(two * i,two)));
}
This article has been dead for over six months. Start a new discussion instead.