top of page

Computing 2016-2017 Semester 2-Question 1

Below are comments and further explanation on question 1.

Question 1.a)i)&ii)

This question aims to test if you fully understand how a C source code can be used to generate an executable file. This is more of memory work. The 2 main steps is to compile .c to object files (.o) and linking .o and appropriate libraries to form .exe.

When a source code cannot be compiled into a executable, it is mainly due to a compilation error. Only syntax error prevents a source code from compiling to generate the intended .exe file.

Question 1.b)

First tricky part of this question is that you ought to know how pre- and post- increment/decrement works, or at least what results they give. Pre-increment adds 1 to the variable's value before returning this incremented value back to the placeholder in the expression while post-increment adds 1 to the variable's value but returns the original value to the placeholder in the expression (or you can think of it as adding 1 after returning the original value). Refer to notes on Pre- and Post- increment/decrement operators for more information.

Therefore, you need to know what values were given to variable y. Also, be careful that values of variable w and x have been changed and later use of them will follow their new values.

Second tricky part of this question is that you should recognise that integer division involved, not your familiar mathematical division where you will get fractions and decimals. In this case, after the 1st statement, w = -9 and x = 7, w/x gives -1, not -1.2857.

Question 1.c)

A very long question, however, it is separated into smaller sub-parts defined by the point form, making the question more doable.

First and second points are simple but do take note that you should check the input from the user before continuing. You can do so by writing the code such:

int first = 0;

do { fflush(stdin);//remove any characters left in the input stream printf("Please provide a positive integer: "); scanf("%d", &first);

}while(first <= 0);

*Take note that you need the "fflush(stdin)" line so that the input stream will be cleared of any leftover or unused characters. For more information on stdin, check it out here.

The third and fourth points requires the use of rand() and srand() respectively. Trick: convert the random value to fit the range of numbers desired:

random_part = rand()%second + 1;

// second is a variable storing the second positive integer value our user has keyed in

How this piece of code works is that rand() will generate a number between 0 and RAND_MAX (RAND_MAX is a constant defined in stdlib.h as 0x7FFF or 32 767). Then, we shall use the remainder of the value when it is divided by second to get values from 0 to second-1. After which, we add 1 to it to get values from 1 to second.

*Remember to add the libraries <time.h> for time() and <stdlib.h> for rand() and srand().

The fifth point is just a simple if-else statement to compare and display the required sentence.

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 1. 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