int number = 0;cin >> number;int arrCount = number;char arr[arrCount] = {0}; //I believe you can initialize an array this way...while(number){ int i = 0; arr = number % 10; number = number / 10; // 1 / 10 = 0 ... does it not? ++i;}for (int i = arrCount - 1; i > -1; --i){ cout << arr << ' ';}
Breaking a three-digit integer. Is this a good way?
I'm just correcting what I had before.
Quote:Original post by Alpha_ProgDesWell, three things here.
int arrCount = number;
char arr[arrCount] = {0}; //I believe you can initialize an array this way...
First off, Variable Length Arrays (VLA's) aren't supported by the C++ standard - they are however defined for C99, so if your compiler is a C/C++ compiler it might not complain.
Secondly, that initialisation will only initialise the first element of the array. Integer types in an array all get default constructed to zero anyway.
Thirdly, you're allocating an awful lot of extra storage here; consider if the user typed the number 123456, then your array will be allocated to a size of 123456 even though all you really needed was enough space to store just 6 digits.
Quote:number = number / 10; // 1 / 10 = 0 ... does it not?For integer arithmetic, indeed it does.
int howmanydigits(int number){ int i = 0; while (number) { number = number / 10; ++i; } return i;}int pow10(int exponent){ int result = 1; for (int i = 1; i < exponent; ++i) { result *= 10; }}cin >> number;int numCount = howmanydigits(number);int start = pow10(numCount);int end = 10;bool isPalndrm = true;while (isPalndrm){ if((number / start) == (number % end)) { number = (number - ((number / start) * start)) / end; start /= 100; if (start == end || start < end) { break; } } else { isPalndrm = false; }}isPalndrm : cout << "Yeah, a palindrome"; : cout << "Oops, not a palindrome";
My simple solution to palindrome problem working with numbers:
theres probably a better way to do it :D
theres probably a better way to do it :D
bool isPal (int number){ // check for single digit or zero if (number / 10 == 0) return true; // keep track of the highest power of 10 int power = 1; // get the first digit int first = number; while (first / 10 != 0) { first /= 10; power *= 10; } // get the last digit int last = number % 10; // obtain the inner number, eg 12321 - 10000 / 10 = 232 int innerNumber = (number - (power * first)) / 10; // recursively check each first and last pair return (first == last) && isPal(innerNumber);}
@Alpha_ProgDes: I see you are trying a new approach. Just to let you know, the error I meant in your original code (and the second code listing) is that you are declaring i inside the loop, so it will be reset to 0 on every iteration.
Also, know that this time, unlike the previous exercise, it is possible to do the whole thing in one loop, without arrays or helper functions. The solution is in fact quite short.
Also, know that this time, unlike the previous exercise, it is possible to do the whole thing in one loop, without arrays or helper functions. The solution is in fact quite short.
I believe I saw a radix + recursion solution (palindrome) and a recursion solution (int breakdown) that must have been 5 lines at most. Unfortunately, I rarely think in terms of recursion, well explicitly anyway. I'm not sure if you took a look at my palindrome solution. But I'm sure you could improve on that as well.
I think it works now:
[source lang="cpp]#include <iostream>int main(){ int num, num2, i = 0; std::cout << "Enter a number: "; std::cin >> num; num2 = num; bool pal = false; do { if( i == 0 ) { i = 1; do { i *= 10; } while( num / i != 0 ); i /= 10; } if( num / i % 10 == num2 % 10 ) pal = true; else { pal = false; break; } num2 /= 10; i /= 10; } while( i != 0 ); if( pal ) std::cout << num << " is palindrom." << "\n"; else std::cout << num << " is not palindrom." << "\n"; system("PAUSE"); return 0;}
@sheep19: Note that the inner loop executes only on the first iteration of the outer loop. This means that it doesn't need to be an inner loop, it can be a separate loop that executes before the outer loop.
Also, there's no need to set pal to true inside the loop.
Finally, know that there is a simpler solution.
@Alpha_ProgDes: You could use recursion, but there's a very simple solution that doesn't need it.
Basically, think about a really simple and trivial way to check is a number if a palindrome or not.
Also, there's no need to set pal to true inside the loop.
Finally, know that there is a simpler solution.
@Alpha_ProgDes: You could use recursion, but there's a very simple solution that doesn't need it.
Basically, think about a really simple and trivial way to check is a number if a palindrome or not.
int howmanydigits(int number){ int i = 0; while (number) { number = number / 10; ++i; } return i;}int pow10(int exponent){ int result = 1; for (int i = 1; i < exponent; ++i) { result *= 10; }}cin >> number;int numCount = howmanydigits(number);int start = pow10(numCount);int end = 10;int checkNumber = 0;bool isPalndrm = false;while (start >= end){ checkNumber = checkNumber + (start * (number % radix)); number = number / end; start = start / end;}(checkNumber == number) ? cout << "Is Palindrome!"; : cout << "Is Not Palindrome";
I have to admit I got some help dmatter's solution.
If it's any simpler than that, please Great One, teach me!
Quote:Original post by Alpha_ProgDes
I have to admit I got some help dmatter's solution.
If it's any simpler than that, please Great One, teach me!
Well actually dmatter's solution is what I had in mind - just reverse the number and check if it's equal to the original. This can be done using one loop with no extra functions like log10() and without counting the digits (so, exactly like dmatter did it).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement