You are on page 1of 6

KL1164 Programming Language

Semester 2, 2012/2013
Assignment A
1. The solution of two simultaneous equations:
a1x + b1y = c1
a2x + b2y = c2

(1)
(2)

is given by (Cramers Rule):


c b b c
a c c a
x= 1 2 1 2 ,y= 1 2 1 2
a1b2 b1a2
a1b2 b1a2
Make a program where the user inputs a1, a2, b1, b2, c1 and c2. The program will then
display the two simultaneous equations (1) and (2) above. After that it will calculate
a1b2 - b1a2. If this value is equal to zero then there is no unique solution a message
will be displayed informing the user of that fact. Otherwise the program will
calculate the solution using the above formula and displays it to the user. Use the six
digits of your ID no. as the inputs such that if the ID is 147892 then a1= 1, a2 = 4, b1
= 7, b2 = 8, c1 = 9 and c2 = 2. Include the code and its output to the user in your
report. Test also for two additional sets of inputs, one set that does not produce a
unique solution and another that does produce a unique solution. Include the outputs
for these additional input sets in your report.
2. Modify the program to solve two simultaneous equations above so that after
processing the potential solution, the user either chooses to exit the program or try
another set of simultaneous equations. The program structure would be as follows:
while(run) {
User inputs a1, a2, b1, b2, c1 and c2.
Program displays equations a1x + b1y = c1 and a2x + b2y = c2.
if(unique solution exist)
Display value of x and y.
else
Inform that a unique solution doesnt exist.
Ask user to input another set of simultaneous equations or end program.
if(end program)
Set run to false
}
The pseudo code in bold is the previous program. Use the same inputs as in part 1
above. Include the code and its output to the user in your report.

Assignment B
1. a) Write out a program to print out the first N (1 < N < 1000) terms in the series:
1,

1 1 1
1
, ,
, 2
4 9 16
N

Enter N such that N is the last three digits of your ID (if the ID is 147892, then N
= 892). Include the last 10 numbers of your output in your report with the code.
Use a for loop and make sure the output is accurate.
b) Modify your program so that it outputs the sums of the terms in the series below
for N = 1 until N equals the last three digits of your ID (892 in the previous case):
N

i
i 1

=1+

1
1
1
1
+ +
2
4
9 16
N

Include the last 10 numbers of your output in your report with the code.
2. The value of the sum above is the Basel problem first posed by Pietro Mengoli in
1644. In 1735 Leonhard Euler found its exact value to be equal to 2 / 6. Hence this
sum can be used to estimate the value of . Modify your program in Exercise 1 (b)
above so that at each iteration of summation it outputs the estimate of . Use the sqrt
function that you can access by including math.h. Include the last 10 numbers of
your output in your report with the code. How many digits of the final estimate are
correct?

C = d
= 3.14159 (correct to 5 decimal places)

Assignment C
1. Write a program to calculate a simple equation result = a operation b input by the
user. The different inputs for operation are as follows and they should be handled by
using the switch statement:
operation

+
*
/

Interpretation
Addition
Subtraction
Multiplication
Division

Compile the complete program and include the code in your report. Include also the
output from entering wx * yz where wxyz are the 4 last digits of your ID i.e. if the ID
is 147892 then wx = 78 and yz = 92.
2. Modify the program above in part 1 so that it can raise numbers to the power of
another number (integer powers only). Let the symbol be ^. Hence if one enters 8.9 ^
2 it will output 79.21. Negative powers are rejected. Include the code in your report
with the output of x.y ^ z where xyz are the 3 last digits of your ID.

Assignment D
The solution to a quadratic equation ax + bx + c = 0 is:

b b 2 4ac
x=
2a
It was introduced to Europe in the 12th century from the works of the Spanish Jewish
mathematician Abraham bar Hiyya who read the works of Al-Khawarizmi (9th century).
1. Create a program that lets the user enter the coefficients a, b and c. The program then
displays the quadratic equation. After that it will test the value of b2 4ac. If it is
larger than 0 then two real solutions are output. If it is equal to 0 then only one real
solution is output. If it is less than 0 then there are no real solutions and an error
message is output. Include the code in your report and the output with a, b and c
derived from your ID such that if your ID is 147892 then a = 14, b = 78 and c = 92.
2. Modify the program so that if b2 4ac is less than 0 then the two complex solutions
will be output. Thus:
x=

4ac b 2
b
i
2a
2a

Include the code in your report with 3 sets of outputs. The 3 sets will be one that
produces two real solutions, one that produces only one real solution and one that
produces two complex solutions.

The discriminant = b2 4ac

Assignment E
1. The vector dot product is defined as:
a b = a1

a2

b1
a3 b2 = a1b1 + a2b2 + a3b3
b3

Create a program that inputs the elements of vectors a and b using your own function
get_array. The vectors should be stored as arrays.
The program should then output the result of the dot product using another user
defined function print_array and dot_product. Include the code (with definitions of
get_array and dot_product) in your report with the output when a and b are derived
from your ID such that if your ID is 147892 then:
1
a = 4 and b =
7

8
9

2

2. The vector cross product is defined as follows:

i
a b = a1
b1

j
a2
b2

k
a3 = a2b3i + a3b1j + a1b2k a3b2i - a1b3j - a2b1k
b3

Modify the program in part 1 above, so that the output is now a vector cross product
calculated by a function vector_product. The output should also be displayed by a
user defined function print_array.

Assignment F
1. A factorial is given by:
n! = 1 2 (n 1) n
Write a non-recursive function factorial that takes n as the input and returns the
result n! Return the result as a float so that large values can be considered. In your
report include code using the function and the output with the last two digits of your
ID as n (i.e. if ID = 147892, n = 92). Include outputs for 2 other values of n. Note
that 0! = 1.
2. The number of ways you can choose k elements from n, C nk is:
C nk =

n!
k!(n k )!

Reuse the function factorial from part 1 above and use the last two digits of your ID
as n as before. Use the third last digit as k (if ID = 147892, k = 8). Include this output
and the code in your report (you do not need to include the definition of the factorial
function).

n(n 1)...(n k 1)
. How does this implementation effect
k!
the output? Include the code for this implementation and the output using the
previous values (without definition of the factorial function as already given in part
1).
C nk can be re-written as

You might also like