You are on page 1of 6

KOLEJ YAYASAN PELAJARAN JOHOR

UJIAN 2 : BAB 5 (ANSWER SCHEME)

SESI II SEMESTER 2019/2020


KOD KURSUS : DDWC 1603
NAMA KURSUS : C++ PROGRAMMING
PROGRAM / SEM : DIPLOMA SAINS KOMPUTER(TEKNOLOGI
MAKLUMAT)
TEMPOH : 2 JAM
TARIKH : FEBRUARI 2020

ARAHAN

Jawab SEMUA soalan.


Ujian ini menyumbang sebanyak 5% daripada jumlah markah kerja kursus.

MARKAH
MARKAH PENUH
SOALAN CO PO/TAXANOMY PELAJAR
M % M %
Bahagian A CO1 PO1/C3 5 0.6
Bahagian B
Soalan 1 CO1 PO1/C3 10 1.25
Soalan 2 CO1 PO1/C3 10 1.25
Soalan 3 CO1 PO1/C3 15 1.9

JUMLAH ___M ___ % 40M 5.0 %

NAMA PELAJAR : …………………………………………………………………….


NO MATRIK : …………………………………………………………………….
SEKSYEN : …………………………………………………………………….
TARIKH : …………………………………………………………………….
NAMA PENSYARAH : NORYANTI BINTI HUSSIN / NURUL AMIRAH BINTI
MAMAT/ AZIHANAFI BIN MOHD DAKIR
Kertas ini terdiri daripada 5 mukasurat termasuk mukadepan
SECTION A: TRUE / FALSE [5 MARKS]
BAHAGIAN A: BETUL/SALAH [5 MARKAH]

1. Statement calculateNum (int num1, int num2, int num3); is a illegal function call.
Pernyataan calculateNum ( int num1, int num2, int num 3); adalah satu panggilan fungsi yang tidak sah.

2. Void is used when the function does not return a value.


Void digunakan apabila fungsi tidak mengembalikan nilai.

3. Pow (2,3) is the calling function to calculate 2 * 2 * 3?


Pow (2,3) adalah panggilan fungsi untuk mengira 2 * 2 * 3?

4. A reference variable which can be used as a function parameter to reference the original variable.
A pembolehubah rujukan yang boleh digunakan sebagai parameter fungsi untuk merujuk kepada
pemboleh ubah asal.

5. Pass-by-value is to pass the value of the argument to the parameter and pass-by-reference is to
pass the reference of the argument.
Pass-by-value adalah untuk lulus nilai argumen kepada parameter dan pass-by-reference adalah untuk
lulus rujukan hujah.
es

Question/Soalan Answer/ Jawapan


1 F
2 T
3 F
4 T
5 T

SECTION B: STRUCTURE [35 MARKS]


BAHAGIAN B: STRUKTUR [ 35 MARKAH]
Question 1 / Soalan1

(a) What are the output will be produced by the following code? [4 M]
Apakah output yang akan di hasilkan oleh kod berikut?

int myFunction(int n) {
int sum=0, i=n;
while( i > 2 ) {
sum = sum + i ;
i = i - 3;
}
}
a) cout << myFunction (8);
b) cout << myFunction (1);

Answer / Jawapan:
a) 5
b) 1

(b) What is the output of the following code?


Apakah output yang akan dihasilkan oleh kod berikut? [3M]

void myProgramming () {
cout << “I Love C++ Programming”;
}
Int main () {
myProgramming();
myProgramming();
myProgramming();
}

Answer/Jawapan:

I Love C++ ProgrammingI Love C++ ProgrammingI Love C++ Programming

(c) What are the output will be produced by the following code? [3M]
Apakah output yang akan dihasilkan oleh kod berikut?

void value(double &price) {


price++;
}
int main() {
double x = 1;
double y = 5;
value(x);
value (y);
cout << "X is "<< x << " and Y is "<< y << endl;
}

Answer/Jawapan;

X is 2 and Y is 6
Question 2 / Soalan 2
Based on the given answer, write a function that prints the following output. [10M]
Berdasarkan pada jawapan yang diberi, tulis fungsi untuk menghasilkan jawapan tersebut.
Output / Hasil:

Enter x, y and z separated by space: 2 4 6

The max value is 6

Answer / Jawapan:

#include<iostream>
#include<conio.h>
using namespace std;
int max3(int x,int y,int z);
int main(){
int x,y,z,max;
cout<<"Enter x, y and z separated by space:";
cin>>x>>y>>z;
max=max3(x,y,z);
cout<<"The max value is "<<max;
getch();
return 0;
}

int max3(int x,int y,int z){


if(x>y)
if(x>z) return x;
else return z;
else if(y>z) return y;
else return z;
}
Question 3 / Soalan 3
Write a complete program to calculate and display Body Mass Index ( BMI). Your program must involve
function(refer a diagram below) [15M]
Tulis program lengkap untuk mengira dan memaparkan Indeks Mass Tubuh (BMI). Program anda mesti
melibatkan fungsi (rujuk gambarajah di bawah).

main

calculateBMI displayBMI

Function main() will ask user to input height in meter and weight that will be sent to calculateBMI.
Function calculateBMI will receive height and weight and also calculate BMI using
formula = weight / ( height * height ).
Function displayBMI will receive the result and display it.
Fungsi main() akan meminta pengguna memasukkan ketinggian dalam meter dan berat yang akan
dihantar untuk calculateBMI.
Fungsi calculateBMI akan menerima tinggi dan berat dan juga mengira penggunaan BMI
formula = berat / (tinggi * tinggi).
Paparan displayBMI akan menerima hasil dan memaparkannya.

Answer / Jawapan;
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;

double calculateBMI (double weight, double height) {


double bmi= (weight)/(height * height);
return bmi;
}

void displayBMI (double bmi){


cout<<"\n\n\tBMI VALUES \n ";
cout<<"Underweight : less than 18.5";
cout<<"\n Normal : between 18.5 and 24.9";
cout<<"\n Overweight: between 25 and 29.9";
cout<<"\n Obese : 30 or greater" << endl;

cout << "\nYour BMI value is " << bmi << endl;
}
int main()
{
double weight, height, bmi;

cout << "Enter your weight in kg: ";


cin >> weight;
cout << "Enter your height in meter: ";
cin >> height;

calculateBMI(weight, height);

displayBMI(calculateBMI(weight, height));

return 0;
}

You might also like