FE1008/CY1402-Computing 2016-2017 Semester 2-Comments & Source Code
- Jul 7, 2017
- 5 min read
Overall personal comments:
The paper consists mainly of questions to test students on their ability to read, write and understand C code, with some being more difficult as it requires students to come out with their own code--testing students' code logic. There were also a few questions that required students to provide memorised or logical answers, I would say so as they can be found more or less directly from the lecture notes. So, if you have studied the notes and have been practicing coding, this paper should be pretty much doable for you.
Below are the source codes in commented form, copy and paste the following code on to your favourite editor and uncomment the relevant parts for that question before compiling and running it (remember to re-comment the code before moving to the next section of code for the next question):
// FE1008 2016-2017 Semester 2 -- Computing Source Code
// Done by KYX
/** Question 1)b) **/
/*
#include <stdio.h>
int w = -10;
int x = 8;
int y = -3;
double z = 22.5;
int main(void)
{
y = w++ - --x;
z = z + w/x * z;
printf("w = %d, x = %d, y = %d, z = %f", w, x, y, z);
}
*/
/** Question 1)c) **/
/*
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int first = 0;
int second = 0;
int random_part = 0;
int main(void)
{
// set seed
srand((unsigned)time(NULL));
do
{
fflush(stdin); //remove the enter key so that program will not continue indefinitely
printf("Please provide a positive integer: ");
scanf("%d", &first);
}while(first <= 0);
do
{
fflush(stdin); // and to remove any leftover characters in the stream.
printf("Please provide another larger positive integer: ");
scanf("%d", &second);
}while(second < first); // make sure second input is greater than first.
// random number generation
random_part = rand()%second + 1; //rand()%second gives 0 to second-1
// compare value of random_part and first
if(first >= random_part)
{
printf("First integer %d is larger than or equal to random number %d", first, random_part);
}
return 0;
}
*/
/** Question 2)a)i) **/
// Originally, p=-7, q=11, r='M' and n=7.3
/*
#include <stdio.h>
int p = -7, q = 11;
char r = 'M';
double n = 7.3;
int main(void)
{
if((-p != q - 4) || n)
printf("True");
else
printf("False");
return 0;
}
*/
/** Question 2)a)ii) **/
// Originally, p=-7, q=11, r='M' and n=7.3
/*
#include <stdio.h>
int p = -7, q = 11;
char r = 'M';
double n = 7.3;
int main(void)
{
if(n > r/q && p < -9)
printf("True");
else
printf("False");
return 0;
}
*/
/** Question 2)b) i) & ii) **/
/*
#include <stdio.h>
int value = 0, base = 50, multiplier = 1;
int main(void)
{
//value = 25;
value = 27;
if(value == 25)
{
multiplier = 2;
base = base * multiplier;
}
else if(value == 24 || value == 26)
{
base = base * multiplier;
}
else if(value < 24 || value > 26)
{
multiplier = 10;
}
printf("base = %d, multiplier = %d", base, multiplier);
return 0;
}
*/
/** Question 2)b) iii) **/
/*
#include <stdio.h>
int value = 0, base = 50, multiplier = 1;
int main(void)
{
value = 25;
//value = 27;
switch(value)
{
case 25:
multiplier = 2;
base = base * multiplier;
break;
case 24:
case 26:
base = base * multiplier;
break;
default:
multiplier = 10;
break;
}
printf("base = %d, multiplier = %d", base, multiplier);
return 0;
}
*/
/** Question 2)c)i) **/
/*
#include <stdio.h>
int main(void)
{
int a = 2, b =41, c;
while(c > 0)
{
a++;
b -= 4;
c = b % a;
// checkpoint 1
printf("a = %d, b = %d, c = %d\n", a, b, c);
if(b < 20)
break;
}
printf("Final: a = %d, b = %d, c = %d", a, b, c);
return 0;
}
*/
/** Question 2)c)ii) **/
/*
#include <stdio.h>
int main(void)
{
int a = 2, b =41, c;
for(;c > 0;)
{
a++;
b -= 4;
c = b % a;
// checkpoint 1
printf("a = %d, b = %d, c = %d\n", a, b, c);
if(b < 20)
break;
}
printf("Final: a = %d, b = %d, c = %d", a, b, c);
return 0;
}
*/
/** Question 3) **/
/*
#include <stdio.h>
int main(void)
{
// 3a)
int num1[10];
int num2[2][10] = { {1,2,3,4,5,6,7,8,9,10},
{0} };
int i;
char ch1[] = "Teddy";
// size before content of array has been changed
printf("Size of num1 array = %d\n", sizeof(num1));
// 3b)
int redo = 0;
for(i = 0; i < 10; i++)
{ do
{
printf("Please enter number %d: ", i+1);
redo = scanf("%d", &num1[i]);
}while(redo == 0);
}
// 3c) & 3d)
i = 0;
while(i < 6)
{
num1[i] = num1[i] + num2[0][i] + ch1[i];
printf("num1[%d] = %d \n", i, num1[i]);
i++;
}
// size after content of array has been changed
printf("Size of num1 array = %d\n", sizeof(num1));
return 0;
}
*/
/** Question 4) **/
/*
#include <stdio.h>
#include <math.h>
double func1(double a, double b);
double func2(double a, double b);
int main(void)
{
double a, b;
printf("Enter a real number: ");
scanf("%lf", &a);
printf("Enter another real number: ");
scanf("%lf", &b);
if(func2(a, b))
{
return 0; //terminate the program
}
else
{
printf("%.5lf", func1(a, b)); //display largest number
}
return 0;
}
double func1(double a, double b)
{
if(a > b)
return a;
else
return b;
}
double func2(double a, double b)
{
// same up to 2 d.p. return non-zero
a = floor(a*1000);
b = floor(b*1000);
printf("After flooring, a = %lf, b = %lf\n", a, b);
// put as int for use of %
int c = a;
int d = b;
printf("After int, c = %d, d = %d\n", c, d);
// comparing values place-by-place
// step 1: compare thousandth place
int a_thousandth = c - c%1000; // remainder of a%1000 gives hundredth and below value. Deduct from a and we get how many thousands there are.
int b_thousandth = d - d%1000;
c %= 1000; //remove thousandth placing
d %= 1000;
int a_hundredth = c - c%100;
int b_hundredth = d - d%100;
c %= 100; // remove hundredth placing
d %= 100;
int a_tenth = c - c%10;
int b_tenth = d - d%10;
if(a_thousandth == b_thousandth)
{
if(a_hundredth == b_hundredth)
{
if(a_tenth == b_tenth)
{
return 1; // non-zero
}
}
}
// no chance of same value up to 2d.p.
return 0;
}
*/
/*
1.23 vs 1.239 --> considered same here
1.23 vs 1.24 --> considered different
how about 1.234 vs 1.243
multiply by 100: 123.4 vs 124.3
then floor it to remove any decimals
Get: 123 vs 124
Better: Multiply by 1000
*/
/** Question 4) **/
#include <stdio.h>
#include <math.h>
double func1(double a, double b);
int func2(double a, double b);
int main(void)
{
double a, b;
int redo = 0;
do
{
fflush(stdin);
printf("Enter a real number: ");
redo = scanf("%lf", &a);
}while(redo == 0);
redo = 0;
do
{
fflush(stdin);
printf("Enter another real number: ");
redo = scanf("%lf", &b);
}while(redo == 0);
if(func2(a, b))
{
printf("SAME up to 2 d.p.\n");
}
else
{
printf("NOT THE SAME up to 2 d.p.\n");
}
return 0;
}
double func1(double a, double b)
{
if(a > b)
return a;
else
return b;
}
int func2(double a, double b)
{
int c, d;
c = floor(a * 1000);
d = floor(b * 1000);
c /= 10;
d /= 10;
printf("int c = %d and int d = %d\n", c, d);
if(c == d)
return 1;
else
return 0;
}
That's all for the source code. If you have any doubts, opinions or suggestions, feel free to leave them in forum section or email me @ KYX@outlook.sg. Thanks~






Comments