GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2009, 22:04
exiledpaladin05 exiledpaladin05 is offline
New Member
 
Join Date: Feb 2009
Posts: 4
exiledpaladin05 is on a distinguished road

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..


CPP / C++ / C Code:
#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
  #2  
Old 10-Feb-2009, 12:59
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Help with Functions


I guessed you were mostly referring to your switch case F.
You mean you want output like this:
Code:
This program calculates Pi using one of four methods: A. Pi= 4 ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...) B. Pi= 2 ( 1 + 1/3 + (1x2)/(3x5) + (1x2x3)/(3x5x7) + ...) C. Pi= 2 x sqrt(3) x (1 - 1/3*3^1 + 1/5*3^2 - 1/7*3^3 + 1/9*3^4 - ...) D. Pi= 4 (4 x 4 x 8 x 8 x 12 x 12 x ...) / [ sqrt(2) x (3 x 5 x 7 x ...) ] E. Pi = sqrt ( 6 * ( 1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + 1/5^2 α ) ) F. Create a table of Pi values using methods A ~ E Q. End processing. Choose A through F, or Q: f Method| Term=1 Term=2 Term=3 Term=4 Term=5 Term=6 Term=7 Term=8 Term=9 dAPi : 4.000 2.667 3.467 2.895 3.340 2.976 3.284 3.017 3.252 dBPi : 2.000 2.667 2.933 3.048 3.098 3.122 3.132 3.137 3.139 dCPi : 3.464 3.079 3.156 3.138 3.143 3.141 3.142 3.142 3.142 dDPi : 3.771 3.017 3.448 3.065 3.344 3.086 3.292 3.098 3.261 dEPi : 2.449 2.739 2.858 2.923 2.963 2.991 3.012 3.027 3.040 --- End Printout --- Would you like to convert Pi again? Y: Yes. N: No.
I did it like this:
CPP / C++ / C Code:
      //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;
        }
        */
        printchart(1, 9); //You could get user input for these params
        break;
/********************************************************************/
int printchart(int beg, int end)
{
  int i , w, dpl;
  dpl= 3;             // decimal places
  w= 6 - (3 - dpl);   // adjust column for different dpl value

  cout.precision(dpl);

  cout << "Method|";
  for(i= beg; i <= end; i++)
  {
    cout.width(w);
    cout << "Term=" << i;
  }
  cout << endl << " dAPi :";

  for(i= beg; i <= end; i++)
  {
    cout.width(w + 1);
    cout << fixed << dAPi(i);
  }
  cout << endl << " dBPi :";

  for(i= beg; i <= end; i++)
  {
    cout.width(w + 1);
    cout << fixed << dBPi(i);
  }
  cout << endl << " dCPi :";
  for(i= beg; i <= end; i++)
  {
    cout.width(w + 1);
    cout << fixed << dCPi(i);
  }
  cout << endl << " dDPi :";
  for(i= beg; i <= end; i++)
  {
    cout.width(w + 1);
    cout << fixed << dDPi(i);
  }
  cout << endl << " dEPi :";
  for(i= beg; i <= end; i++)
  {
    cout.width(w + 1);
    cout << fixed << dEPi(i);
  }
  cout << endl;
  cout << "    --- End Printout ---  "<< endl;

  return 0;
}
I found the formatting info used above here:
http://www.cplusplus.com/reference/iostream/ios_base/
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with some functions baka_yaro C++ Forum 3 14-Mar-2007 06:56
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Functions and Classes - Where did I go wrong? redmage C++ Forum 5 10-Apr-2005 19:31
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.