Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part II Look at the following segment of code. The numbers on the left are for i

ID: 3841214 • Letter: P

Question

Part II Look at the following segment of code. The numbers on the left are for identification ol lines only. All variables are ol type double. Answer the questions A-F follow the code I. unsigned long int Harpo(int num) int index 1; unsigned long int temp 1: while lindex num) temp temp Index index: 10 return temp: 12. in maino 13. int number -0; 14 unsigned long int agnes l; 15. while I) 16 cin number: il (number 19 brca 20 21 agnes Harpo ber) number end: cout The value agnes results from return 0; 21 25. A. What the ol line 12 is purpose B. Where does the while loop begin and end

Explanation / Answer

Hi,

Please find below the answers-

Ans A. while(1) indicates the condition "always true" . This loop is an infinite loop and it will run forever. All the statements that are written inside this loop will run forever.

True condition is represented by 1

False condition is represented by 0

Ans B. While loop beginning and termination can be marked by the opening and closing braces respectively. In this example the while loop opens at line no 17 and it ends at line no. 23

Ans C. The solution is quite similar to solution of A.'1' indicates the "Always true" condition for while loop while '0' indiactes the "Always false" condition. In line number 16, there is an always true condition defined due to which the program takes inputs from the user infinite times and calls the Harpo(number) function each time uses gives the input.

Ans D. Line 21 has a variable 'agnes' that store the value returned by the function call "Harpo(number". The assignment operator "=" is used to assgn the value of the function the variable.

Ans E. The "if" keyword acts as word that helps us defined the conditions in our program and we can control the flow of out program based on the output of the conditions defined. Line 19, instructs the machine to check a condition if the user given number is equal to '-99'. Line 20, states the instructions that computer needs to perform if the condition - number==-99 is met. Line 20 tells the computer that if the user given number matches with -99, then you can break the program. Break wil cause the program to com out of the loop(while loop) and starts the execution from the next line i.e. line number 24.

Ans F. The answer will be 120.

Reason- Harrp(5) will call the harpo function with 5 as parameter.

step 1 - In side the function, index=1, temp =1 and num=5

A condition is evaluated as while(index<=num) , this means while(1<=5) this condition is true, hence the control will go inside the loop and performs below instructions-

Assign temp*index ( 1*1) value to temp and increment the index value by 1

so new values are temp=1, index=2, num=5

step 2 - Now, its aginn checks the condition while(index<=num) or while(2<=5) which is true hence the next insructions will be executed like below-

temp = temp*index ( 1*2) and ++index means index=3, temp=2

step 3 - while(3<=5) gives true so proceed with below calculations again-

temp = temp*index ( 2*3) and ++index means index=4, temp=6

step 4 - while(4<=5) gives true so proceed with below calculations again-

temp = temp*index ( 6*4) and ++index means index=5, temp=24

step 5 - while(5<=5) gives true so proceed with below calculations again-

temp = temp*index ( 24*5) and ++index means index=6, temp=120

Now this value 120 is returned to the calling main function and it is stored into a variable agnes and printed in line 22.

Thanks and Regards,

Vinay Singh