Program that prints out all digits of an integer not working
Revised it, but still not working:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#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 << "]";
}
|
bump
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#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;
}
|
Can someone help? I really need help with this.
Topic archived. No new replies allowed.