Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   double base representation of a number (http://www.go4expert.com/forums/double-base-representation-t17725/)

ikj 28May2009 10:33

double base representation of a number
 
i want to print the double base representation of a number
ie if my number is 38 i shud get 36 + 2 and for 655 i shud get 648 + 6 + 1.
here's my code:

Code:


unsigned long bs(unsigned long a[],unsigned long d, unsigned long beg, unsigned long end, unsigned long x)
{   
    int mid;   
    mid=(beg+end)/2;   
    if (x<a[mid])   
    {       
              end=mid-1;       
              bs(a,d,beg,end,x);   
    }   
    else if(x<a[mid])   
    {       
              beg=mid+1;       
              bs(a,d,beg,end,x);   
    }
    else if(x>a[end])
    {    cout<<a[end]<<" + "; 
          return a[end]; 
    }
    else if(x==a[mid])
    { 
          cout<<"FOUND!!\n";
          return a[mid];
    }
}
void alg(unsigned long a[],unsigned long d, unsigned long x)
{
    unsigned long num;
    num=(x-bs(a,d,0,30,x));
    if (num>0)
        alg(a,d,num);
 
}
main()
{
      int c,n,d;
      d=c=(unsigned long)(pow(32,2));
      unsigned long a[d],b[c],t,x,y;
      d=c=0;
      a[0]=0;
      b[0]=0;
      for(int i=0;i<32;i++)
      {
          for(int j=0;j<32;j++)
          {
              b[c]=(unsigned long)(pow(2,i)*pow(3,j));
              if(j==0 || ULONG_MAX/pow(3,j)>pow(2,i))
              {
                a[++d]=b[c++];
                // cout<<i<<"\t"<<j<<"\t"<<a[d]<<endl;
              }
              else
              {
                  break;
              }
          }
      }
    sort(a,a+d);
    cout<<"enter element:";
    cin>>x;
    cout<<"its representation is :"<<endl;
    alg(a,d,x);
    system("PAUSE");
}

wat im doing in my code for alg() is im finding the element in the array using bs()
if the elemnt i want is in the array im printing that otherwise im finding the element that is closest (from the lesser side) to it and finding the difference ie (x - closest number to x) and calling alg for the difference and so on.
ie for 655 the closest elemnt is 648 and 655-648 is 7 , calling alg with 7 gives the closest elemnt as 6 n 7-6 is 1 n so on..
in my bs() code im returning the elemnt or the one which is closest to it. but somehow its not printing that elemnt n neither is it returning it.


please help...
thanks for reading!

shabbir 28May2009 11:42

Re: double base representation of a number
 
Try giving better titles as that helps others to see it as well. I have edited for you as of now.

ikj 28May2009 11:49

Re: double base representation of a number
 
sorry bout that..
thanks

SaswatPadhi 28May2009 13:24

Re: double base representation of a number
 
I hate to say this, but ijk, I really need to inspect your program yourself before dumping it in a forum. :mad: :mad:
Yeah, dumping is a harsh word to use, but look at your code. What do you think you are doing ! There are so damn many many mistakes. It was really really painful to find and correct the mistakes. It would have been much easier for me to write a program for double base representation, by myself.

( This is not an article, but don't blame me if it's as long as an article; 'cuz I have to highlight the mistakes )


Mistakes :

Code:

unsigned long bs(unsigned long a[],unsigned long d, unsigned long beg, unsigned long end, unsigned long x)
{    // WHY DOES THIS FUNC ACCEPT 'd' ??
    int mid;   
    mid=(beg+end)/2;   
    if (x<a[mid])   
    {       
              end=mid-1;       
              bs(a,d,beg,end,x);   
    }   
    else if(x<a[mid])    //same as previous case !!!!!!!!!
    {       
              beg=mid+1;       
              bs(a,d,beg,end,x);   
    }
    else if(x>a[end])                        // This should be the 2nd IF-check.
    {    cout<<a[end]<<" + "; 
          return a[end]; 
    }
    else if(x==a[mid])                      // This should be the first IF-check.
    { 
          cout<<"FOUND!!\n";
          return a[mid];
    }
}

void alg(unsigned long a[],unsigned long d, unsigned long x)
{
    unsigned long num;
    num=(x-bs(a,d,0,30,x));
    if (num>0)
        alg(a,d,num);
 
}

int main()
{    // main func was missing data-type !
      int c,n,d;
      // 32^2 does not need unsigned long, unsigned int will do, array index should be int not long.
      d=c=(unsigned long)(pow(32,2));
      // array b was ABSOLUTELY un-necessary !
      unsigned long a[d],b[c],t,x,y;
      d=c=0;
      a[0]=0;
      b[0]=0;
      for(int i=0;i<32;i++)
      {
          for(int j=0;j<32;j++)
          {
              b[c]=(unsigned long)(pow(2,i)*pow(3,j));
              if(j==0 || ULONG_MAX/pow(3,j)>pow(2,i))
              {
                a[++d]=b[c++];
                // cout<<i<<"\t"<<j<<"\t"<<a[d]<<endl;
              }
              else
              {
                  break;
              }
          }
      }
    sort(a,a+d);
    cout<<"enter element:";
    cin>>x;
    cout<<"its representation is :"<<endl;
    alg(a,d,x);
    system("PAUSE");
    return 0;      // no return !
}


Remarks :
(1) Your program is full of sh*t. You need to revise your C++ lessons.
(2) Your algo implementations are really poor. Try to implement better algo.


(:mad: Painfully) modified code:
Code: c++

#include <iostream>
#include <cmath>

using namespace std;

unsigned long bs(unsigned long a[], unsigned beg, unsigned end, unsigned long x)
{
    unsigned long mid;
    mid=(beg+end)/2;

    if (x==a[mid])
    {
        cout << a[mid];
        return a[mid];
    }
    else if (beg>end)
    {
        cout<<a[end]<<" + ";
        return a[end];
    }
    else if (x<a[mid])
    {
        end=mid-1;
        return bs(a,beg,end,x);
    }
    else if (x>a[mid])
    {
        beg=mid+1;
        return bs(a,beg,end,x);
    }
}

void alg(unsigned long a[],unsigned d, unsigned long x)
{
    unsigned long num;
    num=x-bs(a,0,d,x);
    if (num>0)
        alg(a,d,num);

}

const unsigned SIZE = 144;
unsigned long a[SIZE],t,x,y;

int main()
{
    int c,n,d;
    d=c=0;
    a[0]=0;
    for (int i=0;i<12;i++)
        for (int j=0;j<12;j++)
            a[12*i+j]=(unsigned long)(pow(2.0,i)*pow(3.0,j));

    sort(a,a+SIZE);
    cout<<"Enter element : ";
    cin>>x;
    cout<<"Its representation is : "<<endl;
    alg(a,SIZE-1,x);
    putchar(10);
    putchar(10);
    system("PAUSE");
    return 0;
}



Sample run :
Enter element : 655
Its representation is :
648 + 6 + 1

Enter element : 36
Its representation is :
36

Enter element : 38
Its representation is :
36 + 2

Enter element : 444
Its representation is :
432 + 12

Enter element : 555
Its representation is :
512 + 36 + 6 + 1

Enter element : 23743
Its representation is :
23328 + 384 + 27 + 4

Enter element : 25653
Its representation is :
23328 + 2304 + 18 + 3

Enter element : 1
Its representation is :
1

Enter element : 0
Its representation is :
0

I repeat again, Please inspect your program yourself before throwing it into a forum.


All times are GMT +5.5. The time now is 22:37.