Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

I did my best But......

[b]Here is a simple problem[/b] from a [italic][b]Microsoft[/b][/italic] [b]code challenge[/b] :
simply built a function to return palindromes between two numbers(0<=Number<=10000),exactly function declaration as :
int *getPalindromicNumbers(int from, int to)

I developed the code from [b]Turbo c v3 @ windows xp[/b] environment.The code was working properly. And I edited it to work in Unix C (as guided by Microsoft{Put dsmain() instd main(),Use new tch such as<stdbool.h>,etc.. ). Because their server is Unix. But the code did not show any result on Unix Server. And Unfortunately I lost the point.


What was the problem with me ?
Please help me ?
The code is give Below:-



The format of code updated with comments for more Readable


[code]



#include
#include
#include

//simple Function to check wheather boolean or not
bool isPal(int num)
{
int tmp,r,s=0;
tmp=num;

while(num!=0)
{
r=num%10 ;
num=num/10;
s=s*10+r;
}

if(tmp==s) return true;
else return false;
}


//Function required from [italic][b]Microsoft[/b][/italic]
int *getPalindromicNumbers(int from, int to)
{

/*
The logic I used is make continous memory heaps to load each number

palPointer is first pointer of array
tmpPointer is pointer to load number in each position

*/

int i,*tmpPointer,*PalPointer,tstPas[100];

// if range is wrong simply return null pointer
if (from<0||to>10000) return NULL;

//allocating requared memory
tmpPointer =(int*)calloc((to-from),sizeof(int));

//specifying first pointer to preserve
PalPointer=tmpPointer;
//allocating palindrome numbers to each position
for(i=from;i<=to;i++) if (isPal(i)) *tmpPointer++=i;
*tmpPointer++=(int)'';

return PalPointer ;
}


// this function is used for checking purpose only
void dsmain()
{
int *cp=getPalindromicNumbers(1,100);
printf("Palindrum Numbers Between 1 and 100 are :");
while(*cp) printf("%p : %d
",(void*)cp,*cp++);
}

[/code]
[italic][b]SibGeth[/b][/italic]
Searching for a peer

Comments

  • LundinLundin Member Posts: 3,711
    I have no idea why it doesn't work in unix: if you want anyone to have a look at that code, you will need to do some code formatting. Is it is now, the code is unreadable. And the program contains severe memory leaks as well.
  • IDKIDK Member Posts: 1,784
    Put some print statements in it and see what happens...
  • nugentnugent Member Posts: 87
    : Put some print statements in it and see what happens...
    :
    seems to work, the unix compiler (gcc) requires a main() function

    [code]
    #include
    #include
    #include

    //simple Function to check wheather boolean or not
    bool isPal(int num)
    {
    int tmp,r,s=0;
    tmp=num;

    while(num!=0)
    {
    r = num%10 ;
    num = num/10;
    s = s*10+r;
    }

    if(tmp == s)
    return true;
    else
    return false;
    }

    //Function required from Microsoft
    int *getPalindromicNumbers(int from, int to)
    {
    /*
    The logic I used is make continous memory heaps to load each number
    palPointer is first pointer of array tmpPointer is pointer to load number in each position
    */

    int i, *tmpPointer, *PalPointer, tstPas[100];

    // if range is wrong simply return null pointer
    if (from<0||to>10000)
    return NULL;

    //allocating requared memory
    tmpPointer = (int*)calloc((to-from),sizeof(int));

    //specifying first pointer to preserve
    PalPointer = tmpPointer;
    //allocating palindrome numbers to each position
    for(i = from; i <= to; i++)
    if(isPal(i))
    *tmpPointer++=i;
    *tmpPointer++=(int)'';

    return PalPointer ;
    }

    // this function is used for checking purpose only
    void dsmain()
    {
    int *cp = getPalindromicNumbers(1,1000);
    printf("Palindrum Numbers Between 1 and 1000 are :");
    while(*cp)
    printf("%p : %d
    ",(void*)cp,*cp++);
    }

    int main()
    {
    dsmain();
    return 0;
    }

    [/code]

    prints (I changed 100 to 1000 from the code previously presented)
    [code]
    Palindrum Numbers Between 1 and 1000 are :0x601014 : 1
    0x601018 : 2
    0x60101c : 3
    0x601020 : 4
    0x601024 : 5
    0x601028 : 6
    0x60102c : 7
    0x601030 : 8
    0x601034 : 9
    0x601038 : 11
    0x60103c : 22
    0x601040 : 33
    0x601044 : 44
    0x601048 : 55
    0x60104c : 66
    0x601050 : 77
    0x601054 : 88
    0x601058 : 99
    0x60105c : 101
    0x601060 : 111
    0x601064 : 121
    0x601068 : 131
    0x60106c : 141
    0x601070 : 151
    0x601074 : 161
    0x601078 : 171
    0x60107c : 181
    0x601080 : 191
    0x601084 : 202
    0x601088 : 212
    0x60108c : 222
    0x601090 : 232
    0x601094 : 242
    0x601098 : 252
    0x60109c : 262
    0x6010a0 : 272
    0x6010a4 : 282
    0x6010a8 : 292
    0x6010ac : 303
    0x6010b0 : 313
    0x6010b4 : 323
    0x6010b8 : 333
    0x6010bc : 343
    0x6010c0 : 353
    0x6010c4 : 363
    0x6010c8 : 373
    0x6010cc : 383
    0x6010d0 : 393
    0x6010d4 : 404
    0x6010d8 : 414
    0x6010dc : 424
    0x6010e0 : 434
    0x6010e4 : 444
    0x6010e8 : 454
    0x6010ec : 464
    0x6010f0 : 474
    0x6010f4 : 484
    0x6010f8 : 494
    0x6010fc : 505
    0x601100 : 515
    0x601104 : 525
    0x601108 : 535
    0x60110c : 545
    0x601110 : 555
    0x601114 : 565
    0x601118 : 575
    0x60111c : 585
    0x601120 : 595
    0x601124 : 606
    0x601128 : 616
    0x60112c : 626
    0x601130 : 636
    0x601134 : 646
    0x601138 : 656
    0x60113c : 666
    0x601140 : 676
    0x601144 : 686
    0x601148 : 696
    0x60114c : 707
    0x601150 : 717
    0x601154 : 727
    0x601158 : 737
    0x60115c : 747
    0x601160 : 757
    0x601164 : 767
    0x601168 : 777
    0x60116c : 787
    0x601170 : 797
    0x601174 : 808
    0x601178 : 818
    0x60117c : 828
    0x601180 : 838
    0x601184 : 848
    0x601188 : 858
    0x60118c : 868
    0x601190 : 878
    0x601194 : 888
    0x601198 : 898
    0x60119c : 909
    0x6011a0 : 919
    0x6011a4 : 929
    0x6011a8 : 939
    0x6011ac : 949
    0x6011b0 : 959
    0x6011b4 : 969
    0x6011b8 : 979
    0x6011bc : 989
    0x6011c0 : 999

    [/code]

    hope this helps



    ------
    nugent

Sign In or Register to comment.