
09-Feb-2009, 22:04
|
|
New Member
|
|
Join Date: Feb 2009
Posts: 4
|
|
|
Help with Functions
Hey guys I am a new user to this site and I need some help with functions. I get the whole idea of functions in that you create one to make your code neater and call upon the function when needed. The problem I'm having is creating an output function. I need to create an output function to output the values of pi using different methods.... it sounds confusing I know but the code may help..
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
//User-Defined Functions
char getMethod (); //Calls Method used
double getNTerms (); //Calls number of terms
double getiFi ();
double dAPi (int n); //Calls A Method
double dBPi (int n); //Calls B Method
double dCPi (int n); //Calls C Method
double dDPi (int n); //Calls D Method
double dEPi (int n); //Calls E Method
double dFPi (int n); //Calls F Method
double allPi ();
void output (double dPi, char Method_Name, int n_Terms); //Calls Output
int main()
{
char cA; //chosing a method
char cR=0; //to run program again
int i; //counter
int n; //user input
int iFi; //incriments by user input
double dPi=0; //total Pi for output
double dPisum=0; //Pi sum before further calculations
double dDenom=1; //denomenator variable
double dNumerator=1; //Numerator variable
double dBDenom =3; //second denomenator for further calculations
double dCDenom=1; //thcRd denomenator for even further calculations
double dBNumerator=1; //method B numerator
double dDenom2=1; //denomenator sum
double dNumeratorSum=1; //numerator sum
double dCNumerator=1;
// for output formatting 10
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
cout << "This program calculates Pi using one of four methods:"<<endl;
cout << "A. Pi= 4 ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...)"<<endl;
cout << "B. Pi= 2 ( 1 + 1/3 + (1x2)/(3x5) + (1x2x3)/(3x5x7) + ...)"<<endl;
cout << "C. Pi= 2 x sqrt(3) x (1 - 1/3*3^1 + 1/5*3^2 - 1/7*3^3 + 1/9*3^4 - ...)"<<endl;
cout << "D. Pi= 4 (4 x 4 x 8 x 8 x 12 x 12 x ...) / [ sqrt(2) x (3 x 5 x 7 x ...) ]"<<endl;
cout << "E. Pi = sqrt ( 6 * ( 1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + 1/5^2 à ) )"<<endl;
cout << "F. Create a table of Pi values using methods A ~ E"<<endl;
cout << "Q. End processing."<<endl<<endl;
//for repeating program while user does not input 'n' or 'N'
while ((cR != 'n') && (cR != 'N'))
{
// reinitializing values
dPi=0;
dPisum=0;
dDenom=1;
dNumerator=1;
dBDenom=3;
dCDenom=1;
dBNumerator=1;
dDenom2=1;
//calls user's method
cA = getMethod();
//switch statement for choosing the method A-E, with a default and Quit option
switch (cA)
{
//for choice a or A
case ('A'):
case ('a'):
n= getNTerms();
dPi= dAPi(n);
break;
//for choice b or B
case ('B'):
case ('b'):
n= getNTerms();
dPi= dBPi(n);
break;
//for choice c or C
case ('C'):
case ('c'):
n= getNTerms();
dPi= dCPi(n);
break;
//for choice d or D
case ('D'):
case ('d'):
n= getNTerms();
dPi=dDPi(n);
break;
//for choice e or E
case ('E'):
case ('e'):
n= getNTerms();
dPi=dEPi(n);
output
break;
//for choice f or F
case ('F'):
case ('f'):
n = getNTerms();
iFi = getiFi();
cout << "N Method A Method B Method C Method D Method E"<<endl;
//allPi = dFPi (n);
for (i=iFi;i<=n;i+=iFi)
{
cout <<i<<" "<<dAPi(i) <<" "<< dBPi(i)<<" "<<dCPi(i) <<" "<<dDPi(i) <<" "<<dEPi(i)<<endl;
}
break;
// ends program when user inputs q or Q
case ('q'):
case ('Q'):
cout<< "bye!"<<endl;
return 0;
break;
default:
cout << "The input is incorrect"<<endl<<endl;
}
//promts user if they'd want to rerun program
cout << "Would you like to convert Pi again?" <<endl;
cout << "Y: Yes. N: No." <<endl<<endl;
cin >> cR;
}
cout << "bye!";
return 0;
}
//user Method
char getMethod()
{
char cA;
cout << "Choose A through F, or Q: "<<endl;
cin >> cA;
return cA;
}
// Number of Terms
double getNTerms()
{
double n;
cout << "How many Terms?"<<endl;
cin >> n;
return n;
}
// returns method A
double dAPi(int n)
{
int i=0;
double dNumerator=0;
double dDenom=1;
double dPisum=0;
double dPi=0;
for (i=1,dNumerator=-1;i<=n;i++,dDenom+=2)
{
dNumerator = dNumerator *-1.0;
dPisum += dNumerator/dDenom;
}
dPisum = dPisum*4.0;
dPi = dPisum;
return dPi;
}
//returns method B
double dBPi (int n)
{
int i=0;
double dNumerator=1;
double dDenom=1;
double dPisum=0;
double dPi=0;
double dBDenom=3;
double dBNumerator=1;
for (i=2,dPisum=1,dDenom=1,dBDenom=3;i<=n;i++,dBNumerator++,dBDenom+=2)
{
dNumerator = (dNumerator * dBNumerator);
dDenom = (dDenom * dBDenom);
dPisum += dNumerator/dDenom;
}
dPisum = dPisum*2.0;
dPi = dPisum;
return dPi;
}
//returns method C
double dCPi (int n)
{
int i; //counter
double dPi=0; //total Pi for output
double dPisum=0; //Pi sum before further calculations
double dDenom=1; //denomenator variable
double dNumerator=1; //Numerator variable
double dBDenom =3; //second denomenator for further calculations
double dCDenom=1; //thcRd denomenator for even further calculations
double dBNumerator=1; //method B numerator
double dDenom2=1; //denomenator sum
double dNumeratorSum=1; //numerator sum
double dCNumerator=1;
for(i=1,dPisum=0,dNumerator=-1,dDenom=1,dCDenom=0;i<=n;i++,dDenom+=2,dCDenom++)
{
dNumerator = dNumerator *-1.0;
dDenom2 = (pow(3.0,dCDenom));
dPisum += dNumerator/(dDenom *dDenom2);
}
dPisum= (sqrt(3.0))*(dPisum);
dPi= dPisum*(2.0);
return dPi;
}
//returns method D
double dDPi (int n)
{
int i; //counter
double dPi=0; //total Pi for output
double dPisum=0; //Pi sum before further calculations
double dDenom=1; //denomenator variable
double dNumerator=1; //Numerator variable
double dBDenom =3; //second denomenator for further calculations
double dCDenom=1; //thcRd denomenator for even further calculations
double dBNumerator=1; //method B numerator
double dDenom2=1; //denomenator sum
double dNumeratorSum=1; //numerator sum
double dCNumerator=1;
for(i=1,dCNumerator=2,dCDenom=0,dDenom=1,dNumerator=1,dNumeratorSum=0,dBNumerator=4,dBDenom=3;i<=n;i++,dBNumerator+=2,dBDenom+=2,dCNumerator+=2)
{
if (i%2==0)
{
dNumerator = dNumerator * dCNumerator; //dCNumerator starts at 2.. every 2 cycles turns doubles
dDenom = (dDenom * dBDenom);
}
else
{
dNumerator = dNumerator * dBNumerator; //dBNumerator starts at 4.. every 2 cycles turns doubles
dDenom = (dDenom * dBDenom);
}
dNumeratorSum= dNumerator;
dCDenom= dDenom;
}
dNumeratorSum = dNumeratorSum *(4.0);
dCDenom = (sqrt(2.0))*(dCDenom);
dPisum= dNumeratorSum/dCDenom;
dPi= dPisum;
return dPi;
}
//returns method E
double dEPi (int n)
{
int i; //counter
double dPi=0; //total Pi for output
double dPisum=0; //Pi sum before further calculations
double dDenom=1; //denomenator variable
double dNumerator=1; //Numerator variable
for(i=1;i<=n;i++,dDenom++)
{
dPisum+=(1.0/(dDenom*dDenom));
}
dPisum=dPisum*6;
dPi= sqrt(dPisum);
return dPi;
}
//returns method F incriments
double getiFi ()
{
double iFi;
cout << "What incriment should be used ?" << endl;
cin >> iFi;
return iFi;
}
//returns method F
/*double dFPi(int n)
{
int i;
double iFi;
for (i=iFi;i<=n;i+=iFi)
{
allPi= <<i<<" "<<dAPi(i) <<" "<< dBPi(i)<<" "<<dCPi(i) <<" "<<dDPi(i) <<" "<<dEPi(i)<<endl;
}
return allPi;
}
*/
//void output (double dPi, char Method_Name, int n_Terms)
//{
// return dPi;
again i have to create a function that will print the output within my switch statement...
thanks again 
Last edited by LuciWiz : 10-Feb-2009 at 02:43.
Reason: Please insert your C++ between [cpp] & ]/cpp] tags
|
|