CSC138 Sept 2011

download CSC138 Sept 2011

of 10

Transcript of CSC138 Sept 2011

  • 8/23/2019 CSC138 Sept 2011

    1/10

    CONFIDENTIAL CS/SEP2011/CSC138

    UNIVERSITI TEKNOLOGI MARAFINAL EXAMINATION

    COURSECOURSE CODEEXAMINATIONTIME

    STRUCTURED PROGRAMMINGCSC138SEPTEMBER 20113 HOURS

    INSTRUCTIONS TO CAN DIDATES1 . This question paper consists of three (3) parts : PART A (10 Questions)PART B ( 4 Questions)PART C ( 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 exam ination room unless permission is given by the invigilator.4. Please check to make sure that this examination pack consists of:

    i) the Question Paperii) an Answ er Booklet - provided by the Faculty

    DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SOThis examination paper cons ists of 10 printed pages

    Hak Cipta Universiti Teknologi MARA C O N F ID E N T IA L

  • 8/23/2019 CSC138 Sept 2011

    2/10

    CONFIDENTIAL 2 CS/SEP2011/CSC138

    PART A (20 MARKS)

    1. W hich of the following are INCORRECT initializations of two-dimensional arrays?I. i n t a [ 2 ] [ ] = { 0 } ;II. i n t a [ 2 ] [ 2 ] = { { l , 2 } , { 3 , 4 } } ;III. f l o a t a [ 2 . 5 ] [ 2 . 0 ] = { { l , 2 } , { l , 2 } } ;IV. f l o a t a [ 2 ] [3 ] = { 3 , 6 , 1 , 8 , 2 , 1 , 8 } ;

    A. I and IIB. I, II and III.C. I, lll.and IVD. I and IV

    For questions 2 and 3, refer the following program segment:i n ti n tf o r{

    }

    a ;a r r a y [ 2 ] [3 ]

    ( i n t i = 0 ;a = 0 ;

    = { { 2 ,i < 2 ;

    f o r ( i n t j = 0 ; j{ i f ( a r r a y [ i ]}c o u t

  • 8/23/2019 CSC138 Sept 2011

    3/10

    CONFIDENTIAL 3 CS/SEP2011/CSC138

    4. An array that will hold 5 names with maximum 20 characters for each name would bedeclared using which of the following statement?A . c h a r n a m e s [ 5 ] [ 2 0 ] ;B . c h a r n a m e s [ 2 0 ] [ 5 ] ;C . c h a r n a m e s [ 2 1 ] [ 5 ] ;D . c h a r n a m e s [ 5 ] [ 2 1 ] ;

    5. Wh at does the following statement refer to?m y _ s t r u c t [ 2 ] . v a l u e

    A. The third element of the array value.B. The second member of m y _ s tr u c t which is cal led value.C. The third component in the structure array m y _ s tr u c t and its member value.D. None of the above.

    6. Wh at is the output for the given program segment?struct a{ int second;

    int minute;int hour;};void main(){ a b = { 3, 6, 4 };

    b.second = b.second * 4;int c = b.minute;b.hour = c * 2 ;cout

  • 8/23/2019 CSC138 Sept 2011

    4/10

    CONFIDENTIAL 4 CS/SEP 2011 /CSC138

    8. What will be the output for the following program seg ment?intfor *cout

    *numbers(int i =(numbers

    =0+

    newi

    i) = "CSC13 8" >> " " >> "A" >> endl;B. fstream outfile;

    outfile.open("data.txt");output

  • 8/23/2019 CSC138 Sept 2011

    5/10

    CONFIDENTIAL 5 CS/SEP2011/CSC138

    PART B (50 MARKS)

    QUESTION 1a) 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

  • 8/23/2019 CSC138 Sept 2011

    6/10

    CONFIDENTIAL 6 CS/SEP2011/CSC138

    QUESTION 2a) The following program segment contains THREE syntax errors. Rewrite the correctprogram codes.

    #includeStruct 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) re ad B ag R ec or d: This function will input all the data for variable newBag of type

    BagType. (5 marks)ii) d is c o u n tB a g : This function will return a structure with the same value as itsarguments where the price reduced by 25%. (4 marks)i ii ) d is p la y R e c o r d : This function wi ll display al l the data stored in variablenewRecord . (3 marks)

    Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

  • 8/23/2019 CSC138 Sept 2011

    7/10

    CONFIDENTIAL 7 CS/SEP2011/CSC138

    QUESTION 3a) 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

  • 8/23/2019 CSC138 Sept 2011

    8/10

    CONFIDENTIAL 8 CS/SEP2011/CSC138

    QUESTION 4Suppose that the f ile i n D a t a . t x t contains the following data:

    10.2015.6016.12

    5.358.207.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 i fstream variable and o u t F i l e to be an ofstream variable.(2 marks)b) Open both files, associate i n F i i e with inD at a. tx t and associate ou t F i l e witho u t D a t a . t x t . (2 marks)c) Calculate the area of the rectangle by reading the data in each line and store the resultsin 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

  • 8/23/2019 CSC138 Sept 2011

    9/10

    CONFIDENTIAL 9 CS/SEP 2011/CSC138

    PART C (30 MARKS)

    QUESTION 1Given the definit ion of co m p u te r record and declaration of co m p ut er s array:

    struct Computer{char brand[20];double price;int operating; // 1-};

    void main(){ Computer computers[20]

    }

    for

    [4] ;

    Linux a n d 2 f o r W i n d o w s

    // 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 ++ p rogram by using modular programming techniques that perform eachof the following tasks (one function for each task):a) Read data into the c o m p u te rs array.b) Display the brand and price of the most expensive com puter under the netbook type.c) Display all brand s of com puters with Windo ws as their operating system

    (15 marks)

    Hak Cipta Universiti Teknologi MAR A CONFIDENTIAL

  • 8/23/2019 CSC138 Sept 2011

    10/10

    CONFIDENTIAL 10 CS/SEP 2011 /CSC138

    QUESTION 2The input text f ile named t i m e c a r d . t x t contains a list of worker's name, identi ficationnumber, position code, and total hours of working for a working day. The data is for aspecific day of the m onth as shown in Figure 1 :

    | t imecard. tx t - Notep ad f m IHAMSSMIMJFHe Edit Format View HelpJM uiiaitO Etaci A i r a a a r M u b i n ; D M 1 0 8 2 0 0 5 A 1 1K h a i r u l N i z a m A h m a d ; G M 1 23 4 Q 9 2 B 8S i t i a m i n a h H a s s a n ; G K 9 8 0 2 3 4 5 B 9

    Figure 1 : The data writ ten in t i m e c a r d . t x t f ile* NOTE: The semicolon symbol (,-) is used as the delimiter.Each worker is paid bas ed on the following table:

    Position CodeABRate per hour (RM)5040

    Working hours more than 8 hours is considered as overtime and will be paid 50% m ore thannormal rate per hour.Exam ple: A w orker with position cod e of A' whose total hours of working is 11 hours w ill bepaid 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 Figure2 in the output text f ile named pa ym en t . t x t .

    ' f t ee n tb l t - N o , e< B d * N t i * * i r * - I** - -**;**!*Fjte Edit Format View HelpI D P o s i t i o n C od e T o t a l H o u r s

    DM1082005 A 11GM1234092 B 8GK9802345 B 9T o t a l n um b e r o f w o r k e r s i s : 3T o t a l n u m be r o f w o r k e r s w o r k i n g o v e r t i m e i s : 2T o t a l o v e r t i m e p a y m e n t i s : RM 2 8 5T o t a l d a i l y p a y m e n t i s : RM 1 3 2 5

    *