Create a program that does the following: Part 1 Approximate Pi to 10 places wit
ID: 3703259 • Letter: C
Question
Create a program that does the following:
Part 1 Approximate Pi to 10 places with the following formulas from using integer division and modulus operators (https://en,.wikipedia.org/wiki/Approximations of PI): 22/7 25/8 256/81 339/108 These are each stored in an array of size 10, each digit in a separate element. For instance, 3.1415926535 would be stored 3 1 4 1 5 9 2 6 5 4 Note: the rounding... void CalculatePi (int myPI, int size, int numerator, int denominator); Part 2 Print each array with a below the location the value first differs from the above value of P For instance: 3.139123456 would print out 3 1 3 9 1 2 3 4 5 6 void PrintArray (int myPII, int goodPI [1, int size);Explanation / Answer
ScreenShot
-----------------------------------------------------------------------------------------------------------------------------
Program
//Header File
#include<iostream>
#include<string>
#include <iomanip> // setprecision
#include <sstream> // stringstream
using namespace std;
//Function declaration
void CalculatePi(char myPI[], int size, int numerator, int denominator);
void printArray(char myPI[], char goodPI[], int size);
//Main function
int main()
{
//Variable declaration
char array[10];
//call function PI calculation
CalculatePi(array, 10, 22, 7);
return 0;
}
//PI calculation Function definition
void CalculatePi(char myPI[], int size, int numerator, int denominator) {
//Local variables
int j = 0;
char goodPI[] = { '3','1','3','9','1','2','3','4','5','6' };
//PI value finding
double val =double (numerator )/ double(denominator);
//Convert into string
stringstream stream;
stream << fixed << setprecision(9) << val;
string s = stream.str();
//Add each characters into array
for (int i = 0; i < s.size(); i++) {
if (s[i]!='.') {
myPI[j]=s[i];
j++;
}
}
//Print PI array
cout << "PI Array" << endl;
for (int i = 0; i < 10; i++) {
cout << myPI[i] << " ";
}
cout << endl;
//Call difference array function
printArray(myPI, goodPI, 10);
}
//Definition of difference calculation array
void printArray(char myPI[], char goodPI[], int size) {
//Check the difference and add * into that place
for (int i = 0; i < size; i++) {
if (myPI[i] != goodPI[i]) {
goodPI[i] ='*';
break;
}
}
//Print the array
cout << "Good Array" << endl;
for (int i = 0; i < 10; i++) {
cout << goodPI[i] << " ";
}
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.