Upload
atie-iekah
View
221
Download
0
Embed Size (px)
Citation preview
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 1/7
CONFIDENTIAL
CS/APR 2007/CSC425
UNIVERSITI TEKNOLOGI MARA
FINAL EXAMINATION
COURSE
COURSE CODE
EXAMINATION
TIME
: INTRODUCTION TO COMPUTER PROGRAMM ING
: CSC425
: APR IL 2007
: 3 HOURS
INSTRUCTIONS TO CANDIDATES
1 .
This question paper consists of two (2) parts : PART A (9 Questions)
PART B (2 Questions)
2. Answer ALL questions from all two (2) parts in the Answ er Booklet. Start each answer on a
new page.
3. Do not bring any material into the examination room unless permission is given by the
invigilator.
4 . Please check to make sure that this examination pack consists of:
i) the Ques tion Paper
ii) an Answ er Booklet - provided by the Faculty
DO NOT TURN THIS P GE UNTIL YOU RE TOLD TO DO SO
This examination paper consists of 7 printed pages
© Hak Cipta Universiti Teknologi M ARA CONFIDENTIAL
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 2/7
CONFIDENTIAL 2 CS/APR 2007/CSC425
PART
A
70 MARKS)
QUESTION
Given the following declarations
double x = 5.5, y = -2.5;
int m = 20, n = 6;
string s = "Hello there ", t = "World";
What
are the
values
of
the following expressions?
a )
x +
n * y -
x + n ) * y
b ) m / n + m % n +
5 * x - n / 5
c ) s t a t i c _ c a s t < i n t > x
+ 3 . 0 ) +
s q r t s q r t p o w m
-
n * 2 ) ,
2 ) ) )
d ) 2 - 2 - 2 - 2 - n ) ) )
e ) s . s u b s t r 4 , 2 )
f) s. le ng th ) + t . le ng th )
(12 m arks)
QUESTION 2
If
the
var iab les
a, b, and c
( t y p e d o u b l e )
are 504,
3 0 2 . 5 5 8 , - 1 2 . 3 1 , respect ive ly . Wr i te
th e C++ state me nts tha t wi l l d isplay the fo l lowing l ine (for clarity, a _ deno tes a b lank
space) .
504 302.56 -1 2. 3
(3 marks)
QUESTION
3
Assuming x ( type do ub le ) is 1 2 . 3 3 5 and n (type i n t ) is 100. Show the outpu t lines for the
following statements. For clarity, use the underscore sym bol ( _ ) to denote a blank space .
cout <
<
<
<
cout <
cout <
<
<
< showpoint <<
< setprecision
< "\n x is "
< " n is " «
< "\n\n n is
< showpoint <<
< setprecision
< "\n x is "
fixed
(2)
<< setw
setw(4)
(6) «
<< n
« setw(2)
fixed
(1)
« setw(5) «
X
;
« n;
x;
(4 marks)
© Hak Cipta Universiti Teknologi MARA C O N F ID E N T IA L
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 3/7
CONF IDENTIAL 3 CS/APR 2007/CSC425
Q U E S T I O N 4
Write a C++ assignment statement to com pute each of the following:
a) The volume of sphere using the formula:
(2 marks)
b) Extract the last three digits of the integer st oc kn um be r and assign to p r i c e with a
decimal point before the last two digits, (e.g. if st oc kn um be r is 1 758 31 6, the last three
digits are 316, therefore, p r i c e is assigned the value of 3 .1 6)
(3 marks)
c) Calculate the area of a triangle using Hero s formula: area = ^(s - a)(s - b )(s - c)
(2 marks)
QUESTION 5
Write appropriate C ++ statement(s) for each of the following:
a) A certain city classifies a pollution index less than 35 as pleasant , 35 through 60 as
unpleasant , and above 60 as hazardous . Display the appropriate classification of a
number representing a pollution index.
(4 marks)
b) For each integer nu m in t with value 1 0, 1 1 , 1 2,1 3, 1 4 , and 1 5, display the symbols A,
B,C, D, E, and F, respectively.
(4 marks)
c) Display a list of points (x, y) on the graph of y = x
4
- 3x + 1 for x ranging from -2 to 2 in
steps of 0.2.
(4 marks)
© Hak Cipta Universiti Teknologi MARA C O N F ID E N T IA L
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 4/7
CONFIDENTIAL
CS/APR 2007/CSC425
QUESTION 6
Referring to the fol lowing i f statement, assume that num is an i n t type:
if (fabs(num) <= 4)
i
(num
cout
else
cout
else
cout <<
> 0)
<< 2 *
« 2 *
num <<
num
num
is
+ 1 «
<< endl
out of
endl;
'•
range.\n ;
Wh at is the output produced from the above statement if:
a ) n um i s - 9
b) num is 0
c) num is 3
(6 marks)
QUESTION 7
Referring to the following program segm ent, answer the questions that follow.
cout << \nEnter an integer: ;
cin >> n;
product = n;
count = 0;
while (count
cout «
product
++count;
< 4)
\nproduct is << product;
*= n;
a) What output values are displayed by the above program segment for each of the
following data value entered?
(i) 5
(ii) 6
(iii) 7
(6 marks)
b) In gene ral, for a data value of any number n, what does this loop display?
(3 marks)
c) Rewrite the above program segment by replacing the w h i l e loop with the f o r loop that
will produce the same output.
(3 marks)
© Hak Cipta Universiti Teknologi MARA
CONFIDENTIAL
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 5/7
CONFIDENTIAL CS/APR 2007/CSC425
QUESTION 8
Assu ming that the array nums [ ] is initially as shown be low.
n u m s [ ]
35 50
17 109
88
[ 2 ;
[ 4 ]
Show the contents of the array elements after each of the following program segments is
executed.
a)
b)
temp = nums[0];
nums[0] = nums[2];
nums[2] = temp;
nums
[o ;
[ 1 ]
[ 2 ]
[ 4 ]
(3 marks)
temp
for
=
(i
nums
nums
[4]
nums[0];
= 1; i < 3; ++
[i] = nums[i +
= temp;
i)
l
nums
[ 2 ;
[ 4 ]
(5 marks)
© Hak Cipta Universiti Teknologi M ARA
CONFIDENTIAL
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 6/7
CONFIDENTIAL
CS/APR 2007/CSC425
QUESTION 9
The array monthName [ ] holds the following three-character strings.
m o n t h N a m e [ ]
Jan
Feb Mar
Apr
May Jun
Jul
Aug Sep
Oct
Nov
Dec
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 1 0 ] [ 1 1 ]
a) If integer n is 3, wha t is displayed by the following statem ent?
cout << monthName[++n] << and << monthName[n + 3 ] ;
(3 marks)
b) Wha t value is assigned to string variable w in te rM o n th s by the following statement?
w i n t e r M o n t h s = m o n t h N a m e [ 1 1 ] + , + m o n t h N a m e [ 0 ] + ,
+ m o n t h N a m e [ 1 ] ;
(3 marks)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
7/23/2019 CSC425_7
http://slidepdf.com/reader/full/csc4257 7/7
CONFIDENTIAL 7 CS/APR 2007/CSC425
PAR TB 30 MARKS)
QUESTION 1
Write a program that calculates and displays values for y when
y = xz(x - z)
Your program should calculate y for values of x ranging between l and 5 and values of z
ranging between 2 and 6. The x variable should control the outer loop and be incremented
in steps of 1 and z should be incremented in steps of l . Your program should also display
the message F u n c t i o n U n de f in e d when the x and y values are equal . The output of
your program should look like the following. (Note: you are required to use programmer-
defined functions in your program)
1.0
1.0
1.0
2.0
2.0
2.0
2.0
3.0
4.0
2.0
3.0
4.0
-2.000
-1.500
-1.333
Function Undefined
-6.000
-4.000
(15 m arks)
QUESTION 2
The expansion of a steel bridge as it is heated to a final Celsius temperature, T
F
from an
initial Celsius temperature, T
o
can be approximated using the formula
Increase in length = a * L* (T
F
-T
o
)
where a is a coefficient of expansion (which for steel is 11.7 e-6) and L is the length of a
bridge at temperature
T
o
.
Using this formula, write a C++ program that displays a table of
expansion lengths for a steel bridge that is 7365 meters long at 0 degrees Celsius, as the
temperature increases to 40 degrees in 5 degree increments. (Note: you are required to
use programmer-defined functions in your program)
(15 m arks)
END OF QUESTION PAPER
© Hak Cipta Universiti Teknologi MARA C O N F ID E N T IA L