It's data (what's printed finally is data[i]). What do I need to change in it?
It's data (what's printed finally is data[i]). What do I need to change in it?
I can't get this to work, please tell me what I am missing:
Code:#include <iostream> #include <iomanip> #include <cmath> using namespace std; int explode(int number, int array[]) { int count = 0; int thing = 0; int digit = 0; int returner = 0; for(int i = 0; i < number; i++) { digit = number % 10; number = number / 10; thing += digit; returner = number % 10; } cout << digit; return returner; } int main() { int digits[10]; int numdigits; int n; cout << "Enter number: "; cin >> n; numdigits = explode(n,digits); cout << "[" << digits[0]; for( int i = 1; i < numdigits; i++ ) cout << "," << digits[i]; cout << "]" << endl; }
Last edited by Kelton2; 02-17-2015 at 06:23 PM.
You have to stop with these "doesn't work" posts. First of all, even if we want to help you, you're also dumping code and expecting everyone to figure out the problem AND the solution. It makes you look like you're lazy and don't care about your own work. We can't give you solutions that you can just hand in.I can't get this to work, please tell me what I am missing:
You do realize that numbers are a sum of powers of ten right? 123456789 = 9*1 + 8*10 + 7*100 + 6*1000 + 5*10,000 + 4*100,000 + 3*1,000,000 + 2*10,000,000 + 1*100,000,000
As you peel off digits you have to shrink the number by a power of ten. The code obviously starts with the smallest powers first.
Go back to simpler code and ask yourself:
1) How is the array storing the number?
2) Where do I start in the array if I want to print back the number? You know that you can't start at 0; you've seen that it is wrong.
You can fix this without make explode very complicated. Remember, the problem is the order of the output, not the output itself. If you can't figure out the array's workings, try one of laserlight's earlier suggestions.
I figured it out, but it prints backwards:
Code:#include <iostream> #include <iomanip> #include <cmath> using namespace std; void explode(int number,int array[]) { while (number > 0) { int digit = number % 10; cout << digit << '\n'; number /= 10; } } int main() { int digits[100]; int numdigits; int n; cout << "Enter number: "; cin >> n; // numdigits = explode(n,digits); cout << "["; while (n > 0) { int digit = n % 10; n /= 10; digits[digit] = digit; // cout << digits[digit]; } cout << "]" << endl; }
A digit has no real tie with where it is in the number. O_o What if the digit you peeled off was 1 in 90210. It doesn't go in digits[1], does it?Code:digits[digit] = digit;
The source of the error in all versions of the code is obvious. You keep saying that it prints backwards. You need to reverse the order of the digits you print. That is all. Nothing else is wrong.
Yes, but it won't always be in that position. I printed digits[4] anyways, and it didn't work. I don't understand how I can get it to print in the right order.
not workingCode:#include <iostream> #include <iomanip> #include <cmath> using namespace std; int explode(int number,int array[]) { int digit = number % 10; while (number > 0) { int digit = number % 10; // cout << digit << '\n'; number /= 10; } return digit; } int main() { int digits[100]; int numdigits; int n; cout << "Enter number: "; cin >> n; numdigits = explode(n,digits); // cout << numdigits << endl; int array[numdigits]; cout << "["; for(int i = 0; i < n; i++) { int digit = n % 10; // n /= 10; // digits[digit] = digit; cout << array[numdigits]; } cout << "]" << endl; }
The first digit of the number is always going to be in the last position. That was what I was trying to illustrate.
We have yet another case of you not thinking.I printed digits[4] anyways, and it didn't work.
You understand how the number is being printed right now, right?I don't understand how I can get it to print in the right order.
You need to reverse this process. You should know how to do this yourself. Go back to the code where the only problem was that the number printed backwards and fix it.
Kelton, for the love of freaking God, google, "How to print an array backwards in C".
You've declared 'digit' twice. As for the printing backwards, I like laserlights suggestion of using recursion for this. It seems the fastest way without using intermediate array memory. I dunno.
just remember:Code:void try(){ try() };
WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;
I got it to work with recursion, but it prints one too many comma and IDK how to fix it:
Can someone help?Code:#include <iostream> #include <iomanip> #include <cmath> using namespace std; int explode(int number,int array[]) { int functi = 0; int digit = number % 10; while (number > 0) { // int digit = number % 10; array[functi] = digit; functi++; // cout << digit << '\n'; number /= 10; } return digit; } void explode(int number) { int ctrack = 0; if (number <= 0) return; int digit = number % 10; explode(number/10); while(ctrack < 1) { cout << digit; ctrack++; cout << ","; } } int main() { int numdigits[100]; int n; cout << "Enter number: "; cin >> n; // int digit = explode(n,numdigits[]); cout<<"["; explode(n); cout<<"]\n"; }