Professional Documents
Culture Documents
CS/SEP2011/CSC138
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
CS/SEP2011/CSC138
A. B. C. D.
}
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
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 ] ; ; ; ;
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
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;
CONFIDENTIAL
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)
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)
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)
CONFIDENTIAL
CONFIDENTIAL QUESTION 4
CS/SEP2011/CSC138
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)
CONFIDENTIAL
CONFIDENTIAL
CS/SEP 2011/CSC138
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)
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
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 .
- I**--**;**!*
T o t a l Hours 11 8 9
|'I"I1P'",WI jsummti
P o s i t i o n Code A B B
: 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)
CONFIDENTIAL