ok here is my problem ( I will explane it )
Π is an irrational number, i.e. it cannot be written as a fraction.
It's approximate value of Π is 3:141592653589793. Below are 5
different series which can be used to approximate Π:
you can find the series on this link::: (it's a picture)
http://i44.tinypic.com/2ujtmxw.gif
the function ask which series he/she wants to use
and the precision (number of digits, up to 10). If after
10000 iterations the series doesn't find the value of Π, it
should display an error message.
Which series do you want?
2
How many digits of accuracy ?
5
It will display either an error message or the value of Π and
the number of iterations necessary to obtain that value.
In order to round a real number to n decimal places....
first the user select series....( i have the code for all series so just see only with the first one)
then enter how many digits of accuracy want
The iterations of series is 10000, first we want to round pi1;(pi1 is Π) to the number that enter the user
and we must round the const approximate;(that it's the number that approximate the value Π )
then we want to make our function to calculate step by step all the iterations and make a check, if find that is == with the const then print the results else print error....
My code is here::: My bigger problem is how i can round the const and the pi value before the calculation????
[cpp]
void menu4() //menu4() - Find the number of iterations to approximate Pi.
{
int digits,counter=0;
char series;
double pi1=0;
const double Approximate=3.141592653589793;
system("cls"); // clear screen
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(68)<< "| Choice 3. |" << endl;
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(68)<< "| |" << endl;
cout <<setw(68)<< "| |" << endl;
cout <<setw(68)<< "| Find the number of iterations to approximate Pi. |" << endl;
cout <<setw(68)<< "| |" << endl; //Menu4() - Find the number of iterations to approximate Pi.
cout <<setw(68)<< "| |" << endl;
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(24)<< "|Series|"<<endl;
cout <<setw(24)<< "--------"<<endl;
cout << "1. P= 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-..."<<endl;
cout << "2. P= sqrt(12*(1- (1/4)+(1/9)-(1/16)+(1/25)-...))"<<endl;
cout << "3. P= sqrt(6*(1+ (1/4)+(1/9)+(1/16)+(1/25)+...)) "<<endl; // menu of the series
cout << "4. P= sqrt(8*(1+ (1/9)+(1/25)+(1/49)+... )) "<<endl;
cout << "5. P= sqrt(24*((1/4)+(1/16)+(1/36)+(1/64)+... ))"<<endl;
cout << "--------------------------------------------------"<<endl;
cout << " Which series? (1-5): ";
cin>>series; // ask the series
cout << " How many digits of accuracy? (<10): ";
cin>>digits; // ask the number of digits
if ((series !='1' && series !='2' && series!='3' && series !='4' && series!='5') || ( digits <1 || digits>10))
{ // if start
cout <<endl;
cout <<" Error Input....."<<endl;
cout <<" Their are Only 5 series...(1,2,3,4,or5)"<<endl;
cout <<" And you can add different digits number.. digits>=1 or digits <=10"<<endl;
cout <<" The program will be terminated in 5 seconds..."<<endl;
Sleep(5000); // stop the program in 5 seconds
} // if end
else
if (series=='1')
{
cout<<endl;
cout<<" ________ "<<endl;
cout<<" |Series 1| "<<endl;
cout<<" --------------------------"<<endl;
cout<<" Pi Aproximation: "<<fixed<<setprecision(digits)<<Approximate<<endl;
cout<<" --------------------------"<<endl;
for (int long n=1; n<=400; n++)
{//star for loop
pi1+= (4*pow(-1.0, n+1))/(2*n - 1);
counter++; //counter for the iterations
if(pi1==Approximate)
{
cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is "<<pi1<<endl;
break;
cin.get();
}
else
cout<<"This series doesn't approximate with this digits"<<endl;
cin.get();
}
cin.get();
} //end of if staytment series 1
} //end of void()
[/cpp]
Π is an irrational number, i.e. it cannot be written as a fraction.
It's approximate value of Π is 3:141592653589793. Below are 5
different series which can be used to approximate Π:
you can find the series on this link::: (it's a picture)
http://i44.tinypic.com/2ujtmxw.gif
the function ask which series he/she wants to use
and the precision (number of digits, up to 10). If after
10000 iterations the series doesn't find the value of Π, it
should display an error message.
Which series do you want?
2
How many digits of accuracy ?
5
It will display either an error message or the value of Π and
the number of iterations necessary to obtain that value.
In order to round a real number to n decimal places....
first the user select series....( i have the code for all series so just see only with the first one)
then enter how many digits of accuracy want
The iterations of series is 10000, first we want to round pi1;(pi1 is Π) to the number that enter the user
and we must round the const approximate;(that it's the number that approximate the value Π )
then we want to make our function to calculate step by step all the iterations and make a check, if find that is == with the const then print the results else print error....
My code is here::: My bigger problem is how i can round the const and the pi value before the calculation????
[cpp]
void menu4() //menu4() - Find the number of iterations to approximate Pi.
{
int digits,counter=0;
char series;
double pi1=0;
const double Approximate=3.141592653589793;
system("cls"); // clear screen
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(68)<< "| Choice 3. |" << endl;
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(68)<< "| |" << endl;
cout <<setw(68)<< "| |" << endl;
cout <<setw(68)<< "| Find the number of iterations to approximate Pi. |" << endl;
cout <<setw(68)<< "| |" << endl; //Menu4() - Find the number of iterations to approximate Pi.
cout <<setw(68)<< "| |" << endl;
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(24)<< "|Series|"<<endl;
cout <<setw(24)<< "--------"<<endl;
cout << "1. P= 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-..."<<endl;
cout << "2. P= sqrt(12*(1- (1/4)+(1/9)-(1/16)+(1/25)-...))"<<endl;
cout << "3. P= sqrt(6*(1+ (1/4)+(1/9)+(1/16)+(1/25)+...)) "<<endl; // menu of the series
cout << "4. P= sqrt(8*(1+ (1/9)+(1/25)+(1/49)+... )) "<<endl;
cout << "5. P= sqrt(24*((1/4)+(1/16)+(1/36)+(1/64)+... ))"<<endl;
cout << "--------------------------------------------------"<<endl;
cout << " Which series? (1-5): ";
cin>>series; // ask the series
cout << " How many digits of accuracy? (<10): ";
cin>>digits; // ask the number of digits
if ((series !='1' && series !='2' && series!='3' && series !='4' && series!='5') || ( digits <1 || digits>10))
{ // if start
cout <<endl;
cout <<" Error Input....."<<endl;
cout <<" Their are Only 5 series...(1,2,3,4,or5)"<<endl;
cout <<" And you can add different digits number.. digits>=1 or digits <=10"<<endl;
cout <<" The program will be terminated in 5 seconds..."<<endl;
Sleep(5000); // stop the program in 5 seconds
} // if end
else
if (series=='1')
{
cout<<endl;
cout<<" ________ "<<endl;
cout<<" |Series 1| "<<endl;
cout<<" --------------------------"<<endl;
cout<<" Pi Aproximation: "<<fixed<<setprecision(digits)<<Approximate<<endl;
cout<<" --------------------------"<<endl;
for (int long n=1; n<=400; n++)
{//star for loop
pi1+= (4*pow(-1.0, n+1))/(2*n - 1);
counter++; //counter for the iterations
if(pi1==Approximate)
{
cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is "<<pi1<<endl;
break;
cin.get();
}
else
cout<<"This series doesn't approximate with this digits"<<endl;
cin.get();
}
cin.get();
} //end of if staytment series 1
} //end of void()
[/cpp]