Thread: Recursion Function Problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Recursion Function Problem

    Code:
    #include <cstdlib> 
    #include <ctime>
    #include <iomanip>
    #include <iostream>
    
    using namespace std; // prototype and name space declaration
    int sqr(int);
    int cube(int);
    int fourth(int);
    int fifth(int);
    
    bool printNum();
    void show(int (*fn)(int), int); 
    
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    
    void main()
    {	
    	
    	while(printNum())
    		;
    
    }
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    
    bool printNum()
    {
    	char input;
    	int block =0;
    	
    	cout<< "To Square Enter:           S"<<endl;
    	cout<< "To Cube Enter:             C"<<endl;
    	cout<< "To the Fourth Power Enter: F"<<endl;
    	cout<< "To the Fifth Power Enter:  H"<<endl;
    	cout<< "To Quit: Q"<<endl;
    	cin >> input;
    	
    	while ( input != 's' && input != 'S'
    			 && input != 'c' && input != 'C'
    			  && input != 'f' && input != 'F'
    			   && input != 'h' && input != 'H'
    			    && input != 'q' && input != 'Q')
    	{		
    
    		cout << "Wrong Choice.\n";
    		cout << "Enter Again: ";
    		cin.clear();
    		cin.ignore(80,'\n');
    		cin >> input;
    	}
    	
              if ( input == 'q' || input =='Q')
    			{
    			cout << "Program Ended. "<<endl;
    			return 0;
    				
    				}
    	while (true)
    
    		{
    
    			int lowVal ,maxVal;
    
    
    			cout <<"Enter A Number For Low Bound: ";
    			cin >> lowVal;
    			
    			cout <<"Enter A Number For Low Bound: ";
    			cin >> Val;
    		
    			if ( input == 'q' || input =='Q')
    				{
    				cout << "Program Ended. "<<endl;
    				return 0;
    				
    				}
    			if ( (lowVal == 0 && maxVal == 0))
    				printNum();
    
    			cout <<"\n";
    			cout <<"Printing "<<(maxVal-lowVal)+1<<" numbers"<<endl;
    			cout <<"\n";
    
    			for (int x = lowVal; lowVal<=maxVal; lowVal++)
    			{
    				switch (input)
    				{
    				case 's':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    
    				case 'S':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    				case 'c':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(cube, lowVal);
    					block++;
    					break;
    					}
    
    				case 'C':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(cube, lowVal);
    					block++;
    					break;
    					}
    								
    				case 'f':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fourth, lowVal);
    					block++;
    					break;
    					}
    
    				case 'F':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fourth, lowVal);
    					block++;
    					break;
    					}
    				case 'h':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fifth, lowVal);
    					block++;
    					break;
    					}
    
    				case 'H':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fifth, lowVal);
    					block++;
    					break;
    					}
    
    				}
    
    				if (block == 10)
    				{
    					block = 0;
    					cout<<"\n";
    				}
    			}
    			cout <<"\n";
    			block = 0;
    			
    	}
    
       
    }
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    int sqr(int num) // list of functions 
    {
        return num * num;
    }
    
    int cube(int num) // this one is to cube
    {
        return num * num * num;
    }
    int fourth(int num)
    {
        return num * num * num * num;
    }
    int fifth(int num)
    {
        return num * num * num * num * num;
    }
    
    void show(int (*fn)(int), int num) // pointer
    {
        cout << fn(num);
    }
    Last edited by nicz888; 11-28-2007 at 09:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM