User Defined Function

W/ While loops. Formatting to main

Page 1 of 1

3 Replies - 2323 Views - Last Post: 15 February 2009 - 02:20 PM Rate Topic: -----

#1 slicshake   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 01-June 07

User Defined Function

Posted 14 February 2009 - 09:05 PM

You can ignore this if short on time /IGNORE
My requirements are to write a program that finds pi like: ∏ = 4 4/3 + 4/5 4/7 + 4/9 4/11... etc + . It needs to prompt user for the number of times to run the above equation. I.E. only once would equal 1, running it twice would give you 4-4/3 so 2.67. Lastly it wants the user to enter the the number of times the program should display the results. I.E. if a user wants 100 values of pi done every 10, it should print 10 results. I.E. Pi @ 10 = 3.048
pi @ 20 =... etc up to 100. All of the input, needs to be done in a user defined function, and the display count should also be done in the user defined function. /END IGNORE

Here is an example of what is wanted:

Enter the number of terms to use: 500

Display Pi after every how many steps? 0
ERROR Input must be positive and non-zero! Please try again.

Display Pi after every how many steps? 100

RESULTS:

100: Pi = 3.131592904
200: Pi = 3.136592685
300: Pi = 3.138259330
400: Pi = 3.139092657
500: Pi = 3.139592656

Final Pi = 3.139592656



You can ignore my code if you want, im not really asking for an answer, just an example of a similar problem that can let me figure mine out. I want to learn this, im just confused on how to out put this to main in the correct form.

Here is my code, it is very jumbled as i threw it together. Just an example of where I am at. My out at 10 values of pie, results every 1 works fine. However, 10 values of pie at 2 results, is not correct.
int userInput();
int main ()
{
double numer; //numerator 
double denom; //denominator 
int pi, check, test;
double input, num, num2, math, output;

pi = userInput ();


cout << pi;

cout << endl << endl;

system("PAUSE");
}

int userInput(){
	double output, input, num, num2, math, numer;
	int test, check;
	test = 0;
	numer = 1;
	math = 0;
	  do{
	   cout << "Enter the number of terms to use: ";
	   cin >> check;	   
			 if (check <= 0){
			 cout << endl << "ERROR - Input must be positive and non-zero!  Please try again."
			 << endl << endl;
			 }
	   }
	   while (check <= 0);
	   
	   do{
	   cout << endl;
	   cout << "Display Pi after every how many steps? ";
	   cin >> input;	   
			 if (input <= 0){
			 cout << endl << "ERROR - Input must be positive and non-zero!  Please try again."
			 << endl << endl;
			 }
	   }
	   while (input <= 0);
	   
	   
	 num = input;
	 num2 = 1;
	 while (test < check ){
		   
	  if (test % 2 == 0)
		   math = math - (4 / numer);   
	  else if (test % 2 == 1) 
		   math = math + (4 / numer);  
	  numer += 2;
		 test++;	  
		
			 
		input = num;
		input = input * test;
		num2++;
		cout << input << " " << -math << endl;
		
	  }
	
	
	  cout << endl;
	  return check;
	  cout << endl;
} 


Is This A Good Question/Topic? 0
  • +

Replies To: User Defined Function

#2 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: User Defined Function

Posted 15 February 2009 - 02:48 AM

You can first define a function that gets the number of terms to compute from the user:

int getNumTerms();
//...
//...
int getNumTerms()
{
	int terms;

	cout << "Enter the number of terms to use: ";
	cin >> terms;

	if(terms < 1) {
		cout << "The number of terms cannot be less than one." << endl << endl;
		terms = getNumTerms();
	}

	return terms;
}


You can then define a function to get the number of steps from the user:

int getNumSteps(int);
//...
//...
int getNumSteps(int terms)
{
	int steps;

	cout << "Display pi every how many steps? ";
	cin >> steps;

	if(steps < 1) {
		cout << "The number of steps cannot be less than one." << endl << endl;
		steps = getNumSteps(terms);
	} else if(steps > terms) {
		cout << "The number of steps cannot be more than the number of terms." << endl << endl;
		steps = getNumSteps(terms);
	}

	return steps;
}


We can call each to get the number of terms and steps:

int numTerms, numSteps;

numTerms = getNumTerms();
numSteps = getNumSteps(numTerms);


We can then create a variable to store the value of pi that's been computed so far:

double pi = 0.0f; // Initialize pi to 0


We can also create two variables to store the numerator and denominator of the next fraction in the series:

// Initialize the numerator to 4 and the denominator to 1: the fraction
// 4/1
double numer = 4.0f;
double denom = 1.0f;


The best way to calculate pi for each step is to, of course, use a loop. So let's create a controlled loop. The loop should loop once for each term that the user specified:

