One way is to turn the recursive function into a helper function which is called from your actual function. This actual function will not print the comma, but it does call the recursive function that does print the comma.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No, at the moment you don't have a recursive helper function. Your explode function that only has one parameter is both the function called in main and the recursive function, whereas my suggestion is to make the function called in main non-recursive and not print the comma, and have that non-recursive function call the recursive helper function that prints the comma.Originally Posted by Kelton2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I have changed it back to iteration and I now have:
It prints backwards. How do I fix it?Code:#include <iostream> using namespace std; int explode(int number,int array[]) { int variable = number; int numberOfDigits = 0; int digit = number % 10; array[digit] = digit; while (number > 0) { int digit = number % 10; int numbe = number; array[numberOfDigits] = digit; cout << array[numberOfDigits]; number /= 10; numberOfDigits++; } int i = 0; int save = 0; save = numberOfDigits; for(i = 0; i < numberOfDigits/2; i++) { array[i] = array[save--]; // cout << "Starting arrayprint:"; // cout << array[i] << endl; } // cout << "End of arrayprint"; // return array[i]; /* while (number > 0) { number /= 10; numberOfDigits++; } */ return numberOfDigits; } /*int printDigits(int number,int array[]) { int digit = number % 10; array[digit] = digit; while (number > 0) { int digit = number % 10; int numbe = number; array[digit] = digit; cout << array[digit]; number /= 10; } int i = 0; for(i = 0; i < number/2; i++) { array[i] = number-i-1; } return array[i]; } */ int main ( ) { int number; int array[10]; cout << "Enter number: "; cin >> number; cout << "["; explode(number,array); cout << ","; cout << "]"; }
Update: It still doesn't work, but I have updated my code. It now has a revised main and prints in a different way. My code is below:
Code:Can someone help?Code:#include <iostream>#include <iomanip> #include <cmath> using namespace std; int explode(int number,int array[]) { int variable = number; int numberOfDigits = 0; int digit = number % 10; array[digit] = digit; while (number > 0) { int digit = number % 10; int numbe = number; array[numberOfDigits] = digit; // cout << array[numberOfDigits]; number /= 10; numberOfDigits++; } int i = 0; int save = 0; save = numberOfDigits; for(i = 0; i < numberOfDigits/2; i++) { array[i] = array[i]; // cout << "Starting arrayprint:"; // cout << array[i] << endl; } // cout << "End of arrayprint"; // return array[i]; /* while (number > 0) { number /= 10; numberOfDigits++; } */ return numberOfDigits; } /*int printDigits(int number,int array[]) { int digit = number % 10; array[digit] = digit; while (number > 0) { int digit = number % 10; int numbe = number; array[digit] = digit; cout << array[digit]; number /= 10; } int i = 0; for(i = 0; i < number/2; i++) { array[i] = number-i-1; } return array[i]; } */ int main() { int digits[10]; int numdigits; int n; cout << "Enter number: "; cin >> n; numdigits = explode(n,digits); cout << "The following is numdigits: " << numdigits; cout << endl; cout << "[" << digits[0]; for( int i = 1; i < numdigits; i++ ) cout << "," << digits[i]; cout << "]" << endl; }
Nevermind, solved.
If you can figure out the number of digits the number has in advance, you can use a plain loop to store the numbers in correct order:
As for how to get the number of digits a number will have, a big hint is to consider the underlined expression in the for loop above (number /= 10). This expression evaluates to zero when there are no more digits left in a number. You can use this to get the number of digits.Code:void extractDigits (int number, int* array, int sizeArray) { for (int i = std::min(sizeArray - 1, numberDigits(number)); i >= 0; i--) { array[i] = number % 10; number /= 10; } }
WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;