You are on page 1of 3

KL 1164 - Computer Programming (C)

Week 3 to 7 – Group 2
Lecturer: Dr. Ashrani Aizzuddin b. Abd. Rahni

Tutorial 5

Execise 1
Write a simple program to organise student marks. The Matric. No. (numeric part)
and marks of each student can be entered. Once all the data is entered it can either be
sorted according to Matric. No. or mark. Use the bubblesort() function given
below, but modify it so that it can rearrange a second array to preserve the match
between a Matric. No. and the student’s mark. The data can then be output according
to Matric No. or mark. Set a maximum number of 10 students to be entered including
your own Matric. No. Use only integer array(s). Show the output of the array in both
orders.

void bubblesort(int a[]) {


int pass, i;

for(pass = 1;pass < SIZE; pass++)


for(i=0;i<SIZE-1;i++)
if(a[i] > a[i+1])
swap(&a[i],&a[i+1]);
}

void swap(int *x, int *y) {


int temp;

temp = *x;
*x = *y;
*y = temp;
}

Exercise 2
A vector dot product for three element vectors is defined as follows:

 b1 
 
a  b = [a1 a2 a3] b2  = a1 b1 + a2 b2 + a3 b3
b3 

Create a program that inputs the elements of vectors a and b (using a user defined
“input array” function). The program should output the result of the dot product.
Include the output in your report when a and b are derived from your Matric. No.
such that if your Matric. No. is 144313 then:
1  3
4  
a =   and b = 1
4 3
Find the dot product for another value of a and b. Use only integer arrays.

Bonus
k
The values of C n (choosing k elements from n, refer to Tutorial 4) are known as the
binomial coefficients, and can be arranged in the following triangle known as Pascal’s
triangle:

k
n
0 1 2 3 4
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1

This triangle was first described in the 10th century by the Indian mathematician
Halayudha and the Persian engineer and mathematician Al-Karaji. The elements of
the triangle can be found from upper rows by the formula:

C nk = C nk11 + Cnk1

In a C program this can be done as follows:


 Declare c[max]
 c[0] = 1

Inside a function produce_row:


For row i:
 Store c[j] in prev[j] until j < i
 c[j] = prev[j – 1] + prev[j] from j = 1 until i – 1
 c[i] = 1

Iterate from i = 1 until i < n.

k
Write a program so that you can output C n with the value of k and n used in Tutorial 4.
Use the last two digits of your Matric. No. as n (i.e. if Matric. No. = 144313, n = 13)
and use the third last digit as k (if Matric. No. = 144313, k = 3). You do not need to
k
output other values of C n in the triangle. Test also for the second value of n and k used
in Tutorial 4. Include the code and its output in your report. How does the output
compare with the outputs in Tutorial 4?

Submission
Submit your report (as word document with your matric no. e.g. Axxxxx.doc) and .c
files (save as ex1.c, ex2.c and bonus.c) together in a zip/rar file (your matric no. e.g.
Axxxxx.rar) and upload in iFolio. Submit on the first Monday after the mid semester
break (15/04/19).

You might also like