Jump to content

C++ : Array of palindrome number

BigCake
Go to solution Solved by G27Racer_188,

@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.

Greetings LTT members! 

 

Unfortunately, I am stuck on the part that my program does not calls appropriate function. For example, if I enter two digit number at first then if I enter 3 digits  number, then the program ignores 2 digit number and moves on to 3 digit numbers and calls its function, Here is the code:

 

#include <iostream>
using namespace std;

void twoDigitChk(int [], int);
void threeDigitChk(int [] , int);
void fourDigitChk(int [], int);
void fiveDigitChk(int [], 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] >9999) {cout << "You can only enter a number having maximum of 5 digits! Enter again: " << flush;}
			       
	   }while( array[i]<10 || array[i]>9999);
	}
	
	
	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,SIZE); 
		break;
		case 3: threeDigitChk(array,SIZE);
		break;
		case 4: fourDigitChk(array,SIZE);
		break;
		case 5: fiveDigitChk(array,SIZE);
	}

/*	if(counter == 2)
	{
	 twoDigitChk(array,SIZE); 
	}
	else if(counter == 3) { threeDigitChk(array,SIZE); };
	else if(counter == 4) { fourDigitChk(array,SIZE); };
	else(counter == 5) {fiveDigitChk(array,SIZE); }*/
	
	return 0;
}

void twoDigitChk(int array[], int SIZE)
{
	for(int i=0; i<SIZE; i++)
	{
		float b= array[i]%10;
		if(array[i]/10 == b )
		cout << "Palindrome" << endl;
	else
	    cout << "Not Palindrome" << endl;
	}
}

void threeDigitChk(int array[], int SIZE)
{
	for(int i=0; i<SIZE; i++)
	{
		float b= array[i]%10;
		if(array[i]/100 == b )
		cout << "Palindrome" << endl;
	else
	    cout << "Not Palindrome" << endl;
	}
}

void fourDigitChk(int array[], int SIZE)
{
	for(int i=0; i<SIZE; i++)
	{
		float b= array[i]%10;
		if(array[i]/1000 == b )
		cout << "Palindrome" << endl;
	else
	    cout << "Not Palindrome" << endl;
	}

}

void fiveDigitChk(int array[], int SIZE)
{
	for(int i=0; i<SIZE; i++)
	{
		float b= array[i]%10;
		if(array[i]/1000 == b )
		cout << "Palindrome" << endl;
	else
	    cout << "Not Palindrome" << endl;
	}
}

Any help will be appreciated.

Link to comment
Share on other sites

Link to post
Share on other sites

50 minutes ago, Atalia Chez said:

 

Took me a while but it looks like it will only check the size of the last number for every number, so if you have 22, 22, 22, 22, 232 it will call threeDigitChk for all of them, which will output "Not Palindrome" for every one except the last. Maybe remove the for loop out of each of the functions and move the switch statement into the other for loop?

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], SIZE);
      break;
    case 3: threeDigitChk(array[j], SIZE);
      break;
    case 4: fourDigitChk(array[j], SIZE);
      break;
    case 5: fiveDigitChk(array[j], SIZE);
  }
}

 
Edited by yuh25

3700x, Asus B450i, 16GB Corsair Vengeance RGB, Gigabyte GTX 970 ITXDan A4-SFX, SX600-G, 120mm AIO.

Link to comment
Share on other sites

Link to post
Share on other sites

@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.

CPU: i7 3770K | MB: EVGA Z77 FTW | RAM: HyperX Savage 2400Mhz 16GB | GPU: R9 280X Toxic | Cooler: Scythe Fuma | PSU: CoolerMaster B600

SSD: Crucial MX300 525GB | HDD: Seagate Barracuda 7200.12 500GB - Toshiba DT01ACA300 3TB

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, G27Racer_188 said:

@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;
}

-snip-

Thank you very much! 

 

Before your answer I proceeded to fix the problem where the last entered digit`s function would work only. However, sometimes being stupid enough to let these common mistakes slip through, I did not even thought about 4 digit and 5 digit math.

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, Atalia Chez said:

<snip>

You need to think a little more outside the box to solve the problem in a much simpler way with less code and as a result less errors and head-scratching.

 

There's no need to read the number as an actual number, you can read it as a string, the user won't see any difference. This solves 2 main problems:

  • It's much simpler to solve the actual problem, you can just compare the string to a reversed copy of itself, if they match it's a palindrome. As an extra bonus you can now accept numbers of any length, so long as they fit in a std::string, which can be billions of digits. (Basically, memory is the only limit).
  • It removes the need for input stream error checking. In your code, if reading a integer from cin fails - because the user enters text in stead of numbers, for example - the stream state goes bad and you program will start failing because you never test and reset the stream state (try it!, enter some text in stead of numbers). By reading a string this problem goes away. The user can enter pretty much anything and the operation will succeed, we simply check if whatever was entered is valid later.

Example implementation:

#include <iostream>
#include <algorithm>
#include <cctype>
#include <array>
#include <string>

bool
IsValidNumber(const std::string& number)
{
	if (!std::all_of(number.begin(), number.end(), isdigit))
	{
		std::cout << "Not a valid number!\n";
		return false;
	}
	if (number.size() < 2)
	{
		std::cout << "Palindrome number needs at least 2 digits!\n";
		return false;
	}
	/* No need to check for a maximum length, this solution works with any number so long as it's 
	 * digits fit in a std::string, that's billions of digits on a typical PC. */

	return true;
}

void
ReadNumber(std::string& number)
{
	do 
	{
		std::cout << "Enter: ";
		std::cin >> number;
	}	while (!IsValidNumber(number));
}

bool
IsPalindrome(const std::string& number)
{
	std::string reverseNumber(number.rbegin(), number.rend());
	return number == reverseNumber;  
}

int main()
{
	constexpr int size = 5;
	std::array<std::string, size> numbers;
	std::cout << "Enter " << size << " numbers to check palindrome pattern\n";

	for (auto& number : numbers)
	{
		ReadNumber(number);
	}

	for (const auto& number : numbers)
	{
		std::cout << number << " : " << (IsPalindrome(number) ? "Palindrome" : "Not Palindrome") << '\n'; 
	}

	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×