You are on page 1of 15

CONFIDENTIAL

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

FINAL EXAMINATION
SEMESTER I
SESSION2015/2016
ANSWER SCHEME

COURSE NAME : COMPUTER PROGRAMMING


COURSE CODE : BFC 20802
PROGRAMME : BACHELOR OF CIVIL
ENGINEERING WITH HONOURS
EXAMINATION DATE : DECEMBER 2015 / JANUARY 2016
DURATION : 2 HOURS 30 MINUTES
INSTRUCTION : 1. ANSWER ALL QUESTIONS IN SECTION A
AND B.
2. CHOOSE ONLY ONE (1) QUESTION IN
SECTION C

THIS QUESTION PAPER CONSISTS OFEIGHT (8) PAGES

CONFIDENTIAL
SECTION A

Instruction: Please answer T (True) or F (False).


(10 marks)
CONFIDENTIAL BFC 20802

Questions TRUE FALSE


a >>, ;, <<, cout, cin are all key words for a programming language. F
b Random Access Memory (RAM) is volatile where it is erased T
when the program terminates
c The operator OR (||) is used where a new rational expression is F
true if both expressions are true.
d The following symbol is a decision symbol where it is used to F
represent operations in which there are two possible selections

e Given x = 100, the answer to the expression x/(100/%10)+x is T


100.

f The following code will not give an error. F


Int main ()
{
char name [21];
cout<< My name is;
cin>> name;
cout<< Good day to you Mr<< name <<endl;
return 0;
}

g The flowchart of a while lope is as shown T

Expression
Statements

h A runtime error appears if the program produces incorrect result F


but it compiles fine. (FALSE)

i Binary numbers are 1010100001010. T

j The ‘if’ statement are allows statement to be conditionally T


executed or skipped over.

SECTION B

Instruction: Answer ALL questions.

Q1 Name the symbols of the flowchart as below:


(5marks)

2 CONFIDENTIAL
CONFIDENTIAL BFC 20802

Symbol Answer
Terminal

Process

Comment

On page Connector

Decision

Q2 Write a single C++ statement or line that accomplishes each of the following:

(a) Assign the division operator of variables P and A to variable σ.


sigma = P/A;
(1 marks)

(b) Print the message "Total for three digits".


cout<< "Total for three digits";
(1 marks)

(c) Instead of a = a + 1, we can write using increment operator as _____________.


a++ 
(1 marks)

(d)Input four integer values from the keyboard into integer variables a, b, c and d.
cin>> a >> b >> c>> d;
(1 marks)

(e) Add x with 2, square root it and stored into y.


y= sqrt(x+2);
(1 marks)

QX Based on the given flowchart, identify the output of the average of odd numbers from 1
to 9. Construct the algorithm based on the flow chart.

3 CONFIDENTIAL
CONFIDENTIAL BFC 20802

sum = 0
i =1

i < 11

True

sum = sum + i

False
i = i+2

av rg = sum /5

(10 marks)

Q4 Label the function of each C++ statement as below:

Statement Function
# include <iostream> Preprocessor Directive
using namespace std;
/ / A typical C++ Program Comment
main ( ) Begin
{ Main function
Variables declaration
…….. ;
Statements;
} End
(5 marks)

Q5 Identify and correct the error(s) in each of the following:


(a) int main(); - delete
{
int a;
cout<< "Insert one integer:"; - missing “
4 CONFIDENTIAL
CONFIDENTIAL BFC 20802

cin>>a;
}
(2 marks)

(b) if ( b > 0 ); - delete


{
cout>> "The number that has been inserted is positive" <<endl; - <<
}
(2 marks)

(c) #include <isostream> - mispell


using namspace std; - mispell
int Main() 

(2 marks)

(d) while (x < 10)


}
cout>> “t\t” << x << “/n”;- << and \
(2 marks)

(e) for (i == 0; i <= n; i++)– only =


