Вы находитесь на странице: 1из 5

KCEC 1108 Computer Programming

Sem I, 2012/2013
1

ACADEMIC SESSION 2007/2008
1. The elements of an array can be initialized in the
array declaration by following the array name with an
equal sign and brace-delimited, as following:
int number[10] = { 32, 27, 64, 18, 95, 14, 90, 70, 50, 37};
Write a program to print out the results

ACADEMIC SESSION 2007/2008
2. Write a program that creates a table of degree
Celsius (C) with the corresponding degrees
Fahrenheit (F). Begin at 10C and proceed to 100C
in 20C increments using a for loop. The sample
output is shown in table, which uses setw(12) and
setw(19) for C and F, respectively.
(Given, 32
5
9
+ |
.
|

\
|
= C F )




ACADEMIC SESSION 2008/2009
3. Write a program to calculate the summation of the numbers 1 to 20 by using,
a) while loop.
b) do-while loop
c) for loop.

ACADEMIC SESSION 2008/2009
4. Write a program that reads in a weight in pounds and ounces and output the
equivalent weight in kilograms and grams.
a) Write three functions: one for input, one for calculating and one for output.
b) int main( ), include a loop that lets the user repeat the computation for new
input values until the user says he or she want to end the program.
Giving that 2.2046 pounds in a kilogram and 16 ounces in a pound.

ACADEMIC SESSION 2008/2009
5. Figure Q5 shows the content of a text file called myData.dat. Write a program to
analyze the data:




Figure Q5

a) Declare a 1-Dimensional array for the data.
b) Write a function that can read an input file and store the data in the array, as
declared in Question Q5(a).
c) Write a function to determine the minimum and maximum values.
d) Write a function to print the results from Question Q5(c) on the monitor
screen.
32 27 64 18
95 14 90
70 50
37

KCEC 1108 Computer Programming
Sem I, 2012/2013
2

ACADEMIC SESSION 2008/2009
6. Write a computer program capable of solving the quadratic equation:
0
2
= + + c bx ax
a) The input data is to consists of the values of a, b and c and is to come from
the keyboard.
b) There are three possibilities of the solution for the quadratic equation. Write
the solution by using decision making method (if-else-if). The output is to
consist of value(s) x and go to the screen.

ACADEMIC SESSION 2009/2010
7. In mathematics texts, the factorial function is usually written n! and is defined
to be the product of all the integer from 1 to n, as follows:
n! = 1 x 2 x 3 x x (n-2) x (n-1) x n
Write a program to solve the above
problem, which n can be any number enters
by the user and using the following loops:
a) do-while
b) for



ACADEMIC SESSION 2009/2010
8. Solve the problem using a two dimensional array. Assume number of array
elements is unknown. The general gas law for an ideal gas is given by



Where P = pressure, V = volume, T = temperature (Kelvin). Write a computer
program that computes the temperature of a gas that is originally at P
1
= 5 atm,
V
1
= 30 liters, T
1
= 273 Kelvin.

a) Write a function for input specification, where the input data should come
from the file IDEAL_GAS.dat and consist of five lines:
Line 1 =
Line 2 =
Line 3 =
Line 4 =
Line 5 =

b) Write a function for output specification, where the output should be printed
on the screen and write to RESULT.dat, consisting of the following
information:








P
2
V
2
P
3
V
3
P
4
V
4
P
5
V
5
P
6
V
6




Sample data file





2

40

3

80

6

50

1

15

2

70





2
2 2
1
1 1
constant
T
V P
T
V P
T
PV
= =
The below-listed pressure, volume and temperature conditions
can occur for a given mass of an ideal gas which is
originally at P = 5 atm, V = 30 litres and T = 273 K.
Case P (atm) V(l) T(K)
1 2 40 ???
2 3 80 ???
3 6 50 ???
4 1 15 ???
5 2 70 ???
KCEC 1108 Computer Programming
Sem I, 2012/2013
3

ACADEMIC SESSION 2009/2010
9. A block is resting on a horizontal plane and is intended
to be pulled with a force acting at an angle to the
horizontal, as shown in Figure Q9. The block weight
30 kN and has a coefficient of friction of 0.2. Write a
program to calculate the magnitude of the force (F)
needed to pull the block at different angles (),
between 10 and 80 degree in 10-degree increments.
Write the result to a file named ANSWER.dat.
Note: You must use at least one function for
calculation, other than main function.









Figure Q9

Modify your program in question 8(a), which allows the user to enter the block
weight (weight_block), coefficient of friction (coe_friction) and increments angle
(inc_angle). The program also prints the result on the screen.

