Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam (75 minutes)

 

Name:                                                                         NetID:
 

Lab Section:                                                                Date: July 5, 2006

 

No questions will be answered during this examination.  If you do not understand a question, read it again.  If you still do not understand it, make reasonable assumptions and write them down. (Points will be deducted if unreasonable assumptions are made.)
DO NOT CHEAT: Cheating includes not only copying from another person but also allowing someone to copy from you.  Anyone who copies or allows someone to copy will receive a score of zero.  So be defensive and protect your work.

 

This examination contains 10 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.

The number of points for a question is roughly proportional to the amount of time you may need for it. Don’t spend too much time on any one question.

Do not forget to sign the attendance list and to write your signature on the line below:

 

_______________________________________________________________________
(If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.)

You may not use any electronic devices, book, notes or other references during this examination.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Score

Grader

1

9

 

 

2

12

 

 

3

8

 

 

4

4

 

 

5

10

 

 

6

8

 

 

7

10

 

 

8

8

 

 

9

12

 

 

10

10

 

 

11

9

 

 

12

12

 

 

13

10

 

 

14

10

 

 

15

8

 

 

16

12

 

 

17

13

 

 

Total

165

 

 

 

 

1.         Assume that a matrix named x exists in your workspace. Write a single Matlab command to do each of the following:

 

      a) Write a command to display the number of rows and columns of the matrix x:



     >>  ______________________________________________________

 

 

 

 

      b) Write a command to add all the values in the matrix x and display this value.

 

     >>  _____________________________________________________

 

 

 

 

      c) Write a command to display the values for ex where x is a matrix.


            >> _________________________________________________

 

 

 

 

 

 

 

 

2.      Write a function named calculate_sales to calculate sales tax.  This function should take as input a  single parameter named sales and return the amount of sales tax. Assume the sales tax rate is 6.25%.  Your function must return the amount of sales tax and should not print the value to the screen. Hint: to compute the sales tax on 100 dollars in sales you would calculate 100 * .0625 .

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.      a) Write one or more Matlab commands to create a 100 by 100 matrix consisting of the vector 1:100   

          on the main diagonal and zeros elsewhere.

 

 

 

 

 

 

 

   

     b) Write one or more Matlab commands to create a vector containing exactly 1000 numbers equally  

          spaced between 0 and 1 (including the numbers 0 and 1).

 

 

 

 

 

 

 

 

 

4.      Write a single Matlab command to create a column vector consisting of 1000 sevens.  Do not write (or abbreviate) 1000 sevens.

 

       

            >> _____________________________________________________


 

 

 

 

 

 

 

5.       Fill in the blanks to make a 3D plot of the function z = sin(x) + cos(y) for x ranging from -2π to 2π and y ranging from -2π to 2π.  Use the meshgrid function to create a grid of 100 x 100 points in the x-y plane. You may use any of the built-in Matlab 3D plot functions.

 


            >> x = linspace( ______________________________________ );

     >> y = x;


     >> [XGrid, YGrid] = ____________________________________ ;

 

     >> ZGrid =   ___________________________________________ ;

 

 

     >>  ____________________________________________________ ;

6.      Given the vectors:

 >> x = 1:100;
 >> y = 101:200;

  

  
Which of the following are equivalent to the command:

        >> plot(x,y)

  
(Circle any that are equivalent.)

       a) plot(x)

 

 

 


   
b) plot(y)

 

 

 

 

 

       c) plot(y,x)

 

 

 


  
d) fplot(x,y)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      Write Matlab command(s) to approximate the definite integral,

 

Use the trapz function with 100 values from 0 to .

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      a) Given a vector z of any length, write a single Matlab command to reverse the order of  z. For example if z = [ 1 2 3 4 5] then after excuting your command z equals [ 5 4 3 2 1]. Your code should work for any vector z and not just z = [ 1 2 3 4 5].




>> z = ____________________________________________________________________

 

    b) Given the vector V of any length, write a single Matlab command to prepend a 0 to the beginning of

        V. For example if V = [ 1 3 5 7] then after executing your command V equals [ 0 1 3 5 7]. Your code

        should work for any vector V and not just V = [ 1 3 5 7].

 

 

 

 

    >> V  = ____________________________________________________________________

 

 

 

 

 

 

 

 

9.      Suppose the following Matlab statement has been executed:

 

     >> A = [1 2 3 4 ; 5 6 7 8];

 

      Write the correct response that Matlab would display when executing the following commands. If an  

       error would result then write ERROR.

 

a)     >> B = [ A’ , [9 ; 10 ; 11 ; 12] ]

 

 

 

 





_________________________________________________________

 

     b) >> A(1,end)



 

 

 

 

 

        ________________________________________________________

     c) >> A(2,[2 3]) = 9;

        >> A

                         A =

 

 

 



                  ___________________________________________________________________

 

 

 

 

 

 

 

 

 

10.  Let A be a 3 x 2 matrix. Which of the following are valid operations? Circle each correct answer.

 

 

A + A'         A ./ A            A .^ A        A * A        A .* A

 

 

11.  Fill in the blanks to convert the following linear equations into the form A* b = c:

 

          5X + 2Z          = 30

          3X­­ + 5Y + 6Z = 27

          7Z + 3Y + 9X = 51     

 

     where A is a matrix of coefficients and c is a column vector of constants.

 

    >> A =                                              

 

    >> c =                                              

 

    Now write the Matlab statement to solve the system for b (Do not solve the system manually):

 

     >> b =                                              

 

 

 

 

 

 

 

 

12.  Complete the code for the function named polyd (below) that computes the derivative of a polynomial (and only polynomials). The input to your function is a vector that represents a polynomial. The output of your function is a vector that represents the derivative of the polynomial.  For example, given the input polynomial p = [4  0  6  3  -4]  (which is 4x4 + 6x2 + 3x – 4) your function would return 

      [16  0  12  3] (which is 16x3 + 12x + 3) Hint: Notice that for this example,  

      p .* [4  3  2  1  0] = [16  0  12  3  0] , which is almost the correct answer.

     Your code should work for any polynomial and not just p = [4  0  6  3  -4] . You may create any

     variables you need to use in your function.

 

 

function dp = polyd( p )

     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13.  Assume the following code has been saved in a file named script.m :

           

          L = 5;

          d = [1 2 3];

          K = [4 5 6];

 

 

   Suppose the following statements are typed at the Matlab prompt. Write the correct output that Matlab  

   would display.

 

     >> clear

     >> L = 9;

     >> script

     >> D = d ./ K;

     >> who