calculating iteratively

This is a discussion on calculating iteratively within the C++ Programming forums, part of the General Programming Boards category; My program is suppost to show the factorial of a number loop 10 times. During each iteration, calculate // factorial ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    14

    Unhappy calculating iteratively

    My program is suppost to show the factorial of a number loop 10 times. During each iteration, calculate
    // factorial ( i ) and display results.

    Now I need to be able to do this by adding a function to the code to do it iteratively.

    this part works but i need to add a nonrecursively function to it

    Code:
    // recursive
    // written by
    // cis
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    unsigned long factorial ( unsigned long );
    
    int main()
    
    {
    	
    	// loop 10 times. during each iteration, calculate
    	// factorial ( i ) and display result.
    	for ( int i = 0; i <= 10; i++ )
    		cout << setw ( 2 ) << i << "! = " << factorial ( i ) << endl;
    	return 0;
    }
    
    unsigned long factorial ( unsigned long number )
    
    {
    	// base case
    	if ( number <= 1 )
    		return 1;
    
    	// recursive step
    	else
    		return number * factorial ( number - 1 );
    }
    now this part will figure it nonrecursively

    Code:
    factorial = 1;
    for ( int counter = number; counter >= 1; counter-- )
    factorial *= counter;
    here is where i'm having problems I can't get it to run properly
    this is what i have so far

    sorry for the amount of code
    any hints in the right direction would be appreciated

    Code:
    // recursive.cpp
    // written
    // cis
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    unsigned long factorial ( unsigned long );
    int factorial1 ( int, int );
    int main()
    
    {
    	int f = 0,
    		factor = 1;
    	// loop 10 times. During each iteration, calculate
    	// factorial ( i ) and display results.
    	for ( int i = 0; i <= 10; i++ )
    		cout << setw ( 2 ) << i << "! = "
    		<< factorial ( i ) << endl;
    	cout << "! = " << factor << endl;
    
    
    	return 0;
    } //  end main
    
    // recursive definition of function factorial
    
    unsigned long factorial ( unsigned long number )
    {
    	// base case
    	if ( number <= 1 )
    		return 1;
    
    	// recursive step
    	else return number * factorial ( number - 1 );
    }// end function factorial
    factorial1 (int number,int factor  )
    {
    	 	for ( int counter = number; counter >= 1; counter-- )
    		factor *= counter;
    	while (counter <= 10 );
    	return factor;
    }

  2. #2
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Sorry I can't help maybe this will.
    If you ever need a hug, just ask.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    here, try this:

    Code:
    int factorial1 (int number,int &factor  )
    {
        int counter;
    
        for(counter = number; counter > 0; --counter) {
            factor *= counter;
        }
    }
    Making factor a reference parameter would be the easiest way. you would have to cout factor in main though. the way you have it, once the return is hit, it is out of the fxn. it won't keep returning until the counter is done. once one return statement is hit, the fxn is done.

    this also assumes that factor is passed as one to begin with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2006, 12:08 AM
  2. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 10:38 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 05:47 PM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 09:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21