Friday, July 31, 2009

Question for C programmers?

Hi


This was the question asked in Adobe written test


1 Write a program to know whether a number is divisible by 3 we cant use / ,% or * operators .Hint was


use itoa function





2 How to reverse a singly linked list via recursion


Any IT gurus there who want to try their hand





3 How to know how many bits are required to make 2 nos equal


I mean say 8 in binary notation is 1000 and 10 is 1010 so the answer is 1 so for any 2 nos A and B given in decimal form write a program





I m more interested in getting answers for first 2

Question for C programmers?
One of the property of a number divisible by 3 is that its sum of digits will be also divisible by 3.


Suppose abcd is a 4 digit decimal number





abdc = 1000a + 100b + 10c + d = (999a + 99b + 9c) + (a + b + c + d)





Here the





999a + 99b + 99c





is divisible by 3. So if





a + b + c + d





is also divisible by three then the number is divisible by three.





int mod3(int number)


{


char *iter;


char *str = itoa(number);


while( *(str+1) != '\0' ) {


number = 0; iter = str;


while( *iter != '\0' ) {


number += (*iter - '0');


iter++;


}


str = itoa(number);


}


if ( (*str == '3') || (*str == '6') || (*str == '9') )


return 0;


return 1;


}





Links to a site that have some reverse linked list algorithm


http://www.faqs.org/qa/qa-9150.html
Reply:answer 1.


this has very less efficiency. but works





while(num%26gt;0)


{


num=num-3;


}


if(num==0)


printf("Number is divisible by 3");


else


printf("Number is not divisible by 3");








answer 2.





struct node


{


int datal;


struct node *next;


};





void main()


{


struct node *head, dummy;


head = create(); // creating a linked list and


// assigning starting node to head





dummy = reverse(head,head);





}





struct node * reverse( struct node *p, struct node *head)


{


struct node *temp;


if(p-%26gt;next==NULL)


{


head = p;


return p;


}


temp=reverse(p-%26gt;next,head);


temp-%26gt;next = p;


p-%26gt;next = NULL;


return p;


}





answer 3:


Use xor operation on 2 operands.


then find out number of 1's in the number





//assuming number is of 32 bits





int a, b, res, i, sum,n=1;


scanf("%d%d",%26amp;a,%26amp;b);


res = a^b; // x-or operation





sum=0;


for(int i=0;i%26lt;32;i++)


{


if(res %26amp; n%26lt;%26lt;i)


sum++;


}





printf("The number of bits to be changed is %d", sum);
Reply:i only know the first one. I guess you'll have to use a for loop as follows


for( int i=x; i%26gt;=0; i-=3)





at the end of the loop, if i==0 then its divisable by 3 or else it isnt.
Reply:Answer 1.


/* without using itoa */





main(){ int i; scanf("%d",%26amp;i);


for(;i%26gt;=3;i-=3);


!i?printf("divisible")?printf(" not divisible");}





/* using itoa */





main(){ int i ,s=0; char a[30];


scanf("%d",%26amp;i);





/*add the digits till the summation is less than 10*/





do{ itoa(i,a,10);


for(i=0;a[i]!='\0';i++)


s=s+a[i]-'0';


i=s;s=0;


}while(i%26gt;=10);


itoa(i,a,10);


if((!strcmp("3",a) || (!strcmp("6",a) || (!strcmp("9",a))


printf("divisible..")


else printf("not divisible");


}
Reply:None of these questions are important. They are used in tests because they are small enough to allow an answer. The testers think that programmers who can give a neat sweet answer will be better than those who can't. I estimate that they will be right 20% of the time and wrong 80% of the time. Gurus who can rattle off a quick answer to these could be almost useless at producing commercially-viable user software, and vice versa.





It looks very reminiscent of the horrendous state of mathematics exams at Cambridge up until about 1914. The questions had degenerated into just icksy-tricksy things needing weird skills to answer, but it didn't produce mathematicians, and as a result English maths was DECADES behind Scotland and Europe, if not a century. It took Hardy and Littlewood to get the stupid system binned, and replaced with REAL mathematics. But I'm not a Hardy, and I can't see what to do about these clever-but-dumb programming tests.
Reply:1: Any numbers who's digits sum to a multiple of three is itself divisible by three.





2: Do a Google search for linked list algorithms - there is already plenty of information on this issue without me repeating it here.





3: Look into XOR...





Rawlyn.
Reply:The first one has already been answered.


The second could be answered as :





node * reverse(node * nd)


{


if(nd-%26gt;next==NULL)


return nd;


else


return (reverse(nd-%26gt;next)-%26gt;next=nd);


}








The third one is pretty simple.:


just take a XOR of the two no. and then count the no. of 1 in the result..


It is too simple and i am sleepy..so write the code yourself


No comments:

Post a Comment