{
increment = increment –i;- should be +
}
(2 marks)
(f) main (void); - delete
{
char a[10];
cout << “insert your name:”;
cin >> a;
cin.ignore();- delete
(2 marks)

Q6 Write a single C++ statement or line that accomplishes each of the following:

(a) An expression that uses a relational operator to return TRUE


if the variable Ahmad is not equal to Ali.
if (Ahmad != Ali)
(1 marks)

(b) A WHILE loop that will display the numbers from 500 to 550.
while (numbers=>500&& numbers <=550)
(1 marks)

(c) Declare a constant YIELD_STRENGTH inside a function with


value30
constYIELD_STRENGTH = 30;
(1 marks)

5 CONFIDENTIAL
CONFIDENTIAL BFC 20802

(d) What is the output of the following code?


bool even = false;
cout<< (even ? "true" : "false") <<endl;

false
(1 marks)

(e) Print a statement that prompt “b2 – 4ac”.


b*b – 4*a*c;
(1 marks)

Q7 Fill in the blank with the correct answer:


(10 marks)

#include <iostream>
usingnamespacestd;

intmain()
{
double hours, rate, pay;
// Get the number of hours worked.
cout<< "How many hours did you work? ";
cin>> hours;

// Get the hourly pay rate.

cout<< "How much do you get paid per hour?";

cin>> rate;

// Calculate the pay.

pay = hours * rate; // Display the pay.

cout<< "You have earned $" << pay <<endl;


system ("pause");
return 0;
}

6 CONFIDENTIAL
CONFIDENTIAL BFC 20802

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
floatnew_sigma, new_tau, theta, A1, P;
floatcos_value = cos(theta);
floatsin_value = sin(theta);

Q8 cout<<"Enter
Fill in the blank value ofanswer:
with the correct P : \n";
cin>>P; (10 marks)
cout<<"Enter value of A1 : \n";
cin>>A1;

cout<<"Enter value of theta : \n";


cin>>theta;

new_sigma=P*cos(theta)*cos(theta)/A1;
cout<<"answer, new_sigma =
\n"<<new_sigma<<"\n"<<"\n";
cin.get ();

cout<<"Enter value of P : \n";


cin>>P;

cout<<"Enter value of A1 : \n";


cin>>A1;

cout<<"Enter value of theta : \n";


cin>>theta;

new_tau=P*sin(theta)*cos(theta)/A1;
cout<<"Answer, new_tau = \n"<<new_tau<<"\n";

system("PAUSE");
return 0; 7 CONFIDENTIAL
}
CONFIDENTIAL BFC 20802

SECTION C
Instruction: Choose ONE (1) question only.

Q1 Construct i) a pseudo code based on the flowchart (refer to Figure Q1)and write ii) a
complete C++ code which will ask user to travel from Ayer Hitam to southern
inbounds using the PLUS Highway. The program will display the destinations, the toll
rates and distances according to the selection by the user.
Example of Output: refer to Figure Q1.
(30 marks)
Q2 A stone is thrown upwards with an initial velocity (u) of 30 m/s. The maximum peak
the stone could reach is calculated by the following calculation and conditions.
The acceleration, a = g = -9.81 m/s2

v 2−u2 02−302
a) The distance, s= = =45.87 m/s
2a 2 (−9.81 )

Then from the moment the stone is thrown upwards, the time needed for the stone to
retouches the earth surface is calculated according to the following condition

b) Time needed to retouch the surface


2
−u ± √ u2−4 as −30± √ 30 −4 (−9.81 ) ( 0 )
t= = =6.12 s
a −9.81

8 CONFIDENTIAL
CONFIDENTIAL BFC 20802

The total time requires for the stone to retouch the surface from the moment it is thrown
upwards is given as 6.12s. Within 6 seconds, the movement of the stone is illustrated as
a quadratic path and the displacement of the stone from the peak is shown in the
following table.

c) By referring the following equation, the calculation for the displacement could
be achieved by using a calculation loop.

1
s=ut+ at 2
2

