7 Replies - 874 Views - Last Post: 01 February 2009 - 05:50 PM Rate Topic: -----

#1 fav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 09

Palindrome help

Posted 01 February 2009 - 04:56 PM

I've fixed it thanks!

This post has been edited by fav: 01 February 2009 - 05:37 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome help

#2 Lillefix   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Palindrome help

Posted 01 February 2009 - 05:08 PM

Because this is a task were you would benefit greatly from using a function or to, you should take a look at these functions I had lying around. See if you can get some inspiration, but for learning I would strongly advise not to copy and paste them.
#include <iostream>
#include <math.h>
#include <vector>

using namespace::std;

vector<int> split(int num)
{
	vector<int> temp;
	for (int i = int(pow(10, int(log10(num)))); i >= 1; i /= 10)
	{
		int numb = num / i;
		num -= numb * i;
		temp.push_back(numb);
	}
	return temp;
}

int reverse(int num)
{
	vector<int> temp = split(num);
	int sum = 0;
	for (int i = temp.size() - 1; i >= 0; i--)
	{
		sum += temp[i] * int(pow(10, i));
	}
	return sum;
}

int main()
{
	vector<int> print;
	int num = 13123234;
	print = split(num);

	cout << "Number:\t\t" << num << endl;
	cout << "Reversed:\t" << reverse(13123234) << endl;
	cout << "Split:\t\t";

	for (int i = 0; i < print.size(); i++)
		cout << print[i] << " ";

		cout << endl;

	return 0;
}

This post has been edited by Lillefix: 01 February 2009 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 fav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 09

Re: Palindrome help

Posted 01 February 2009 - 05:13 PM

Thanks for trying to help, but I cannot use your code because it is too advanced. We have not yet learned most of the words used in your code, we've only had class for 3 weeks now. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#4 Lillefix   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Palindrome help

Posted 01 February 2009 - 05:25 PM

Oh, ok, in that case I would first try to what you want for a three digit number and then when you have managed that, you can move onwards the other combinations aswell. This is often a good aproach in programming.
Was This Post Helpful? 0
  • +
  • -

#5 Greltam   User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Palindrome help

Posted 01 February 2009 - 05:26 PM

Heh. Are you limited to If statements? I wrote some basic code that will get you how many digits the number has and places them into an array you can use to compare the numbers
//import statements

int HowManyDigits(int y);

int _tmain(int argc, _TCHAR* argv[])
{
	int arrayX[6];
	int x;
	cout << "Enter integer to check for palindrome" << endl;
	cin >> x;
	int numOfDigits = HowManyDigits(x);
	for(int i = 0; i < numOfDigits; i++)
	{
		arrayX[i] = x % 10;
		x /= 10;
	}
	
}

int HowManyDigits(int y)
{
	int numDigits = 0;
	while(y != 0)
	{
		numDigits++;
		y /= 10;
	}
	return numDigits;
}



Was This Post Helpful? 0
  • +
  • -

#6 Lillefix   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Palindrome help

Posted 01 February 2009 - 05:33 PM

As to calculating the digits of a number you can simply use 'int(log10(num))', but your way is also acceptable (if not even faster?) if you don't want to include <math.h> for some reason.

To OP, excactly how does your if-statements fail? Does the compiler give you any errors or are your logic behind them wrong?

This post has been edited by Lillefix: 01 February 2009 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 fav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 09

Re: Palindrome help

Posted 01 February 2009 - 05:33 PM

Yes, I am limited to IF statements. Thanks for your help though, both of you. I have it working for all digit non-palindrome answers now, I just have to tweak it a little more to get it to work for palindromes of 4, 5, and 6 digits LOL. Thanks a bunch!
Was This Post Helpful? 0
  • +
  • -

#8 Greltam   User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Palindrome help

Posted 01 February 2009 - 05:50 PM

To be limited to IF statements is quite a handicap.. why would your teacher require such a silly impediment? :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1