hello, I'm a new on c++ an i want your help... here is my problem
PI is an irrational number, i.e. it cannot be written as a fraction.
It's approximate value of PI is 3.141592653589793. Below are five
different series which can be used to approximate PI:
you can find it here::
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 PI, it
should display an error message.
//example::
Which series do you want?
1
How many digits of accuracy ?
4
\\example
output:: The series 1 it's approximate on 1000 iterations = 3.1415
It will display either an error message or the value of PI and
the number of iterations necessary to obtain that value.
In order to round a real number to n decimal places, you
can use the following algorithm 78375 divided by 10^3 = 78.375
here is my code:: (My problem is that I CAN"T find the way to round the pi1 right)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
void menu4() //menu4() - Find the number of iterations to approximate Pi.
{
int counter=0,flag=0,digits;
char series;
long double pi1=0;
long double result=0;
long double pi12=0;
long double ValuePi1=31;
long double ValuePi2=314;
long double ValuePi3=3141;
long double ValuePi4=31415;
long double ValuePi5=314159;
long double ValuePi6=3141592;
long double ValuePi7=31415926;
long double ValuePi8=314159265;
long double ValuePi9=3141592653;
long double ValuePi10=31415926535;
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;
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
if (series !='1' && series !='2' && series!='3' && series !='4' && series!='5')
{ // 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')
{ // end of if SERIES 1
cout << " How many digits of accuracy? (<10): ";
cin>>digits; // ask the number of digits
switch (digits)
{ //star of switch
case 1: result = ValuePi1/pow(10.0,digits);break;
case 2: result = ValuePi2/pow(10.0,digits);break;
case 3: result = ValuePi3/pow(10.0,digits);break;
case 4: result = ValuePi4/pow(10.0,digits);break;
case 5: result = ValuePi5/pow(10.0,digits);break;
case 6: result = ValuePi6/pow(10.0,digits);break;
case 7: result = ValuePi7/pow(10.0,digits);break;
case 8: result = ValuePi8/pow(10.0,digits);break;
case 9: result = ValuePi9/pow(10.0,digits);break;
case 10: result = ValuePi10/pow(10.0,digits);break;
} //end of switch
cout<<endl;
cout<<" ________ "<<endl;
cout<<" |Series 1| "<<endl;
cout<<" --------------------------"<<endl;
cout<<" Pi Aproximation is: "<<result<<" with "<<digits<<" digits accuracy"<<endl;
cout<<" --------------------------"<<endl;
for (int long n=1; n<=10000 && flag!=1; n++)
{//star for loop
pi1+= (4*pow(-1.0, n+1))/(2*n - 1);
pi12=pi1/pow(10.0, digits);
if (pi12==result)
{
flag=1;
}
else
counter++;
}//loop end
if (flag==1)
{
cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is "<<pi1<<endl;
cin.get();
} // if end
else
cout<<"This series doesn't approximate on Value PI, Change series or digits of accuracy"<<endl;
cin.get();
}//end of if staytment series 1
cin.get();
}//end of void ()
| |