You are on page 1of 10

CONFIDENTIAL

CS/SEP2011/CSC138

UNIVERSITI TEKNOLOGI MARA FINAL EXAMINATION

COURSE COURSE CODE EXAMINATION TIME

STRUCTURED PROGRAMMING CSC138 SEPTEMBER 2011 3 HOURS

INSTRUCTIONS TO CANDIDATES 1. This question paper consists of three (3) parts : PART A (10 Questions) PART B ( 4 Questions) PART C ( 2 Questions)

2. 3. 4.

Answer ALL questions in the Answer Booklet. Start each answer on a new page. Do not bring any material into the examination room unless permission is given by the invigilator. Please check to make sure that this examination pack consists of: i) ii) the Question Paper an Answer Booklet - provided by the Faculty

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


This examination paper consists of 10 printed pages
Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

CONFIDENTIAL PART A (20 MARKS)

CS/SEP2011/CSC138

1. Which of the following are INCORRECT initializations of two-dimensional arrays?


I. II. III. IV. i n t a[2] [ ] = {0}; i n t a [ 2 ] [ 2 ] = { { l , 2 } , { 3 , 4 }} ; f l o a t a [ 2 . 5 ] [ 2 . 0 ] = { { l , 2 } , { l , 2 }} ; f l o a t a [ 2 ] [3] = { 3 , 6 , 1 , 8 , 2 , 1 , 8 } ;

A. B. C. D.

I and II I, II and III. I, lll.and IV I and IV

For questions 2 and 3, refer the following program segment:


int a;

i n t a r r a y [ 2 ] [3] = { { 2 , 3 , 1 } , { 6 , 7 , 9 } } ; f o r ( i n t i = 0; i < 2; i++ ) {


a = 0; f o r ( i n t j = 0; j < 3 ; j + + ) { i f ( a r r a y [ i ] [ j ] % 3 = = 0) a++; } c o u t << a << " " /

}
2. What is the purpose of this program segment? A. B. C. D. To To To To sum the numbers can be divisible by 3 for each row sum the numbers can be divisible by 3 for each column count the numbers can be divisible by 3 for each row count the numbers can be divisible by 3 for each column

3. What is the output for the above program segment?


A. B. C. D. 2 l 1 2 3 3 l l

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/SEP2011/CSC138

4. An array that will hold 5 names with maximum 20 characters for each name would be declared using which of the following statement?
A. B. C. D. char char char char names names names names [5] [ 2 0 ] [2 0] [5] [ 2 1 ] [5] [5] [ 2 1 ] ; ; ; ;

5. What does the following statement refer to?


my_struct[2] .value

A. B. C. D.

The third element of the array value. The second member of m y _ s t r u c t which is called value. The third component in the structure array m y _ s t r u c t and its member value. None of the above.

6. What is the output for the given program segment? struct { int int int a second; minute; hour;

};
void main() { a b = { 3, 6, 4 }; b.second = b.second * 4; int c = b.minute; b.hour = c * 2 ; cout << b.second << " " << c << " " < < b.hour;

}
A. B. C. D. 12 12 12 6 12 12 12 6 12 12 6 6

7. The and decrement operations on pointer variable. A. addition, subtraction B. modulus, division C. + + 1 - D. All of these
Hak Cipta Universiti Teknologi MARA

operators can be used for increment or

CONFIDENTIAL

CONFIDENTIAL

CS/SEP 2011/CSC138

8. What will be the output for the following program segment? int *numbers = new int [5] ; for (int i = 0 i c= 4; i++) * (numbers + i) == i; cout << numbers[2] << endl; A. B. C. D. 0 3 2 1

9. Which of the following header file is necessary for file input/output? A. B. C. D. #include #include #include #include <fstream> <iomanip> <cstdlib> <fileIO>

10. Which of the following is the correct way to write data to a file? A. ofstream outfile; outfile.open("data.txt"); output >> "CSC13 8" >> " " >> "A" >> endl; B. fstream outfile; outfile.open("data.txt"); output << "CSC138" << " " << "A" << endl; C. ofstream outfile; outfile.open("data.txt"); outfile << "CSC138" << " " << "A" <<endl; D. ifstream outfile; outfile.open("data.txt") ; outfile << "CSC138" << " " << "A" <<endl;

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL PART B (50 MARKS)

CS/SEP2011/CSC138

QUESTION 1 a) Trace the above program and determine the final output. const int ROW = 3; const int COL = 4; void mystery (int myst [] [COL], int row)

{
for (int a=0; a<row; a++)

{
for (int b=0; b<2; b++) myst [a] [COL - 1] += myst [a] [b] ;
} }

void main()

{
int myst[ROW] [COL]={{4,8,3,7},{1,9,5,2},{10,6,4,2}}; mystery(myst,ROW); for(int i=0; i<ROW; i++)

{
f o r ( i n t j = 0 ; j<COL; j++) cout<<myst [i] [j]<<" " ; cout<<endl;
}

J
(3 marks) b) Given a two-dimensional array matrix:

16 3 26 1
i)

8 12 16 42

11 7 22 1

28 11 4 6

Write a function definition named sumByRow to calculate and display the sum of the elements for each row of the matrix. (5 marks)

