top of page

Computing 2016-2017 Semester 2-Question 2

Below are comments and further explanation on question 2.

Question 2.a)i)

A quick way to tell the answer is TRUE is that n on the right hand side of || equals 7.3, a non-zero value. And in C, a non-zero value is seen as TRUE while zero is seen as FALSE. Since the logical operator OR will return TRUE if any one side of the operand is TRUE, and we know that the right hand side is TRUE, therefore, we know that this expression will return TRUE.

If you are interested, the left hand side of the OR operand gives FALSE. It is because p is -7 and -p gives 7 which is EQUAL to q - 4 (11 - 4 = 7).

Question2.a)ii)

Similarly, if we were to look at the right hand side of this expression (p < -9), we know that it evaluates to FALSE since -7 is more than -9, not the other way around. Since the logical operator AND will return FALSE if any one side of the operand is FALSE, the expression will evaluate to FALSE.

Just a side note on calculating the left hand side (n > r / q). We first calculate r / q, which is 77 / 11 = 7 ('M' = 77 according to the ASCII table), please note that this is a form of integer division as well. Since n = 7.3 is greater than 7, the left hand side returns TRUE.

Question2.b)

This question is probably the easiest in the whole paper, and it is a question whereby you should score 10/10. Just match the values to the appropriate if-else cases and you will get your answers.

Question2.b)iii)

This part requires some thinking if this is your first time doing such question, but it is in fact very simple, and once you have understood the conversion concepts, it can be applied to other similar questions.

Step-by-step analysis:

  1. Look at the if-else structure and try to understand what it is trying to do. For example, in the first if statement, it is saying that if value EQUALS to 25, multiplier = 2 and base = base * multiplier.

  2. The second if statement (else if) says that if value EQUALS to 24 OR 26, base = base * multiplier.

  3. The third if statement (else if) says that if value is strictly SMALLER than 24 OR strictly LARGER than 26, multiplier = 10.

  4. Now that we are clear on what this whole if-else structure is trying to achieve, we can start on our switch case.

switch(value)

{

case 25:

...(for the first if statement)

break;

case 24:

case 26:

...(for the second if statement)

break;

default:

...(for the last if statement)

break; // this break statement is optional

}

*Note: you need to have the break statement after each case to prevent switch case fall-through--in which the program executes other cases' codes (that comes after it) after entering one of the cases. However, also note that in the above code, I purposely made use of this fact to combine case 24 and 26. When value equals 24, the case 24 is satisfied and code enters and starts executing from there but immediately falls-through into case 26's statements. We can do this since both case 24 and 26 share the same piece of code. This is also why the last break statement inside default case is optional. It is because the default case is written at the end, after all the other cases, which means that there is no other cases for the code to fall-through into after it enters the default case. Thus, the break statement becomes optional but I would still recommend that you write it as a form of good practice.

Question 2.c)i)

This question requires you to be very tedious, not missing any step and using the correct values through each iteration. Writing out the final values of every variable after each iteration is very helpful and always remember to use the latest values instead of the starting values.

If you have done it right, there were 5 iterations and the finals values are: a = 7, b = 21, c = 0.

Question 2.c)ii)

This part will also be easy if you have practiced conversion between while loop, do-while loop and for loop. If you need explanation on how each of this loop works, and how to convert between each of them, look through For, While and Do-While Loops. There are many ways to do it, but this is the simplest way to complete the task:

for(a = 2, b = 41; c > 0; )

{

a++;

b -= 4;

c = b % a;

//Checkpoint 1

if(b < 20)

break;

}

If you would like to see my source code, or my other code variations, please check out FE1008/CY1402-Computing 2016/2017 Semester 2-Comments & Source Code.

That's all for Question 2. If you have any doubts, opinions or suggestions, feel free to leave them in forum section or email me @ KYX@outlook.sg. Thanks~

Related Posts

See All
Recent Posts

Related Posts

Leave your comments or questions below:

bottom of page