ACADEMIC SESSION 2009/2010
10. A moving car is being braked at constant acceleration. Compute whether or not
the car has come to a complete halt. The program should fulfill the following
requirements:
a) Read the initial velocity (v
0
), acceleration (a) which is negative to
represent braking and amount of braking time (t
0
) from the keyboard.
b) Determine whether the car has completely stopped or is still moving with a
final velocity (v
1
). Print the statement The car has come to a complete
stop or the final velocity on the screen.
c) If the car is complete stop, determine the distance (s
0
) traveled by the car
before completely stopping after the brake has being applied. Print the
result on the screen.
d) If the car still travels with the final velocity (v
1
), calculate the required time
(t
1
) and distance (s
1
) to stop the car completely and print the result on the
screen. Assuming the acceleration is constant.
e) Program should includes a loop that lets the user to repeat the
computation for new input values until the user says he or she wants to
end the program.

ACADEMIC SESSION 2010/2011
11. A motorcycle accelerates at a constant rate from a stationary position. The gear
in which the motorcycle is in depends on the velocity as shown in table. Write a
program which is able to have a user input an acceleration and the riding time.
Also, determine which gear the motorcycle is in at that time and limit the
maximum velocity to 100 km/hr. Print the velocity and the gear number to the
screen. For any velocities that are on the border of a gear change, use the
higher gear. The program must use the following decision making structure:
(a) if-else-if
(b) switch
Velocity (km/hr) Gear
0-25 1
25-50 2
50-75 3
75-100 4



KCEC 1108 Computer Programming
Sem I, 2012/2013
4

ACADEMIC SESSION 2010/2011
12. There are three large rectangular block need to be pushed by worker(s) from
rest to a specified velocity. The surface is frictionless and the pushing distance
is specified in Table Q12. Assume that one worker can push with the force of
800N. Write a program to:
(a) read the data from the file BLOCK.dat
(b) determine the mass (m) for each block
(c) determine the required force (F) to push each block.
(d) determine the minimum number of worker(s) required to push each block.
(e) display the output on the screen and write the result to a file named
RESULT.dat
(Given, kinetic energy,
2
2
1
mv E
k
= and work done, Fs W = )
Table Q12
Material
Height,
h (m)
Width,
w (m)
Length,
l (m)
Density,
(kg/m
3
)
Distance to
push,
s (m)
Velocity to
achieve,
v (m/s)
Concrete 3 6 5 2500 20 0.55
Wood 4.2 8.1 3.4 1200 12.4 0.81
Steel 5.9 3.2 2.5 5200 18.6 0.43


ACADEMIC SESSION 2010/2011 (Similar to ACADEMIC SESSION 2007/2008)
13. Write a program that creates a table of degree Celsius (C) with the
corresponding degrees Fahrenheit (F) and Kelvin (K). Begin at 0C and
proceed to 100C in 10C increments. The sample output is shown in TABLE
Q12, which uses setw(12), setw(12), and setw(16) for C, F and K. You must
using a
(a) while loop
(b) do-while loop
(c) for loop
(Given, 32
5
9
+ |
.
|

\
|
= C F and 15 . 273 + = C K )
Table Q12





KCEC 1108 Computer Programming
Sem I, 2012/2013
5

ACADEMIC SESSION 2011/2012

14. Write a program to read-in 10 scores key-in by the user, which are stored into
an one-dimensional array score[10]. Determine the minimum and maximum
values of the data. Write a function to determine the mean and standard
deviation of the data. Store the results into a file called statistic.dat.
(Given,
N
x
x

= and
( )
N
x x
S


=
2
)

ACADEMIC SESSION 2011/2012
15. The grading structure for a class is shown in Table Q15. Write a program to
analyze 7 different numerical marks (all integers).
(a) For the input, read the 7 marks & credit hours from the input file marks.dat.
(b) Determine the grade for each score.
(c) Determine the Grade Point Average (GPA).
(d) Save the results to a file called grade.dat.
Figure Q15 shows the example of input (marks.dat) and output file (grade.dat).
(Given,
( )


=
Hours Credit
s Credit Hour s GradePoint
GPA )

Table Q15
Marks Grade Grade Points
80 100 A 4.0
75 79 A- 3.7
70 74 B+ 3.3
65 69 B 3.0
60 64 B- 2.7
55 59 C+ 2.3
50 54 C 2.0
45 49 C- 1.7
40 44 D+ 1.5
35 39 D 1.0
< 35 F 0.0















Figure Q15

-----OOOO-----
75 2
68 2
85 2
80 3
78 3
71 3
81 3

Input file
marks
credit
hours
75 2 A-
68 2 B
85 2 A
80 3 A
78 3 A-
71 3 B+
81 3 A
GPA = 3.69

Output file
marks
credit
hours grade
GPA

Вам также может понравиться