Tuesday, July 28, 2009

C++ question?

What is the out put of the following code?


input 38 45 71 4 -1


cin %26gt;%26gt; sum


cin %26gt;%26gt; num


for ( j=1; j %26lt;= 3; j++)


{


cin %26gt;%26gt; num;


sum = sum +num


}


cout %26lt;%26lt; " sum = " %26lt;%26lt; sum %26lt;%26lt; endl;





Im getting stuck here because of the consecutive cin's uptop we have a cin the after the for sstatement we have a cin. Any ideas or suggestions?

C++ question?
Assuming you've fixed the syntax errors, the output would be:





sum = 112





Assuming sum and num are defined as numeric types.





The first two cin's place 38 into sum and 45 into num.


Then, within the loop, num gets assigned and is added to sum for 71, 4, and -1. The result is 112.
Reply:for ur convinience just swap the osition of cin%26gt;%26gt;num; and sum=sum+num;


and dont forget ; after the statment


then ur all nums will be addressed/used
Reply:the code is ok, without the ; at the end of statement and some var declaration.
Reply:I TOTALLY do not understand that, maybe because I'm still in fifth grade XD


No comments:

Post a Comment