int i;

for(i = 0; i < numTerms; i ++) {
	// ...


In each pass in the loop, we're going to add or subtract a fraction from pi. First, let's create a variable to hold the fraction (of course, the value of the fraction is going to equal the numerator divided by the denominator):

for(i = 0; i < numTerms; i ++) {
	double fraction = (numerator / denominator);
	// ...



We add the fraction to pi on every odd term and subtract from pi on every term. Let's write it test to see if the current term is odd or even, and update pi appropriately:

for(i = 0; i < numTerms; i ++) {
	double fraction = (numerator / denominator);

	if(((i + 1) % 2) == 0) // Even term; subtract fraction from pi
		pi -= fraction;
	else // Odd term; add fraction to pi
		pi += fraction;
	//...


After each pass, the denominator increases by two, so lets add that in:

//...
	else // Odd term; add fraction to pi
		pi += fraction;
	
	denom += 2;
	//...


We have to display the value of pi every numSteps steps. To determine if we should print pi, we're going to see if the current term number is divisible by numSteps. If it is, we should print pi:

//...
	denom += 2;

	if(((i + 1) % numSteps) == 0)
		cout << (i + 1) << ": Pi = " << pi << endl;
	//...


After the loop, you can print the final value of pi:

		//...
		cout << (i + 1) << ": Pi = " << pi << endl;
	}

	cout << "Final Pi = " << pi << endl;


You can put all of this functionality into one function:

int getNumTerms();
int getNumSteps(int);
void calculatePi();
//...
//...
void calculatePi()
{
	int numTerms;
	int numSteps;

	numTerms = getNumTerms();
	numSteps = getNumSteps(numTerms);

	double pi = 0.0f;
	double numer = 4.0f;
	double denom = 1.0f;
	int i;

	for(i = 0; i < numTerms; i ++) {
		double fraction = (numer / denom);

		if(((i + 1) % 2) == 0)
			pi -= fraction;
		else
			pi += fraction;

		denom += 2;

		if(((i + 1) % numSteps) == 0)
			cout << (i + 1) << ": Pi = " << pi << endl;
	}

	cout << "Final Pi = " << pi << endl;
}
,

and call it in main:

int main()
{
	calculatePi();
	return 0;
}


Here is the entire program:

//
// Program: Pi Calculator
//
// File: picalc.cpp
//
// Purpose: Calculates pi using the Leibiz formula. The program
//		  prompts the user for the number of terms in the series,
//		  to factor in. The program then prints the value of pi
//		  computed thus far after every certain number of steps,
//		  also given by the user
//
// Author: Brenton Saunders
//
// Date: 15 Jan 2009
//
// Revision History: N/A
//
#include <iostream>

using namespace std;

int getNumTerms();
int getNumSteps(int);
void calculatePi();

int main()
{
	calculatePi();

	return 0;
}

void calculatePi()
{
	int numTerms;
	int numSteps;

	numTerms = getNumTerms();
	numSteps = getNumSteps(numTerms);

	double pi = 0.0f;
	double numer = 4.0f;
	double denom = 1.0f;
	int i;

	for(i = 0; i < numTerms; i ++) {
		double fraction = (numer / denom);

		if(((i + 1) % 2) == 0)
			pi -= fraction;
		else
			pi += fraction;

		denom += 2;

		if(((i + 1) % numSteps) == 0)
			cout << (i + 1) << ": Pi = " << pi << endl;
	}

	cout << "Final Pi = " << pi << endl;
}

int getNumTerms()
{
	int terms;

	cout << "Enter the number of terms to use: ";
	cin >> terms;

	if(terms < 1) {
		cout << "The number of terms cannot be less than one." << endl << endl;
		terms = getNumTerms();
	}

	return terms;
}

int getNumSteps(int terms)
{
	int steps;

	cout << "Display pi every how many steps? ";
	cin >> steps;

	if(steps < 1) {
		cout << "The number of steps cannot be less than one." << endl << endl;
		steps = getNumSteps(terms);
	} else if(steps > terms) {
		cout << "The number of steps cannot be more than the number of terms." << endl << endl;
		steps = getNumSteps(terms);
	}

	return steps;
}

Was This Post Helpful? 1
  • +
  • -

#3 slicshake   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 01-June 07

Re: User Defined Function

Posted 15 February 2009 - 01:47 PM

Thanks so much, I was able to work through via your examples and figured it out. I was unsure of how to input values from function to function and this answered my question. I was also unsure of how to do properly out user defined number of steps, thought I was on to something like you have, wasn't quite their yet however. Brilliant! Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: User Defined Function

Posted 15 February 2009 - 02:20 PM

I'm glad I can help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1