C++ : Array of palindrome number
@Atalia Chez Took me a bit but I have fixed it
#include <iostream> using namespace std; void twoDigitChk(int); void threeDigitChk(int); void fourDigitChk(int); void fiveDigitChk(int); int main() { int counter=0; const short int SIZE= 5; int array[SIZE]; cout << "********************************************************** " << endl; cout << "Enter 20 numbers to check palindrome pattern" << endl; for(int i=0; i<SIZE; i++) { cout << "Enter: " << flush; do{ cin >> array[i]; if(array[i] < 10){cout << "Palindrome number can not have less than 2 digits! Enter again: " << flush;} if(array[i] >99999) {cout << "You can only enter a number having maximum of 5 digits! Enter again: " << flush;} }while( array[i]<10 || array[i]>99999); } for(int j=0; j<SIZE; j++) { counter= 0; int num= array[j]; while(num>0) { num/=10; ++counter; } switch(counter) { case 2: twoDigitChk(array[j]); break; case 3: threeDigitChk(array[j]); break; case 4: fourDigitChk(array[j]); break; case 5: fiveDigitChk(array[j]); } } return 0; } void twoDigitChk(int number) { float b= number%10; if(number/10 == b ) cout << "Palindrome" << endl; else cout << "Not Palindrome" << endl; } void threeDigitChk(int number) { float thirdDigit= number%10; float firstDigit= number/100; if(firstDigit==thirdDigit) cout << "Palindrome" << endl; else cout << "Not Palindrome" << endl; } void fourDigitChk(int number) { int lowerTWO = number%100; int higherTWO = number/100; float fourthDigit = lowerTWO%10; float thirdDigit = lowerTWO/10; float secondDigit = higherTWO%10; float firstDigit = higherTWO/10; if(firstDigit==fourthDigit && secondDigit==thirdDigit) cout << "Palindrome" << endl; else cout << "Not Palindrome" << endl; } void fiveDigitChk(int number) { int lowerTWO = number%100; int higherTWO = number/1000; float fifthDigit = lowerTWO%10; float fourthDigit = lowerTWO/10; float secondDigit = higherTWO%10; float firstDigit = higherTWO/10; if(firstDigit==fifthDigit && secondDigit==fourthDigit) cout << "Palindrome" << endl; else cout << "Not Palindrome" << endl; }
The problem was that switch statement was after checking the number of digits in all 5 entered numbers, so it would only call function that checks as many digits that were in the last entered number.
Second was that you passed the entire array for the function to check, and you every function went through the whole array. This is not correct.
I changed it so that every function only has one parameter and that is the current number from the array.
Third your functions only checked for a match of first an last digit of any length number (this works for 2 and 3 digit, but not for more) so even 12571 would return "Palindrome" which it isn't.
That's why four and five digit ones first get lower and higher digits and than check for a match of all four digits.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now