ii) Write a function definition named d i v i s i b l e B y F o u r to count and return number of elements that is divisible by four (4). (5 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL QUESTION 2

CS/SEP2011/CSC138

a) The following program segment contains THREE syntax errors. Rewrite the correct program codes. #include<iostream> Struct course{ int course_id; int creditHours; char lecturersName[5] [30] ;

}
void main()

{
Course cl; cin>>cl.course_id; cin>>cl.creditHours; cin>>cl.lecturersName;

J
(3 marks)
b) Given the following structure definition and function prototypes: struct BagType

{
char style[2 0]; char brand [3 0] ; double price; int count;

};
void readBagRecord(BagType& newBag); BagType discountBag(BagType oldRecord); void displayRecord(BagType newRecord) ; Write the definition for the following functions: i) readBagRecord: This function will input all the data for variable newBag of type BagType. (5 marks)

ii) discountBag: This function will return a structure with the same value as its arguments where the price reduced by 25%. (4 marks) iii) d i s p l a y R e c o r d : This function will display all the data stored in variable newRecord. (3 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/SEP2011/CSC138

QUESTION 3 a) Determine the output for the following program : void main()

{
int arrSize =5 ; int *ptrA, *ptrB; int j, count, value = 1 ; ptrA = new int[arrSize]; for(j = 0; j < arrSize; j++){ ptrA[j] = value + 1; value = value + ptrA[j] ;

}
cout << "Value for ptrA:\n"; for(j = 0 ; j < arrSize; j++) cout << ptrA[j]<<" \n" ; ptrB = ptrA; for(count = 0; count < arrSize; count++) ptrB[count] = ptrB[count] + count; cout << "\nValue for ptrB:\n"; for(j = 0; j < arrSize; j++) cout << ptrB[j]<<" \n" ;

J
(4 marks)
b) Given the following program segment: void main()

{
int *marks, n; cout << "Enter number of students: "; c m >> n; marks = new int [n] ;

}
Write C++ statements that do the following: i) Read and store the scores for n students in the array. (2 marks) ii) Determine and display the highest score in the array. (5 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL QUESTION 4

CS/SEP2011/CSC138

Suppose that the file i n D a t a . t x t contains the following data:


10.20 15.60 16.12
5.35 8.20 7.28

The numbers in each line represents the length and width of a rectangle. Write C++ statements that will do the following tasks: a) Declare i n F i i e to be an ifstream variable and o u t F i l e to be an ofstream variable. (2 marks)

b) Open both files, associate inFiie with inData. t x t and associate outFile with
outData.txt.
(2 marks) c) Calculate the area of the rectangle by reading the data in each line and store the results

in o u t D a t a . t x t .
(5 marks) d) Close i n F i i e and o u t F i l e . (2 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/SEP 2011/CSC138

PART C (30 MARKS)

QUESTION 1 Given the definition of computer record and declaration of computers array: struct Computer

{
char brand[20]; double price; int operating; // 1- for L i n u x a n d 2 f o r W i n d o w s

};
void main()

{
Computer computers[20] [4] ; // // // // // // There are 4 types: 0 for desktop/PC, 1 for notebook, 2 for netbook and 3 for tablet PC. Each type has 20 units

}
Write a complete C++ program by using modular programming techniques that perform each of the following tasks (one function for each task): a) Read data into the computers array. b) Display the brand and price of the most expensive computer under the netbook type. c) Display all brands of computers with Windows as their operating system (15 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

10

CS/SEP 2011/CSC138

QUESTION 2 The input text file named t i m e c a r d . t x t contains a list of worker's name, identification number, position code, and total hours of working for a working day. The data is for a specific day of the month as shown in Figure 1:
| timecard.txt - Notepad FHe Edit Format View Help
f m IHAMSSMIMJ

JMuiiaitOEtaci Airaaar M u b i n ; D M 1 0 8 2 0 0 5 A 1 1 K h a i r u l N i z a m A h m a d ; GM1234Q92 B 8 S i t i aminah Hassan;GK9802345 B 9


Figure 1: The data written in t i m e c a r d . t x t file * NOTE: The semicolon symbol (,-) is used as the delimiter. Each worker is paid based on the following table: Position Code A B Rate per hour (RM) 50 40

Working hours more than 8 hours is considered as overtime and will be paid 50% more than normal rate per hour. Example: A worker with position code of A' whose total hours of working is 11 hours will be paid RM50 multiply by 8 hours, plus RM75 multiply by 3 hours, that is RM400 + RM225 = RM625. The program will read data from the input text file and produce the report as shown in Figure 2 in the output text file named payment. t x t .

'fte entblt - No,e < Bd * N t i * * i r *


Fjte Edit Format View Help

- I**--**;**!*
T o t a l Hours 11 8 9

*<" ^K|lS >.,


J8P~

|'I"I1P'",WI jsummti

ID DM1082005 GM1234092 GK9802345 Total Total Total Total

P o s i t i o n Code A B B

Overtime RM225 RMO RM60

T o t a l Payment RM625 RM320 RM380

number o f w o r k e r s i s : 3 number of w o r k e r s w o r k i n g o v e r t i m e i s o v e r t i m e payment i s : RM 285 d a i l y payment i s : RM 1325

: 2

Figure 2: Sample report produced on payment. t x t file Write a complete C++ program to calculate and produce the report as shown in Figure 2 above. (15 marks)

END OF QUESTION PAPER


Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

You might also like