Computer Science 2015

Embed Size (px)

Citation preview

  • 8/16/2019 Computer Science 2015

    1/13

     Page 1 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    THE UNIVERSITY OF THE WEST INDIES

    EXAMINATIONS OF May 2015

    Code and Name of Course: COMP1105 - COMPUTER PROGRAMMING 1

    Date and Time: Duration: 3 hours 

    INSTRUCTIONS TO CANDIDATES: This paper has 9 pages and 4 questions

     All Questions are Compulsory

    Marking Code:

    Tests acceptable and basic knowledge of the material = 31.5

    Tests applied skills and critical and analytical thinking = 21.5

    Tests excellent skills and breadth of knowledge = 7

    Question 1 [15 marks] (8 marks + 5 marks + 2 marks)

    You are being interviewed as a contract programmer for the Faculty of Science and Technology.Your responsibility is to create simple, suitable, and secure games for students enrolled in

    summer school to play in labs as a means of relaxation in the intense six week schedule of

    summer school, in the down periods between classes and lab sessions.

    Note: Read the entire question before you begin.

    The interviewers ask you to write pseudocode (in 45 minutes) for a simple guessing game. Writeyour solution all in one program. All of the necessary preamble and concluding logic for an

    algorithm is expected.

    [2 marks]

    a.  In this game you have to start by printing a grid with a title as follows:

    “THE GRID OF GUESSES

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

  • 8/16/2019 Computer Science 2015

    2/13

     Page 2 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ” 

    Call a function (sub-program) with at least one loop in it to print the layout of the grid.

    [3 marks]

     b.  Then print the following message:

    “Welcome to the guessing game. You have 25 tries to guess the number that I am

    thinking of between 1 and 30. Each time you make an incorrect guess, we willshow it to you in the GRID OF GUESSES. You may keep guessing until you are

    successful or until you have filled the grid.” 

    The algorithm must then generate the number between 1 and 30 and not reveal it to the

    user, ask the user to guess the number and then read the guess.

    [2 marks]

    c.  The algorithm must check the guess against the generated number and if they are the

    same it must print a message to the screen saying “Congrats, you have won!”, and then

    stop the game.[2 marks]

    d.  If the guess is not correct, the algorithm must perform two tasks:

    Print to the screen the incorrect guess in the grid in the next available spot.

    Check to see if the guess is greater or less than the random number and indicate thisto the user.

    [2 marks]

    For example, if the random number is 17 and the player guesses 10, the following would be

     printed to the screen:

    “THE GRID OF GUESSES 

    10 | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _Your number is too small. Try again” 

    If the player then guesses 20, the following would be printed to the screen:

    “THE GRID OF GUESSES

    10 |20 | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

  • 8/16/2019 Computer Science 2015

    3/13

     Page 3 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    Your number is too big. Try again” 

    e. 

    Your algorithm must also keep score. Use a counter that starts at 25 and for everyincorrect guess, decrement it. The more guesses the less points. The user must be

    restricted to 25 guesses.

    [2 marks]Print your final score to the screen and to the file: myScores.txt.

    [2 marks]

    You do not have to use an array to hold the contents of the grid.

    YOUR SOLUTION SHOULD INCLUDE DOCUMENTED PSEUDOCODE ONLY. 

    NO C-CODE IS REQUIRED.

    SCHEME:BEGIN ALGORITHM (MAIN PROGRAM) [ ½ MARK ]

    DECLARE [ ½ MARK ]

    integer iSecretinteger iGuess,

    integer array pos

    integer guessesinteger points

    File pointer fp to point to counter.txt

    Initialize [[ ½ MARK ]Initialize pos to „\s‟ // can wor k because of ascii

    Initialise guesses to 25

    /*

    a.  In this game you have to start by printing a grid with a title as follows:“THE GRID OF GUESSES 

    | | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _” 

    Call a function (sub-program) with at least one loop in it to print the layout of the grid.

    */

    Call SubProgram PrintGrid [1/2 mark]

  • 8/16/2019 Computer Science 2015

    4/13

     Page 4 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

     print

    “Welcome to the guessing game. You have 25 tries to guess the number that I am

    thinking of between 1 and 30. Each time you make an incorrect guess, we will show it toyou in the GRID OF GUESSES. You may keep guessing until you are successful or until

    you have filled the grid.” 

    // [1 mark]

    /*

    The algorithm must then generate the number between 1 and 30 and not reveal it to the

    user, ask the user to guess the number and then read the guess.*/

    iSecret Generate a random number between 1 and 20 // [1/2 mark]

     print "Guess the number (1 to 30)” // [ ½ mark for request and read] 

    read iGuess

    /*the algorithm must check the guess against the generated number and if they are thesame it must print a message to the screen saying “Congrats, you have won!”, and then

    stop the game.

    */

    [1/2 mark] [1/2 mark]

    DO WHILE (iSecret NOT EQUAL iGuess) AND (guesses >= 0)

    if iSecret EQUAL iGuess then // [ 1/2 mark]

     print “Congrats, you have won” // [1/2 mark] exit program // [1/2 mark]

    endif

    /*10 | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _Your number is too small. Try again” 

    */

    if iSecret < iGuess || iSecret > iGuess then [ ½ mark]increment guesses [ ½ mark]

     pos [guesses]  iGuess [ ½ mark]

    decrement points // keep track of guesses and points [ ½ mark] print “ 

     pos [0] | pos[1]| pos[2]| pos[3]| pos[4]| //etc. for all positions [ ½ mark]

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ pos [5] | pos[6]| pos[7]| pos[8]| pos[9]| //etc. for all positions

  • 8/16/2019 Computer Science 2015

    5/13

     Page 5 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _| | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    | | | | |

     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ” if iSecret < iGuess then [ ½ MARK ] // HANDLE THE SMALL 

     print “Your number is too small. Try again” 

    if iSecret > iGuess then [ ½ MARK ] //HANDLE THE LARGE 

     print “Your number is too large. Try again” 

     print "Guess the number (1 to 30)” // [ ½ mark for request and read] 

    read iGuess

    End WHILE //(iSecret NOT EQUAL iGuess) AND (guesses < = 25) // while they have not

    // guessed correctly [1/2 mark]// and < 25 tries [1/2 mark]

    open fp for writing [ ½ MARK ]

    if fp EQUAL null [ ½ MARK ] print “error” [ ½ MARK ]

    exit program

     print “here are your points: points” to fp [ ½ MARK ]

    End Algorithm [1/2 MARK ]

    Begin Algorithm (SubProgram PrintGrid) // define function header [1/2 mark]

    //use a loop correctly to do something in the grid patternFor i = 1 to 5 DO // print the five rows [½ mark]

    For i = 1 to 5 DO [ ½ MARK ]Print “ |” // print tab and pipe pattern five times

    EndFOR

    For i = 1 to 5 DO [  ½ MARK ]Print “_ _ _ _ _” // pr int underline pattern 5 times

    EndFOR

    EndFOREnd Algorithm (SubProgram PrintGrid)

    Question 2: Multiple Choice [15 Marks - 1 mark each] (9 marks + 6 marks)

    i.  Which of the following is not a valid C variable name?

    a.  int #number; b.  float 1rate;

    c.  int main;

    d.  all of the above[1 mark]

    ii.  The format identifier „%lf‟ is also used for _____ data type?

    a. 

    char b.  int

  • 8/16/2019 Computer Science 2015

    6/13

     Page 6 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    c.  float

    d.  double

    [1 mark]iii.  What is the output of this C code if the input is 6, 8 and 10?

    1. #include 2.

    3. void main()4. {5. int num1, num2, num3;6.7. printf("Enter the values of num1, num2 and num3 ");8. scanf("%d %d %d", &num1, &num2, &num3);9. printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);10. if (num1 > num2)11. {12. if (num1 > num3)13. {14. printf("num1 is the greatest among three \n");15. }16. else

    17. {18. printf("num3 is the greatest among three \n");19. }20. }21. else if (num2 > num3)22. printf("num2 is the greatest among three \n");23. else24. printf("num3 is the greatest among three \n");25. }Figure 1

    a.  num1 = 6 num2 = 8 num3 = 10 num3 is the greatest among three b.

     

    num1 = 6 num2 = 8 num3 = 10

    num3 is the greatest among three

    c. 

    num1 = 6 num2 = 8 num3 = 10 num3 is the greatest among threed.

     

    num1 = 10 num2 = 8 num3 = 6

    num1 is the greatest among three

    [1 mark]

    iv.  How many times will “True” be printed?

    1.  #include 2.  int main()3.  {4.

     

    int i = 0;5.

     

    while (i == 0);6.

     

    printf("True\n");7.

     

    printf("False\n");8.

     

    }

    a.  Infinite times b.  1 time and then False

    c.   Never

    d. 

    It depends

  • 8/16/2019 Computer Science 2015

    7/13

     Page 7 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    [1 mark]

    1. /* C program to check whether a given integer is odd or even*/

    2.

    3. #include

    4.5. void main()

    6. {

    7. int ival, remainder;

    8.

    9. printf("Enter an integer : ");

    10. scanf("%d", &ival);

    11. remainder = ____________________; // 1

    12. if (remainder == 0)

    13. printf("%d is an _________ integer\n", ival); // 214. else

    15. printf("%d is an _________ integer\n", ival); // 3

    16. }

    Figure 2

    v.  The C-code in Figure 2 above will check whether a given integer is odd or even. What

    should be entered in the blanks?

    a.  (1) ival / 2 (2) even (3) odd b.  (1) remainder % 2 (2) even (3) odd

    c.  (1) remainder % 2 (2) odd (3) even

    d.  (1) ival % 2 (2) even (3) odd[1 mark]

    vi.  The keyword used inside a loop to return to the beginning of the loop

    a. Exit b.Goto

    c.Continue d.Return

    [1 mark]

    vii.  What will be the output of the code below?

    a.  dance (three spaces to the left of dance)

     b.  dance (followed by three spaces after dance)c.  dance

    d.  *dance

    [1 mark]#include int main(){

    char s[8] = "dance";int i = 9;

    printf("%*s", i, s);}

  • 8/16/2019 Computer Science 2015

    8/13

     Page 8 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    viii.  What will be the output value if you will compile and execute the following c code? 

    1. 

    #define x 5+2 2.  void main(){ 

    a.  int i; b.  i=x*x*x; 

    c. 

    printf("%d",i);3.  } 

    [1 mark]

    a.  343

    b.  27c.  133

    d.  Compiler

    ix.  User-defined data types can be derived using___________.

    a.  struct

     b.  enum

    c. 

    typedefd.  All of the mentioned

    [1 mark]

    x.  Which of the following is a unary operator?

    a. && c. ? :

    b. ! d. %c[1 mark]

    xi.  What is the output of this C code?

    1. 

    #include

    2. 

    int main()3. 

    {4.  foo();5.  foo();6.  }7.

     

    8. 

    void foo()9.

     

    {10.

     

    int i = 11;11.

     

    printf("%d ", i);12.  static int j = 12;13.  j = j + 1;14.  printf("%d\n", j);15.  }

    a.  11 12 11 12

     b.  11 13 11 14

    c.  11 12 11 13

    d.  Compile time error

    [1 mark]

    xii.  Which of these statements relates to modularization?

    a. 

    Application code is subdivided into separate subprograms, with each running aspecific function or subroutine.

  • 8/16/2019 Computer Science 2015

    9/13

     Page 9 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

     b.  Parts of code can be split up and worked on by multiple programmers, teams or

     by an individual over time, with each part of code dedicated to doing on specific

    task.c.  Subprograms are recyclable, and the programmer can simply call on an existing

    module in a new program that he/she is working on, saving time and energy in

    his/her work.

    d. 

    All of the above [1 mark]

    xiii.  What will be the output of the program below?

    #include

    int main(){

    int k=1;printf("%d = = 1 is %s\n", k, k= =1?"TRUE":"FALSE");return 0;

    }

    a.  k = = 1 is TRUE

    b.  1 = = 1 is TRUEc.  1 = = 1 is FALSE

    d.  K = = 1 is FALSE

    [1 mark]

    xiv.  What does fp point to in the program below?

    #include

    int main(){

    FILE *fp;fp=fopen("trial", "r");return 0;

    }

    a. The first character in the file  b. A pointer to the beginning of the file.

    c. The name of the file.

    d. The last character in the file.[1 mark]

    xv.  The sequential model of software development which has no feedback is known as the:

    a.  Spiral Model

    b.  Waterfall Modelc.  Fountain Model

    d.  Iterative Model[1 mark]

    Question 3 [15 marks][7.5 marks+6.5 mark +1 mark]

  • 8/16/2019 Computer Science 2015

    10/13

     Page 10 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    a.  Examine the program shown in Figure 3 and list all of the syntax and logical errors that are

     present by stating the line number, the error [1/2 mark] and indicating the correct syntax or

    logic [1/2 mark]. The program is written to swap the values of num1 and num2 into the othervariable. So at the end of the program, the original value of num1 should be in num2 and the

    original value of num2 will be in num1.

    Mark and correct each highlighted issue to a max of 5 marks. There are 10 opportunities formarks.

    [5 marks]1. #include ;2.3. void swap(a, b);4. {5. temp = a;6. a = b;7. b = temp;8. return();9. }10.

    11. int main();12. {13. num1 = 10, num2 = 20;14.15. printf("Before swapping num1 = %d num2 = %d\n", num1, num2);16. swap(int num1, int num2);17.18. printf("After swapping num1 = %d num2 = %d \n", num1, num2);19. return();20. }

    Figure 3

     b.  Explain the scope of the variables num1 and temp.

    [2 marks]Answer:

    They have local scope[1 mark]. A variable that is declared inside a function definition is local. It

    is created and destroyed every time the function is executed, and it cannot be accessed by anycode outside the function. [1 mark] 

    c.  Using the code example briefly explain term call-by-value.

    [1 mark]

    The values of num1 and num2 are copied and passed to a and b. The value of num1 and

    num2 do not change.

     b.  Write the C-program for a time converter that will request and accept the time in Bridgetown

    and ask the user for a number representing a second city. The options for the second country

    are 1- New York, 2- London (England), 3- Toronto, 4- Hong Kong, 5- Acapulco (Mexico), 6-Detroit, and 7- St. George's (Grenada). You must use a switch statement to print the time in

    Barbados and the time in the given city during the summer months. The summer time

    conversion is as follows:

    1-   New York: Same as Bridgetown

    2-  London (England): +4 hours

    3-  Toronto: Same as Bridgetown

    4- 

    Hong Kong: +12 hours

  • 8/16/2019 Computer Science 2015

    11/13

     Page 11 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    5-  Acapulco: -2 hours

    If any other city is given as input the program should print a suitable message.[7 marks]

    #include

    main( ){int city;int hours, minutes; // 1 mark for variables used in program  char mode[3];

    // 1 mark for prompting and scanning variables correctlyprintf(“Please enter the time in Barbados in the format 12:00”); scanf(“%d:%d”, &hours, &minutes); 

    printf(“Is it AM or PM?”); // 1 mark for some accommodation for AM/PM  scanf(“%s”, mode); 

    // 1 mark to request and readprintf(“Please enter the number indicating the second city”); 

    printf(“ 1- New York, 2- London (England), 3- Toronto, 4- Hong Kong”); printf(“ 5- Acapulco (Mexico)”); 

    scanf(“%d”, &city); 

    switch( city ){ // No change and a reasonable default 1 mark// hours+4 , +12, –2 1 mark

    case 1:case 3: printf(“No change in time”);case 2: hours += 4;case 4: hours += 12;case 5: hours -= 2;

    default : printf(“ERROR: Your second city is not known”); }// 1 mark for accommodating for 12 hrs or 24 hrs (if military time used)

    if ((hours > 12) && (strcmp(mode, “AM”)) {hours = 12 – hours;strcpy(“PM”, mode);} 

    else if ((hours > 12) && (strcmp(mode, “PM”)) {hours = 12 – hours;strcpy(“AM”, mode);} 

    else if ((hours < 0) && (strcmp(mode, “PM”)) {hours = 12 + hours;strcpy(“AM”, mode);} 

    else if ((hours > 12) && (strcmp(mode, “AM”)) {hours = 12 + hours;strcpy(“PM”, mode);} 

    printf(“Second city time is %d:%d %s”, hours, minutes, mode);

    } // end program

    Question 4 [15 marks][7 marks+4 marks+4marks]

    a.  Write a function called swapI t  that accepts two character arrays (string1 and string2 are arrays

    filled with characters and string2 is the same size as string1), an integer value for the size of the

    arrays (arraysize), and swaps the contents of the strings i.e. contents of two strings areinterchanged.

    For example, if the declarations and initializations are as follows

    char string1 [] = “School”; 

  • 8/16/2019 Computer Science 2015

    12/13

     Page 12 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    andchar string2 [] = “Pastel”; 

    thenstring2 will become “School” 

    andstring1 will become “Pastel” 

    [5 marks]void swapit(char string1[], charstring2[], int arraysize) // 1 mark  

    { const int mysize = arraysize; // 1 mark declarations 

    char temparray [mysize];

    for (int i = 0; i < arraysize; i++) // 1 mark loop to temporarily store 

    temparray [i] = string1[i];

    for (i = 0; i < arraysize; i++) // 1 mark loop 2 to copy one to another  

    string1 [i] = string2[i];

    for (i = 0; i < arraysize; i++) // 1 mark loop 3 copy out of temporary storage

    string2 [i] = temparray[i];

    return;}

     b.  A certain computer stores floating point numbers using a 10-bit mantissa, a 5-bit exponent (in

    excess 2n-1

     notation) and 1 bit for the sign. Show how the number 20.375 would be stored in a16-bit word. (Assume the exponent is represented in Sign and Magnitude form). What is the

    error in the representation?

    [5 marks]0 1 0 1 0 0 0 1 1 0 0 1 0 1 0 1

    sign mantissa exponent

    Sign bit [ 1 mark] 

    Convert whole part to binary [1/2 mark] Convert fraction to binary [1/2 mark] 

    Result: 10100.01100

    Exponent is 5 [1 mark] In excess 2

    n-1 notation with 5 bits => 5 + 2

    5-1[1 mark]

     

    Convert to binary 10101 [1 mark] 

    c.  The basic outline of a C program is shown below:#include int main(){ FILE *fpt1, *fpt2;

    int num_t;int num_line;

    if ((fpt1 = fopen(“mockingbird.txt”, “r”) == NULL) ||(fpt2 = fopen(“counter.txt”, “r”) == NULL)) 

    { printf(“Can’t open the files\!\n”); exit(1);

    }//...

  • 8/16/2019 Computer Science 2015

    13/13

     Page 13 PLEASE TURN OVER

    The University of the West Indies  Course code: COMP1105 May 2015

     __________________________________________________________________________________________________

    DO NOT WRITE OR TYPE ON THE BACK OF THIS SHEET: USE ONE SIDE ONLY

    INSTRUCTIONS:  Each page  must be signed by the FI RST AND SECOND EXAM INERS. Completed forms should be

    handed to the Assistant Registrar (Examinations).

    .. ........... ................................... ............................................First Examiner Second Examiner

    Date: 2015/04/….  Date: 2015/04/....

    //Point A//(i)num_t = 0; //[1/2 mark]num_line = 0; //[1/2 mark]  while (!feof(fpt1)) //[1/2 mark]{ fscanf( fpt1, “%c%”, &mychar); //[1/2 mark]

    if (mychar == „t‟)  //[1/2 mark]

    num_t++; //[1/2 mark]//[1/2 mark]if ((mychar == „.‟)||(mychar == „!‟)|| (mychar == „?‟))

    num_line++; //[1/2 mark]}

    //(ii)fprintf(fptr2, “%d, %d”, num_t, num_line);//[1 mark]

    //...

    fclose(fpt1);

    fclose(fpt2);

    return 0;}

    Assume that mockingbird.txt has a format similar to the following:

    When he was nearly thirteen, my brother Jem got his arm badly broken atthe elbow. When it healed, and Jem's fears of never being able to playfootball were assuaged, he was seldom self-conscious about his injury.His left arm was somewhat shorter than his right; when he stood orwalked, the back of his hand was at right angles to his body, his thumbparallel to his thigh. He couldn't have cared less, so long as he could

    pass and punt. When enough years had gone by to enable us to look backon them, we sometimes discussed the events leading to his accident. Imaintain that the Ewells started it all, but Jem, who was four years mysenior, said it started long before that.

    Write the code that will be inserted at Point A,  by declaring any additional variables thatyou need and writing code to do the following:

    i.  Read in the contents of mockingbird.txt character by character  and count allof the occurrences of the letter „t‟ and the number of sentences in the file.

    Remember that sentences end with full-stops, question marks and exclamation

    marks.[4 marks]

    ii.  Output the number of the occurrences of the letter „t‟ and the number of

    sentences in the story to the file counter.txt. [1 mark]

    END OF EXAM