solution midterm 2.pdf

7
THE UNIVERSITY OF BRITISH COLUMBIA APSC 160: MIDTERM EXAMINATION FEBRUARY 21 ST , 2005 Name: ____SOLUTION___________________ Student #: _________________ Signature: _____________________ Lab Section: ______ Instructor: _______________ Question Mark Max 1 6 2 5 3 5 4 4 5 20 6 10 Total 50 Rules Governing Formal Examinations 1. Each candidate must be prepared to produce, upon request, a Library/AMS card for identification. 2. Candidates are not permitted to ask questions of the invigilators, except in cases of supposed errors or ambiguities in examination questions. 3. No candidate shall be permitted to enter the examination room after the expiration of one-half hour from the scheduled starting time, or to leave during the first half hour of the examination. 4. Candidates suspected of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action. a. Having at the place of writing any books, papers or memoranda, calculators, computers, audio or video cassette players or other memory aid devices, other than those authorized by the examiners. b. Speaking or communicating with other candidates. c. Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received. 5. Candidates must not destroy or mutilate any examination material; must hand in all examination papers; and must not take any examination material from the examination room without permission of the invigilator. Notes about this examination 1. You have 60 minutes to write this examination. 2. No notes, books, or any type of electronic equipment is allowed including cell phones and calculators. 3. Good luck!

Upload: jfoxx7777

Post on 10-Nov-2015

5 views

Category:

Documents


0 download

