Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CONFIDENTIAL CD/FEB 2023/CSC404

UNIVERSITI TEKNOLOGI MARA


FINAL EXAMINATION

COURSE PROGRAMMING II
COURSE CODE CSC404
EXAMINATION FEBRUARY 2023
TIME 3 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of two (2) parts: PART A (4 Questions)


PART B (2 Questions)

2. Answer ALL questions in the Answer Booklet. Start each answer on a new page.

3. Do not bring any material into the examination room unless permission is given by the
invigilator.

4. Please check to make sure that this examination pack consists of:

i) the Question Paper


ii) an Answer Booklet - provided by the Faculty

5. Answer ALL questions in English.

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 7 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 CD/FEB 2023/CSC404

PART A

QUESTION 1

a) The following program segment contains three (3) errors. Rewrite the correct program
segment.

i n t number;
i n t *p;
s t r i n g player="Mokhtar D a h a r i " ;
p = namel;
cout«&p; / / d i s p l a y t h e a d d r e s s of a p l a y e r u s i n g a p o i n t e r
c o u t « " t h e a d d r e s s of p i s " « & p ; / / d i s p l a y t h e a d d r e s s of p

(5 marks)

b) Answer the following questions.

i) Write a function named getMarks() to prompt user to input marks for 5 subjects. The
function uses only 1 variable named marks and for loop statement to input and find
the average of marks obtained by the students. Then, the average of marks is
returned to the calling function using passing by value method.

(5 marks)

ii) Based on Table 1, write a function named d i s p l a y R e s u l t () to display the


averageMarks calculated in b i ) that sent to the function through its parameter.

Table 1: Average Marks

Average Marks Result


More than 80 Excellent
More than 50 Good
Less than 51 Failed

(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CD/FEB 2023/CSC404

iii) The following program segment is used to call the functions named getMarks ()
and d i s p l a y R e s u l t ( ) . However, the program segment contains errors. Find and
correct the errors.

#include <iostream>
using namespace std;
void getmarks(float&) ;
void displayResult(int & ) ;
int main ()
{
float average;
getMarks();
displayResult(&average) r
cout<<"The average is"«average;
}

(5 marks)

QUESTION 2

Given the following table that stores the marks of 3 courses for 3 students:

Table 2: Student Marks

Student Name CSC404 CSC405 CSC406


Najwa 68 87 93
Hajar 94 52 65
Laila 80 57 63
Based on Table 2, answer the following questions.

a) Create a two-dimensional array named score [ ] [ ] to store the marks of CSC404,


CSC405 and CSC406 for Najwa, Hajar and Laila.

(2 marks)

b) Next, find the total marks for each student and store them in 1 dimensional array named
t o t a l [ ] . Display all values in t o t a l [ ] .

(8 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CD/FEB 2023/CSC404

QUESTION 3

a) Table 3 listed the amount spend per day for food of five students in a week. For example,
for day one, Student 1 spend RM90, while for the same day, Student 2 spend RM35
and so on as shown in the Table 3.

Table 3: Amount spend for a week

Student 1 Student 2 Student 3 Student 4 Student 5


Day 1 90 35 60 40 90
Day 2 80 40 35 90 60
Day 3 70 55 40 60 80
Day 4 50 70 65 45 70
Day 5 95 35 61 48 38
Day 6 82 40 38 20 65
Day 7 75 55 45 50 77

i) Declare and initialize a two-dimensional array to store the amount spend as in Table
3.

(2 marks)

ii) Write a program segment to compute and display the average spending by each
day.

(4 marks)

iii) Write a program segment to find and display the day with the highest spending for
the third person. The output should look like as the following example:

The highest spending for the third person is RM XXX on Day XXX

(4 marks)

b) Given the following declaration:

int setA[6] = {6,5,2,3,4,9};


int setB[6] = {36,20,4,9,18,25};

Write a function definition named muisame ( ) that receives both arrays. The function
compares each element of both arrays at the same index. If both values are even,
multiply both values and store into an array named Group. Then, display the contents
of the array Group.

(10 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CD/FEB 2023/CSC404

QUESTION 4

a) Given the following struct definition:

struct Books
{ string title;
float price;
string publisher;
};

i) Write a function named inputBookDataO that asks the user to enter all information
of a book.

(4 marks)
ii) Given the declaration:

Book b o o k l ;

Write the code that calls function inputBookData () for b o o k l .

(1 mark)

b) The following struct array is used to represent lecturers information:

struct Lecturer
{ string name;
double salary;
int university; // codes used: 1-UiTM, 2-UKM
};
Lecturer people[MAX];

Assuming the array already contains data, write the codes to display the list of UiTM
lecturers and UKM lecturers separately, then calculate and compare the average
salary of lecturers from the 2 universities to see which university has the higher
average salary.

(15 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CD/FEB 2023/CSC404

PARTB

QUESTION 1

Answer the following questions.

a) Write a program in C++ to declare two dynamic integer variables named numl and
num2. Input a number into each numl and num2. Find and display the sum of the
numbers.

(5 marks)

b) Given a C++ statement:

string text[]= {"a","b","c","d'\ "e"};

Write a program in C++ to use the string t e x t to:

i) Declare a string pointer variable named pText. Set the value of pText to the
address of t e x t . Use the pointer pText to do the operations in Question 1 b) ii and
b) iii.

ii) Use a loop to count and display how many strings "a" in string t e x t .

iii) Use a loop to determine and display if there are any vowels in string t e x t . Vowels
are letters a, e, i, o and u.

(10 marks)

QUESTION 2

a) Write C++ statement(s) to declare an integer array named marks with size 30 and an
integer pointer variable named pMarks. Set the value of pMarks to the address of marks.
Using a loop, store marks values into the array using pointer pMarks.

(5 marks)

b) Write the definition of function f i n d H i g h e s t M a r k s ( i n t * a r r a y , i n t s i z e ) that


receives the array in Question 2a. The function finds and returns the highest marks.

(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 CD/FEB 2023/CSC404

c) Write C++ statements to find and display the grade from the array in 2a. The grade is
determined based on Table 4.
Table 4: Grade

Marks Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F

(5 marks)

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like