Tuesday, July 28, 2009

Need an answer within 6 hours! C++ program - find the limitation of the following program?

char *func(char *buf){


if(buf!=NULL){


int len=strlen(buf);


for(int i=0 ; j=len-1 ; i%26lt;len/2; i++ ; j--){


char t=buf[i];


buf[i]=buf[j];


buf[j]=t;


}}


return buf;


}








We are supposed to find the limitation %26amp; propose a solution. The limitation has nothing to do with syntax.








Another program:





void function(int a[]){


int i, j, min;


for(i=0 ; i%26lt;99; i++){


min=i;


for(j=i+1 ; j%26lt;100; j++){


if (a[j]%26lt;a[min]){


min=j;


}}


swap(a[i], a[min]);


}}





What's the most critical limitation of this function %26amp; propose a solution.

Need an answer within 6 hours! C++ program - find the limitation of the following program?
For the first piece i starts from 0 instead of 1, so you can say i%26lt;(len-1)/2 instead of i%26lt;len/2, this will reduce the number of iterations , why dont you just use the reverse string function instead of this , another solution is to use 2 arrays and then transfer each char from buf1 to buf2 in reverse order and set the pointer.





in the second piece should it be j%26lt;99 instead of j%26lt;100


and the swapping you need to do only if i is not equal to min
Reply:The above answer is not 100% correct, so either try it yourself or else you may contact a C++ expert live at website like http://askexpert.info/ .


No comments:

Post a Comment