Construct a pseudo code of a flowchart and write a complete C++ code to calculate
the condition as instructed in a), b) and c). The program will display the distance, time
needed to reach the peak and the displacement of the stone within 6 iterations or
seconds.
(30 marks)

START
-END OF QUESTION –

OUTPUT
"YOU ARE TRAVE LLING F ROM AYE R H ITAM"

OUTPUT
"SE LE CT YOUR DE STIN ATION "

FINAL EXAMINATION
SEMESTER / SESSION :SEM I / 2015/2016 PROGRAMME :2BFF/ 3 BFF
COURSE:COMPUTER PROGRAMMINGCOURSE CODE: BFC20802

P RE SS 1 P RE SS 2 P RE SS 3 P RE SS 4 P RE SS 5 P RE SS 6
MACHAP SIMPANG SE DE NAK KULAIJ AYA SE NAI (U) SKUDAI
RE NGGAM

RATE 1 RATE 2 RATE 3 RATE 4 RATE 5 RATE 6


RM 1.20 RM 3.10 RM 5.70 RM 7.50 RM 8.70 RM 9.10

DISTAN CE 1 DISTANCE 2 DISTAN CE 3 DISTANCE 4 DISTANCE 5 DISTANCE 6

7.40 KM 21.90 KM 39.20 KM 52.60 KM 61.10 KM 65.90 KM

OUTP UT : PRINT DE STINATION, RATE AND


DISTANCE
9 CONFIDENTIAL
END
CONFIDENTIAL BFC 20802

FIGURE Q1

FINAL EXAMINATION
SEMESTER / SESSION : SEM I / 2015/2016 PROGRAMME: 2BFF/ 3 BFF
COURSE : COMPUTER PROGRAMMING COURSE CODE : BFC20802

TABLE 1
Time (second) Displacement
(meter)
1 25.095
2 40.380
3 45.855
4 41.520
5 27.375
6 3.420

Height (m)

10 CONFIDENTIAL
CONFIDENTIAL BFC 20802

FIGURE Q2

START

OUTP U T
"YOU ARE TRAVE LLING F ROM AYE R H ITAM"

Q1 Example of Output: refer to Figure QX.


OUTP UT
"SE LECT YOUR DE STINATION "

P RE SS 1 P RE SS 2 P RE SS 3 P RE SS 4 P RE SS 5 P RE SS 6
MACHAP SIMPANG SE DENAK KULAIJ AYA SE NAI (U ) SKU DAI
RE NGGAM

RATE 1 RATE 2 RATE 3 RATE 4 RATE 5 RATE 6


RM 1.20 RM 3.10 RM 5.70 RM 7.50 RM 8.70 RM 9.10

DISTANCE 1 DISTANCE 2 DISTANCE 3 DISTANCE 4 DISTANCE 5 DISTANCE 6

7.40 KM 21.90 KM 39.20 KM 52.60 KM 61.10 KM 65.90 KM

OUTP UT : P RINT DE STINATION , RATE AN D


DISTANCE
11 CONFIDENTIAL
END
CONFIDENTIAL BFC 20802

ANSWER SECTION C Q1
//Program travel
#include <iostream>
using namespace std;

