Thursday, July 30, 2009

TURBO C question?

Ok here's a program of turbo c.The output of this program is RESULT=95.


The question is how?I just can't get it.











#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


int change(int[]);


main()


{


int a,arr[8]={5,19,25,6,35,8,45,50...


a=change(arr);


printf("RESULT=%d",a);


getch();


}


int change(int a[])


{


int limit=0,i=0,sum=0;


while(!limit)


{


sum+=a[i];


if(sum%26gt;90)


limit++;


}


return(sum);


}











The thing is when I calculate on my own the answer comes 98 which is obviously not matching to 95.Secondly what does limit++ do here?

TURBO C question?
The limit here works as a flag to when te loop While should stop :





while(!limit)





/**





this will only stop if limit is not 0 ... so the code will keep on being executed non-stop till limit changes its initial state(0).





**/








if(sum%26gt;90)


limit++;





/**





what makes the loop stops is this part of the code where the total sum is asked if its value is over 90. Remember that 90 is NOT greather then 90. So this wraps as to what the limit does on the code. simple flag.





**/





The reason why your total sum is 95 and not 98 ...





sum+=a[i];





/**





as you can notice here your index i never changes its value so all you add is the first value from the array a.





your loop is doing 5+5+...+5 till you get to the point where sum is 90, then 95, limit is incremented, the loop is terminated and the sum value is returned ... (95).





**/





by what i understand you are trying to sum the values of the array and not only the first one a easy fix would be to increment i.





sum+=a[i++];





After fixing that your sum will total 98 just like you calculated.





...
Reply:now consider


limit=0


so while(!limit)/*is infinite


like while(!0) condition is true now controll will be in loop till then limit is not equal to 1 (limit==1)





sum+=a[i] // always sum=sum+5 so on the when the loop will have excute 18 time then sum==90 but


here ..


sum%26gt;90 is false now on the 19'th time sum will be 95 then


if(sum%26gt;90) is true and now limit will be 1


limi++; //the value of limil is 1


and wihile( !1 ) like as so loop condition is false and now you out of loop











so while() loo is depend on the limit


means limit crtol the loop excution











bye ..


tc
Reply:There's no "i++" in the loop. The loop is adding 5 to sum until the sum is greater than 90, so the first multiple of 5 greater than 90 is 95.
Reply:Boss the expected answer is right. Initially the value of limit is 0.So !limit =1.So while condition is true.so sum=0+5.But this is less than 90.So it doesnot enter the if condition which is (limit=limit+1).So it keeps on adding 5 to the sum untill it(sum value) reaches 95 and the it comes inside the IF condition and then increments the limit value.Now the value of limit is 1 the !limit is 0 so the while condition fails.And 95 is returned as the sum.
Reply:I think u r question is wrong . Once again u compile it.





Here initial value of limit is 0. so when we give condition while(!limit) it takes negation limit value i.e. 1 or true.





if sum becomes %26gt; than 90, limit will increment to 1.


so at the condition it's negation will be 0 and condition becomes false

garden flowers

No comments:

Post a Comment