TRANSCRIPT

  • THE UNIVERSITY OF BRITISH COLUMBIA APSC 160: MIDTERM EXAMINATION

    FEBRUARY 21ST, 2005

    Name: ____SOLUTION___________________ Student #: _________________

    Signature: _____________________ Lab Section: ______ Instructor: _______________

    Question Mark Max 1 6 2 5 3 5 4 4 5 20 6 10

    Total 50

    Rules Governing Formal Examinations 1. Each candidate must be prepared to produce, upon request, a

    Library/AMS card for identification. 2. Candidates are not permitted to ask questions of the

    invigilators, except in cases of supposed errors or ambiguities in examination questions.

    3. No candidate shall be permitted to enter the examination room after the expiration of one-half hour from the scheduled starting time, or to leave during the first half hour of the examination.

    4. Candidates suspected of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action.

    a. Having at the place of writing any books, papers or memoranda, calculators, computers, audio or video cassette players or other memory aid devices, other than those authorized by the examiners.

    b. Speaking or communicating with other candidates. c. Purposely exposing written papers to the view of

    other candidates. The plea of accident or forgetfulness shall not be received.

    5. Candidates must not destroy or mutilate any examination material; must hand in all examination papers; and must not take any examination material from the examination room without permission of the invigilator.

    Notes about this examination

    1. You have 60 minutes to write this examination. 2. No notes, books, or any type of electronic equipment is allowed including cell phones and

    calculators. 3. Good luck!

  • Name (please print): Page 2 of 7 Section 1: Multiple Choice One mark each. For each of the multiple choice questions below, circle the best answer.

    Q1A. Abstraction is: a) anything that is difficult to understand b) the process of showing essential details while hiding non-essential details c) the process of converting a high level program to machine code d) a C data type

    Q1B. Which of the following is not an operator in C? a) + b) % c) $ d) *

    Q1C. Which of the following is not a mass storage device? a) hard disk b) floppy disk c) flash drive d) random access memory

    Q1D. Which of the following operators has the highest priority? a) (binary) + b) (unary) c) < d) &&

    Q1E. The programming language C is a) a program translator b) a high-level language c) a machine code language d) an assembly language

    Q1F. Which of the following is not a potential benefit of using functions in a program? a) the program is shorter b) the program is easier to read c) you can write a program without needing parameters d) it's easier to re-use parts of the program

  • Name (please print): Page 3 of 7 Section 2: Short Answer Questions

    [5] Q2. Consider the following expressions assuming that p and q are Boolean values.

    ( p && q ) || ( !p && !q )

    ( p || !q ) && ( p || q )

    Complete the truth tables below (one for each expression) to determine if the two expressions are equivalent.

    p q p && q !p !q !p && !q (p && q) || (!p && !q) T T T F F F T T F F F T F F F T F T F F F F F F T T T T

    p q !q p ||!q p || q (p || !q) && (p || q) T T F T T T T F T T T T F T F F T F F F T T F F

    Conclusion: Are the two expressions equivalent?

    The expressions are not equivalent. We can see, for example, that when p and q are both false, the values of the two expressions are not equal (one is true, the other is false).

  • Name (please print): Page 4 of 7 [5] Q3. Binary / Decimal conversion

    a) Convert the following binary number into decimal: 10010110. Show your work.

    10010110 = 1(128) + 0(64) + 0(32) + 1(16) + 0(8) + 1(4) + 1(2) + 0(1) = 150

    b) Convert the following decimal number into binary: 42. Show your work.

    [4] Q4. Evaluating expressions Evaluate the expressions below, assuming the following declarations have been made:

    int a = 8; int b = 3; double c = 6.0; double d = 0.1;

    a) a / b * c 12.0

    b) c / d a * b 36.0

    c) a % b + b % a 5

    d) a * d * c b / a 4.8

  • Name (please print): Page 5 of 7 Section 3: Writing and Understanding C Programs

    [20] Q5. Be sure to read the entire question before you write any code!

    Write a complete program in C that computes the tension in a cord using the following formula:

    Tension = (2m1m2) / (m1+m2) g

    where m1 and m2 are two masses (in kilograms) attached to either end of the cord, and specified by the user, and g is the acceleration of gravity = 9.80665 m/s2

    Your program should continually ask the user to enter two masses, compute the tension, and write the result to an output file until the user enters the value -1 as the first mass. The output file should contain a heading row at the top that labels each column in the file. Each successive row should have 3 columns containing the first mass, the second mass, and the tension. The output file should not contain the sentinel value -1 at the end.

    In the interest of time you may omit comment statements from your solution.

  • Name (please print): Page 6 of 7

    #include #include #define GRAVITATIONAL_FORCE 9.80665 #define TRUE 1

    int main( void ) { double mass1, mass2; double tension; FILE* outFile;

    outFile = fopen( "output.txt", "w" );

    if( outFile == NULL ) { printf( "Error opening output file\n" ); } else { fprintf( outFile, "Mass 1 Mass2 Tension\n" );

    while( TRUE ) { printf( "Enter first mass: \n" ); scanf( "%lf", &mass1 ); if( mass1 == -1.0 ) break; printf( "Enter second mass: \n" ); scanf( "%lf", &mass2 );

    tension = 2 * mass1 * mass2 * GRAVITATIONAL_FORCE / ( mass1 + mass2 ); fprintf( outFile, "%8.3lf %8.3lf %8.3lf\n", mass1, mass2, tension ); }

    fclose( outFile ); }

    system("PAUSE"); return 0;

    }

  • Name (please print): Page 7 of 7 [10] Q6. Trace the following program to determine its output

    #include #include #include

    int main(void) { int a = 6; double b = 6.1; double c = 0.0;

    double foo( int a, double b );

    a = a * b; printf("a = %i, b = %f, c = %f\n", a, b, c);

    c = foo( a, b ); printf("a = %i, b = %f, c = %f\n", a, b, c);

    system("PAUSE"); Output from program: return 0; } 36 6.1 0.0 36 6.1 double foo( int a, double b ) 36 6.1 5.8 { printf("a = %i, b = %f\n", a, b); a += 2; return b 0.3; printf("a = %i, b = %f\n", a, b); }

    Trace Table (be sure to trace all variables in the program):

    main foo a b c a b 6 6.1 0.0 36 36 6.1 38 5.8