Hello, Another exercise. In this particular problem we are given a five digit integer and program have to determine if its a Palindrome. Author states that we have to separate each digit into its own variables using % and /. I came up with solution but i don't know if its acceptable. It produces correct result but it looks bad to me. Here's my code, can someone look over it and give me any suggestions what i should have done instead or how i can improve it. Also author only had five digit numbers as examples.
Does your book mention functions and use strings yet? Personally I've done this problem before and have made a solution that can solve any number (as long as it fits into an integer).
Your program relies on the fact that you have a 5 digit number and you made variables for each one. One change would be to allow numbers smaller and larger than 5 digits using a vector.
#include <vector>
#include <iostream>
int main()
{
int original = 12312;
int tempDigit;
std::vector<short> digits; // a variable length array of digits of the number;
bool isPalindrome = true;
if (original == 0) digits.push_back(0); // Supplied case because I use 0 as a stop in the while loop below.
while (original != 0) // While original != 0 (Assuming positive number!)
{
tempDigit = original % 10; // Get the digit on the right.
original /= 10; // Make the number smaller by one digit.
digits.push_back (tempDigit); // add tempDigit to vector.
// Repeat until number becomes 0;
}
// Now this puts out value in reverse (123 is stored in the vector as 321) so we need to
// reverse our vector using its constructor!
digits = std::vector<short>(digits.rbegin(), digits.rend());
// rbegin() and rend() are iterators.
// Now we can begin our check! You can use a for loop!
for (int i = 0, j = digits.size() - 1; i < j; ++i, --j)
if (digits[i] != digits[j]) isPalindrome = false;
if (isPalindrome) std::cout << "Correct" << std::endl;
else std::cout << "Failure" << std::endl;
return 0;
}
Unfortunately with that limit on knowledge for the chapter you need to do heavy editing each time to the code to handle different sized numbers beyond 5 digits. But if this is any consolation I took your code and changed what may help you think it looks better. You can see I used a while loop instead of a for loop and only use 10 as a divisor the entire loop.
#include <iostream>
usingnamespace std;
int main()
{
int iWhole = 15451,
iNum1 = 0,
iNum2 = 0,
iNum3 = 0,
iNum4 = 0,
iNum5 = 0,
iRemainder;
if (iWhole == 0) cout << "Error" << endl; // Case for later.
int i = 4; // This is for your statements below.
while (iWhole != 0) // Use a while loop instead.
{
iRemainder = iWhole % 10;
iWhole /= 10; // Now I only need to divide and mod by 10.
// This process is now done in reverse.
// (iNum5 is replaced then 4 and so on.)
if ( i == 0 )
iNum1 = iRemainder;
elseif ( i == 1 )
iNum2 = iRemainder;
elseif ( i == 2)
iNum3 = iRemainder;
elseif ( i == 3 )
iNum4 = iRemainder;
else
iNum5 = iRemainder;
--i;
}
if ( iNum1 == iNum5 && iNum2 == iNum4 )
cout << "Its a palindrome" << endl;
else
cout << "Its not a pslindrome" << endl;
return 0;
}
int size = log10(number)+1;//gets number of digits for the size of the array
int digits[size];
for (int i = 0; i < size; i++)// each iteration of the loop gets another digit.
{
digits[i] = number%10;
number /= 10;
}
It is then easy to test the array as a palindrome. Just include cmath for the log10() function.
Edit: If you're turning this in, I would fix you're grammer and spelling on lines 43 and 45
Thanks for the input , but arrays are not covered therefore violate the requirement. Wolfgagng thanks it is better with a while loop dont know why i didnt think off of it earlier. Theres another simple method just to reverse the number but that would to violate the requirement because it would not separate each digit into its own variable, I know this is not practical ways of getting results but have to do what given problem asks.