### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203`` ``````#include #include #include #include #include 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 #include #include #include #include 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; }``````
Last edited on
A recursive function is one that calls itself. You seem to have that down. However a recursive function can only return one value to main. You could using some sort of container but your assignment wants you to use a recursive function to display a table, not return a bunch of values to convert(from my understanding of your explanation anyways).

I've changed your program around a bit to use a recursive function that prints the table. Please do not use global variables, you may want to have a look at static variables[0].

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106`` ``````#include #include #include #include #include using namespace std; void printTable(const int &, float, const float &, const float &); // Don't use global variables, when required use static variables int main() { /// MOVED VARIABLES 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 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"; printTable(noOfvalues, startValue, increment, convFactor); system("pause"); } } // use appropriate parameter names, that's what they're for void printTable(const int &valuesToGen, float valToConv, const float &incr, const float &convFactor) { if (valuesToGen > 1) { // make sure we have two values to print cout << " " << setw(7) << left << valToConv << setw(15) << left << valToConv*convFactor; if (valuesToGen % 2 == 0) // if even std::cout << endl; else // if odd std::cout << "|"; printTable(valuesToGen - 1, valToConv + incr, incr, convFactor); // CALLS ITSELF } else if (valuesToGen == 1) cout << " " << setw(7) << left << valToConv << setw(15) << left << valToConv*convFactor << "|" << endl; }``````

[0]http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
Last edited on
@ChajusSaib thank you so much! I will try compiling this.
Looks like you've got an error over and that's why it won't compile. Watch out with those errors as they can be very hard to be detected sometimes. If you need help detecting those you can try using a programs to help you, such as chrckmarx which works fine I think.
Anyway, good luck!
Ben.
Topic archived. No new replies allowed.