int main ( ) {
int press;

cout<<"YOU ARE TRAVELLING FROM AYER HITAM"<<endl;


cout<<"SELECT YOUR DESTINATION"<<endl;
cout<<"PRESS 1. MACHAP"<<endl;
cout<<"PRESS 2. SIMPANG RENGGAM"<<endl;
cout<<"PRESS 3. SEDENAK"<<endl;
cout<<"PRESS 4. KULAIJAYA"<<endl;
cout<<"PRESS 5.SENAI (U)"<<endl;
cout<<"PRESS 6.SKUDAI"<<endl;

switch (press) {
case 1
cout<<"FROM AYER HITAM TO MACHAP"<<endl;
cout<<"THE TOLL RATE IS RM 1.20"<<endl;
cout<<"THE MILEAGE WILL BE 7.40 KM"<<endl;
break;
case 2
cout<<"FROM AYER HITAM TO SIMPANG RENGGAM"<<endl;
cout<<"THE TOLL RATE IS RM 3.10"<<endl;
cout<<"THE MILEAGE WILL BE 21.90 KM"<<endl;
break;
case 3
cout<<"FROM AYER HITAM TO SEDENAK"<<endl;
cout<<"THE TOLL RATE IS RM 5.70"<<endl;
cout<<"THE MILEAGE WILL BE 39.20 KM"<<endl;
break;
case 4
cout<<"FROM AYER HITAM TO KULAIJAYA"<<endl;
cout<<"THE TOLL RATE IS RM 7.50"<<endl;
cout<<"THE MILEAGE WILL BE 52.60 KM"<<endl;
break;
case 5
cout<<"FROM AYER HITAM TO SENAI (U)"<<endl;
12 CONFIDENTIAL
CONFIDENTIAL BFC 20802

cout<<"THE TOLL RATE IS RM 8.70"<<endl;


cout<<"THE MILEAGE WILL BE 61.10 KM"<<endl;
break;
case 6
cout<<"FROM AYER HITAM TO SKUDAI"<<endl;
cout<<"THE TOLL RATE IS RM 9.10"<<endl;
cout<<"THE MILEAGE WILL BE 65.90 KM"<<endl;
break;
default :
cout<<"PRESS 1 TO 6"<<endl;

return 0;
}

PSEUDO CODE

1. Start
2. Read i
3. Input the destination
Destination based on selection
Case 1
Print "YOU ARE TRAVELLING FROM AYER HITAM TO MACHAP"
Print "THE TOLL RATE IS RM 1.20"
Print "THE MILEAGE WILL BE 7.40 KM"
Case 2
Print "YOU ARE TRAVELLING FROM AYER HITAM TO SIMPANG RENGGAM"
Print "THE TOLL RATE IS RM 3.10"
Print "THE MILEAGE WILL BE 21.90 KM"
Case 3
Print "YOU ARE TRAVELLING FROM AYER HITAM TO SEDENAK"
Print "THE TOLL RATE IS RM 5.70"
Print "THE MILEAGE WILL BE 39.20 KM"
Case 4
Print "YOU ARE TRAVELLING FROM AYER HITAM TO KULAIJAYA"
Print "THE TOLL RATE IS RM 7.50"
Print "THE MILEAGE WILL BE 52.60 KM"
Case 5
Print "YOU ARE TRAVELLING FROM AYER HITAM TO SENAI (U)"
Print "THE TOLL RATE IS RM 8.70"
Print "THE MILEAGE WILL BE 61.10 KM"

Print "YOU ARE TRAVELLING FROM AYER HITAM TO SKUDAI"


Print "THE TOLL RATE IS RM 9.10"
Print "THE MILEAGE WILL BE 65.90 KM"
Default
Print "PRESS 1 TO 6"
4. Display Case
5. End

13 CONFIDENTIAL
CONFIDENTIAL BFC 20802

Q2

// program for rectilinear motion by Dr. Nizam Zakaria


#include <iostream>
#include <cmath>
using namespace std;

int main() {

float s, u, v, t, g, distance, movement, time1, time2;


cout<< "Input initial velocity (u) and gravity (a@g)";
cin>> u >> g;
distance = (v*v-u*u)/(2*(-g));
time1 = (-u+sqrt(u*u-4*(-g)*s))/(-g);
time2 = (-u-sqrt(u*u-4*(-g)*s))/(-g);

if (time1 > 0) {
cout<< "Time required to retouch =" << time1 <<endl;
}
else if (time2 > 0) {
cout<< "Time required to retouch =" << time2 <<endl;
}
for(int x = 1; x < 7; x++){
movement = u*x+0.5*(-g)*x*x;
14 CONFIDENTIAL
CONFIDENTIAL BFC 20802

cout<< "The movement for" << x << "second will be" <<endl;
cout<< movement <<endl;
}
return 0;
}

15 CONFIDENTIAL

You might also like