UAS 23-24 FAC1003 Programming II
--- Page 1 ---
UNIVERSITI MALAYA
UNIVERSITI MALAYA
PEPERIKSAAN ASASI SAINS FIZIKAL
EXAMINATION FOR FOUNDATION IN PHYSICAL SCIENCE
SESI AKADEMIK 2023/2024 : SEMESTER 2
ACADEMIC SESSION 2023/2024 : SEMESTER 2
FAC 1003 : Pengaturcaraan II
Programming II
Mei/Jun 2024
May/June 2024
Masa: 2 jam
Time: 2 hours
ARAHAN KEPADA CALON :
INSTRUCTIONS TO CANDIDATES :
Calon dikehendaki menjawab semua soalan.
Candidate is required to answer all questions.
[University crest: shield bearing two tigers, a hibiscus flower, and a lion, with banner reading "ILMU PUNCHA KEMAJUAN"]
(Kertas soalan ini mengandungi 6 soalan di dalam 8 halaman bercetak)
(This question paper consists of 6 questions on 8 printed pages)
--- Page 2 ---
FAC 1003
BAHAGIAN A / PART A
1. (a) Pengisytiharan tatasusunan Pasum ialah:
cout<<"Input size of layer, row and column";
cin>> x >> m >> n;
Pasum[x][m][n];
Isi ruang kosong bagi segmen kod dalam C++ dibawah yang membolehkan pengguna memasukkan nilai setiap elemen dalam Pasum dan mencetak elemen tersebut dalam bentuk matrik.
cout<<"enter the values in the array: \n";
for(___(i)_______)
{
for(_____(ii)________)
{
for(_____(iii)_________)
{
cout<<"Value at arr["<<i<<"]["<<j<<"]["<<k<<"]: ";
cin>> _____(iv)_________;
}
}
}
cout<<"\nprinting the values in array C: \n";
for(___(v)________)
{
for(___(vi)_________)
{
for(___(vii)_________)
cout<<_____(viii)_____<< " ";
_______(ix)_______;
}
________(x)__________;
}
(b) Pengisytiharan pembolehubah dan tatasusunan 2D diberi seperti di bawah:
int FX[2][3] ;
int T;
(i) Tuliskan kod untuk mengistiharkan petunjuk FFX kepada tatasusunan FX?
(ii) Apakah kod untuk mewakilkan T sebagai hasil tambah 100 dan FX dengan petunjuk?
(iii) Apakah kod untuk mencetak nilai FX dengan menggunakan petunjuk?
2/8
--- Page 3 ---
FAC 1003
(a) The Pasum array declaration is given as below:
cout<<"Input size of layer, row and column";
cin>> x >> m >> n;
Pasum[x][m][n];
Fill in the blanks for code segment in C++ that user can input value on each element in Pasum and print the elements in a matrix.
cout<<"enter the values in the array: \n";
for(___(i)________)
{
for(_____(ii)__________)
{
for(_____(iii)__________)
{
cout<<"Value at arr["<<i<<"]["<<j<<"]["<<k<<"]: ";
cin>> _____(iv)_________;
}
}
}
cout<<"\nprinting the values in array C: \n";
for(___(v)__________)
{
for(___(vi)__________)
{
for(___(vii)__________)
cout<<_____(viii)_____<< " ";
_______(ix)________;
}
_________(x)__________;
}
(b) Declaration of variable and pointer are given as follows:
int FX[2][3] ;
int T;
(i) Write a code to declare pointer FFX to array FX.
(ii) What code is used to assign T as addition of 100 and FX[0][0] by pointer?
(iii) What is the coding to print the value of FX by the pointer?
(10 markah/marks)
3/8
--- Page 4 ---
FAC 1003
#include <iostream>
Using namespace std;
___(a)___ calculateSum(___(b)___, ___(c)___) {
a = a + b; // Modify 'a' to store the sum
}
___(d)___ calculateProduct(___(e)___, ___(f)___) {
a = a * b; // Modify 'a' to store the product
}
int main()
{
int num1, num2;
// Prompt the user to enter two integers
cout<<"Enter the first integer: ";
cin>>num1;
cout<<"Enter the second integer: ";
cin>>num2;
// Calculate the sum
calculateSum(___(g)___, ___(h)___);
cout<<"\nSum of "<<num1<<"and "<<num2 <<"is: "<<num1 <<endl;
// Reset num1 to the original value for calculating product
int originalNum1 = num1;
// Calculate the product
calculateProduct(___(i)___, ___(j)___);
cout<<"Product of "<<originalNum1 <<" and "<<num2 <<" is: "
<<num1 <<endl;
return 0;
}
Rajah 1 / Figure 1
Lengkapkan kod C++ di dalam Rajah 1, yang mengira jumlah dan hasil darab dua integer yang dimasukkan oleh pengguna. Kod tersebut mengandungi dua fungsi berasingan, calculateSum() dan calculateProduct(), yang masing-masing mengambil rujukan kepada dua integer tersebut sebagai parameter dan mengira jumlah dan hasil. Keputusan dipaparkan dalam fungsi main().
Complete the C++ code in Figure 1 that calculates the sum and product of two integers entered by the user. The code consists of two separate functions, calculateSum() and calculateProduct(), which take references to the two integers as parameters and calculate the sum and product, respectively. The results are displayed in the main() function.
(10 markah/marks)
4/8
--- Page 5 ---
FAC 1003
BAHAGIAN B / PART B
3. Tulis sebuah program C++ untuk memasukkan integer ke dalam matriks berukuran $3 \times 3$. Hitung jumlah elemen yang merupakan sama ada gandaan 5 atau 8 dan paparkan jumlah elemen tersebut. Program perlu memaparkan matriks asal di dalam bentuk matriks. Kemudian, ubahsuai tatasusunan 2D kepada tatasusunan 1D. Paparkan tatasusunan 1D tersebut.
Write a C++ program to input integers in a matrix of size $3 \times 3$. Calculate the sum of elements that is multiple of either 5 or 8 and display the sum of those elements. The program must display te original matrix in a matrix form. Then, modify the 2D array to 1D array. Display the 1D array.
(15 markah/marks)
4. Tulis sebuah program C++ yang boleh memasukkan nama pelajar dan markah subjek pengaturcaraan mereka ke dalam tatasusunan 1D. Saiz dimensi tatasusunan bergantung kepada bilangan pelajar di dalam kelas. Semua elemen tatasusunan 1D dimasukkan oleh pengguna. Kemudian sistem akan menentukan gred bagi markah setiap pelajar (rujuk Jadual 1). Sistem perlu memaparkan nama dan markah pelajar, dan mencetak output yang serupa dengan Rajah 2. Sila gunakan petunjuk untuk setiap tatasusunan dan pemboleh ubah.
[Table: Jadual 1 / Table 1]
| Gred/Grade | Markah/Mark | Huraian Gred/Grade Description |
|---|---|---|
| A | 75 - 100 | Cemerlang / Distinction |
| B | 60 - 74 | Kepujian / Credit |
| C | 50 - 59 | Lulus / Pass |
| D | 40 - 49 | Lulus / Pass |
| E | 30 - 39 | Gagal / Fail |
| F | 0 - 29 | Gagal / Fail |
[Figure: Rajah 2 / Figure 2 — tabular output]
NAME MARK GRADE
---- ---- -----
Abu 89 A
Aminah 72 B
Ali 55 C
Liu 85 A
5/8
--- Page 6 ---
FAC 1003
Write a C++ system that can input student names and their programming subject marks in a 1D array. The dimension size of array is depending on the numbers of students in a class. All of the elements in the 1D array are input by the user. Then, the system will determine the grade of each student's score (refer Table 1). The system needs to display the student names and their respective score, and prints an output similar to Figure 2. Please use pointers for each array and variable.
(15 markah/marks)
5. Tulis sebuah program C++ yang menggunakan penggunaan aplikasi fungsi untuk mengira rumus seperti di bawah:
$$d = \frac{|ah + bk + c|}{\sqrt{a^2 + b^2}}$$
Program ini mesti memasukkan tiga fungsi bernama PowerTwo, MultiplyTwo dan FindD. PowerTwo ialah fungsi non-void yang mengira kuasa 2 ($a^2$ atau $b^2$), MultiplyTwo juga fungsi non-void untuk mengira pendaraban 2 pemboleh ubah ($ah$ atau $bk$) dan FindD adalah fungsi void yang mencari nilai $d$ dan mencetaknya di dalam fungsi.
Write a program that uses the application of functions to compute the formula as below:
$$d = \frac{|ah + bk + c|}{\sqrt{a^2 + b^2}}$$
The program must include three functions named PowerTwo, MultiplyTwo and FindD. The PowerTwo is a non-void function which calculates the power of 2 ($a^2$ or $b^2$), MultiplyTwo is also non-void function to calculate multiplication of 2 variables ($ah$ or $bk$) and FindD is the void function that finds the value of $d$ and print in the function.
(15 markah/marks)
6/8
--- Page 7 --- FAC 1003
- Tulis program C++ yang menggunakan rekursi untuk menjana siri Fibonacci sehingga had yang ditentukan. Siri Fibonacci ialah jujukan nombor di mana setiap nombor ialah hasil tambah dua nombor sebelumnya, biasanya bermula dengan 0 dan 1. Tuliskan fungsi rekursif
fibonacciuntuk mengira siri Fibonacci. Fungsi harus mengambil parameter integer $n$, mewakili had sehingga siri Fibonacci dijana.
Program anda hendaklah termasuk:
- Fungsi rekursif
fibonacciyang mengira dan mencetak siri Fibonacci sehingga had yang ditentukan. - Pengendalian ralat yang betul untuk memastikan bahawa had adalah integer bukan negatif. Kendalikan kes di mana hadnya ialah 0 atau 1 secara berasingan.
- Demonstrasi program dengan mengambil input pengguna untuk had dan mengeluarkan siri Fibonacci.
Rajah 3 menunjukkan contoh input/output program.
[Figure 3: rectangular box containing sample program output] Enter the limit for Fibonacci series: 8 Fibonacci series up to 8: 0 1 1 2 3 5 8 13 [End of box] Rajah 3
Write a C++ program that uses recursion to generate the Fibonacci series up to a specified limit. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. Write a recursive function fibonacci to calculate the Fibonacci series. The function should take an integer parameter $n$, representing the limit up to which the Fibonacci series is generated.
Your program should include:
- The recursive function
fibonaccithat calculates and prints the Fibonacci series up to the specified limit. - Proper error handling to ensure that the limit is a non-negative integer. Handle cases where the limit is 0 or 1 separately.
- A demonstration of the program by taking user input for the limit and outputting the Fibonacci series.
7/8
--- Page 8 --- FAC 1003
Figure 3 shows sample input/output of the program.
[Figure 3: rectangular box containing sample program output] Enter the limit for Fibonacci series: 8 Fibonacci series up to 8: 0 1 1 2 3 5 8 13 [End of box] Figure 3
(15 markah/marks)
TAMAT END
8/8
Verbatim transcription via Kimi K2.6 vision subagents.