Number Anagrams

closed account (1wvX92yv)
hello everybody,
i was recently working on a problem that required me to find a number and its double that have the same digits(obviously rearranged).....this is what i came up with...

#include <iostream>
#include<stdio.h>

using namespace std;

int digit(long num1,long num2)
{
// int num1=167985432,num2=987654321;
long ray1[10],ray2[10];
int n=num1,m=num2,i,j;

i=0;
while(n)
{
ray1[i]=n%10;
n=n/10;
++i;
}

j=0;
while(m)
{
ray2[j]=m%10;
m=m/10;
++j;
}

int flag=0;

if(i==j)
{

for(int a=0;a<=i;++a)
{
for(int b=0;b<=i;b++)
{
if(ray1[a]==ray2[b])
{
flag++;
// for(int c=b;ray2[c]!='\0';c++)
{
// ray2[c]=ray2[c+1];
}
break;
}
}
}



}

if(flag==i)
{
cout<<"Success....."<<endl;
return 1;
}
else
{
//cout<<"Failure....."<<endl;
return 0;
}



}


int main()
{
cout << "Hello world!" << endl;

for(long i=1;;++i)
{
cout<<i<<endl;
int j=i*2;
if(digit(i,j))
{
cout<<"Yipeeeeeeeeeeeeeeee..........";
break;
}
}

/* int t1,t2;
cin>>t1>>t2;
if(digit(t1,t2)==1)
cout<<"Success.........double time";

*/
return 0;
}


so here is the problem, the problem works for all numbers that don't contain a zero, a number with is somehow mistreated....
ex.,the prgm outputs 1255 as a viable number, but 1255*2=2510 ,having a zero, does not have the same digits as the original number.........

any thoughts would be appreciated.......been breaking my head for long....
You have a problem, your program only checks if both numbers contain same digits and numbers could contain multiple same digits, so your program would say that 1255 and 1522 contain same digits in different order, which is not true.
You have to fix that first. (If you don't know how to solve this I could tell you)

And your program doesn't work with zeros because it doesn't work with any number, your conversion loop doesn't get the right digits of the number.
If you used a string instead of long int array (idk why did you used long since digits are only 0-9) you could use atoi function to get the digits as array of characters.
Last edited on
closed account (1wvX92yv)
Hello zoran404,
I figured as much, but still can't see where I am going wrong,
Do I have to cancel the digits in the second array once they match? I have tried that, but got the same result.......

Please tell me how to go about this, even the logic would do, you don't have to send a code.

And thank you very much for replying.... :-)......
That is exactly what I though of, remove the digit from the second array once you have matched them.

But now I got an even better idea. Instead of searching to match all digits, you can just sort the arrays and if they are identical then they have the same digits. Can't be simpler than that.

And I told you to use strings instead of arrays of type long and to use itoa to convert an integer to a string because your method for making the digit arrays doesn't work right.
I'm talking about this:
1
2
3
4
5
6
7
i=0;
while(n)
{
ray1[i]=n%10;
n=n/10;
++i;
}



If you still have problems then post your new code.
Last edited on
closed account (1wvX92yv)
Hello zoran404,

Thanks, i used your idea and just implemented a code that checks the digits and keeps count of how many of each digit appeared........ It worked beautifully.

But I did not understand what you told about itoa......... I have never come across it,

Do you mind telling me what exactly it is? Maybe some link...... (i am still a student, been programming since a year.....)
If you don't know what itoa is then just type itoa in the search field at the top of this page.

And post your code so I would see what you have done. thanks
Topic archived. No new replies allowed.