Thread: Need help with a recursive function

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    Need help with a recursive function

    Hello,


    I have an assignment coming up in which we are supposed to input a value and then convert it into another value by multiplying it with a conversion factor/multiplier. The inputs are the units of the input value and converted value, the conversion factor, the value to start with, the increments and the amount of increments to generate. Once it has been generated, it needs to be displayed in a table like the one below.


    Inches Centimetres| Inches Centimetres
    1.00 2.5400000 | 2.00 5.0800000
    3.00 7.6200000 | 4.00 10.1600000
    5.00 12.7000000 | 6.00 15.2400000
    7.00 17.7800000 | 8.00 20.3200000
    9.00 22.8600000 | 10.00 25.4000000


    Displaying this table seems pretty straightforward however we are told to try using recursive functions to try and display the converted values. I can't seem to do that part. Any ideas? I made part of the code but the actual value calculation using recursive functions seems confusing. Any ideas?

    Code:
    #include <cmath>
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <ctype.h>
    
    using namespace std;
    
    string originalUnit; //Name of original measurement unit
    string convertedUnit; //Name of converted measurement unit
    float convFactor = 0; //Conversion factor/multiplier
    float startValue = 0; //Starting value of conversion table
    float increment = 0; //Increment from one original measurement value to the next
    int noOfvalues = 0; //Number of original measurement values to be generated
    
    float x = 0;
    
    float calculation(int, float, float) ;
    
    int main()
    {
    	cout << "Unit Conversion Table Generator\n\n";
    	for(;;)
    	{
    		do
    		{
    			cout << "Enter name of the original measurement unit (e.g. inches): ";
    			cin >> originalUnit;
    			if (isalpha(originalUnit[0]))
    			{
    				cout << originalUnit << "\n";
    			}
    			else
    			{
    				cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
    			}
    		} while (!isalpha(originalUnit[0]));
    
    		do
    		{
    			cout << "Enter name of the converted measurement unit (e.g. centimetres): ";
    			cin >> convertedUnit;
    			if (isalpha(convertedUnit[0]))
    			{
    				cout << convertedUnit << "\n";
    			}
    			else
    			{
    				cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
    			}
    		} while (!isalpha(convertedUnit[0]));
    
    		cout << "Enter conversion factor / multiplier (e.g. 2.54): ";
    		cin >> convFactor;
    
    		cout << "Enter the starting value of the conversion table for the original measurement unit: ";
    		cin >> startValue;
    
    		cout << "Enter the increment from one original measurement value to the next: ";
    		cin >> increment;
    
    		do
    		{
    		cout << "Enter the number of original measurement values to be generated: ";
    		cin >> noOfvalues;
    			if (noOfvalues > -1)
    			{
    				cout << noOfvalues << "\n\n";
    			}
    			else if (noOfvalues < 0)
    			{
    				cout << "ERROR: Value must be positive.\n";
    			}
    		} while (noOfvalues < 0);
    
    		cout << originalUnit << " to " << convertedUnit << " Unit Conversion Table\n\n";
    
    		cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "|"; 
    		cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "\n";
    
    		cout << " " << setw(7) << left << startValue << setw(15) << left << startValue*convFactor << "|";
    		cout << " " << setw(7) << left << startValue + increment << setw(15) << left << (startValue + increment)*convFactor << "\n";
    		
    		calculation(noOfvalues, startValue, increment);
    
    
    		
    		system("pause");
    		}
    }
    
    
    
    float calculation(int b /*noOfvalues*/, float a /*startValue*/, float c /*increment*/)
    {
    	if (x <= b*a)
    	{
    		cout << "Value to convert: " << x << endl;
    		calculation(b, a, c + x);
    	}
    return 0;
    }#include <cmath>
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <ctype.h>
    
    using namespace std;
    
    string originalUnit; //Name of original measurement unit
    string convertedUnit; //Name of converted measurement unit
    float convFactor = 0; //Conversion factor/multiplier
    float startValue = 0; //Starting value of conversion table
    float increment = 0; //Increment from one original measurement value to the next
    int noOfvalues = 0; //Number of original measurement values to be generated
    
    float x = 0;
    
    float calculation(int, float, float) ;
    
    int main()
    {
    	cout << "Unit Conversion Table Generator\n\n";
    	for(;;)
    	{
    		do
    		{
    			cout << "Enter name of the original measurement unit (e.g. inches): ";
    			cin >> originalUnit;
    			if (isalpha(originalUnit[0]))
    			{
    				cout << originalUnit << "\n";
    			}
    			else
    			{
    				cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
    			}
    		} while (!isalpha(originalUnit[0]));
    
    		do
    		{
    			cout << "Enter name of the converted measurement unit (e.g. centimetres): ";
    			cin >> convertedUnit;
    			if (isalpha(convertedUnit[0]))
    			{
    				cout << convertedUnit << "\n";
    			}
    			else
    			{
    				cout << "ERROR: Value must start with an alphabet character. Please try again.\n";
    			}
    		} while (!isalpha(convertedUnit[0]));
    
    		cout << "Enter conversion factor / multiplier (e.g. 2.54): ";
    		cin >> convFactor;
    
    		cout << "Enter the starting value of the conversion table for the original measurement unit: ";
    		cin >> startValue;
    
    		cout << "Enter the increment from one original measurement value to the next: ";
    		cin >> increment;
    
    		do
    		{
    		cout << "Enter the number of original measurement values to be generated: ";
    		cin >> noOfvalues;
    			if (noOfvalues > -1)
    			{
    				cout << noOfvalues << "\n\n";
    			}
    			else if (noOfvalues < 0)
    			{
    				cout << "ERROR: Value must be positive.\n";
    			}
    		} while (noOfvalues < 0);
    
    		cout << originalUnit << " to " << convertedUnit << " Unit Conversion Table\n\n";
    
    		cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "|"; 
    		cout << " " << setw(7) << left << originalUnit << setw(15) << left << convertedUnit << "\n";
    
    		cout << " " << setw(7) << left << startValue << setw(15) << left << startValue*convFactor << "|";
    		cout << " " << setw(7) << left << startValue + increment << setw(15) << left << (startValue + increment)*convFactor << "\n";
    		
    		calculation(noOfvalues, startValue, increment);
    
    
    		
    		system("pause");
    		}
    }
    
    
    
    float calculation(int b /*noOfvalues*/, float a /*startValue*/, float c /*increment*/)
    {
    	if (x <= b*a)
    	{
    		cout << "Value to convert: " << x << endl;
    		calculation(b, a, c + x);
    	}
    return 0; }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A couple of points.

    1. Lose all those global variables, by putting them in main() and passing them as parameters (as needed).

    2. Recursion is a loop.
    Code:
    for ( i = 0 ; i < 10 ; i++ ) {
      // do stuff with i
    }
    vs
    Code:
    void foo ( int i ) {
      if ( i < 10 ) {
        // do stuff with i
        foo(i+1);
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-18-2013, 11:01 PM
  2. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  3. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  4. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  5. recursive function
    By brianptodd in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2003, 01:08 PM

Tags for this Thread