CISC106, Fall 2006

Second Midterm Exam Makeup (E02b)

Exam Date: November 14, 2006

Name: ________________________________________________________________


UD Email: _____________________________________________@ udel.edu


Circle your section number:      

     010       011        012        013

     014       015        016        017

 

Do not write your name on any page except this one.

Answer all questions on this paper directly (including the multiple choice)

Total Points: ???


A hint about allocating your time:

You need to answer 100 points worth of questions in 50 minutes, so....


Multiple Choice: What is the output?

Each of the problems in this section gives you some input that is typed into MATLAB inside the box with the heavy border.

Which of the boxes afterwards with the light border represents the output you will get back?

Consider each problem as a new problem—that is, suppose that you do a clear command in between each problem.

Feel free to use the blank space on the exam sheet for scratch work. .

  1. (4 pts) Suppose you have the following M file in your current working directory.

    function result = mysteryFunction(a,b)
    %mysteryFunction does something mysterious
    %Examples:
    % ... (omitted to make you think harder)
    %D. Duck for CISC106 sect 99, 11/10/2006
    if (a < b)
    result = a:b;
    else
    result = b:a;
    return;
    end
    Then, suppose you type the following at the MATLAB prompt. What is the output?

    >> mysteryFunction(6,3)
    1. ans = 
      6:3
      >>
    2. ans = 
            3:6
      >>
    3. No output—just a new >> prompt
    4. ans =
            3 4 5 6
      >>
    5. ans =
            6 5 4 3
      >>
  2. (4 pts) Suppose you have the following M file in your current working directory.

    function result = yetAnotherFunction(a,b)
    %yetAnotherFunction does something with a and b
    %Examples:
    %  ... (omitted to make you think harder)
    %D. Duck for CISC106 sect 99, 11/10/2006
      if (a > b)
        result = [ a b ];
      else
        result = [ b a ];
      end
      return;
    end
    
    Then, suppose you type the following at the MATLAB prompt. What is the output?

    >> yetAnotherFunction(2,4)
    1. ans = 
      8
      >>
    2. ans = 
            24
      >>
    3. No output—just a new >> prompt
    4. ans =
            2  4
      >>
    5. ans =
            4  2
      >>
  3. (4 pts) Suppose you have the following M file in your current working directory.

    function result = stillAnotherFunction(v)
    %stillAnotherFunction does something with a vector v
    %Examples:
    %  ... (omitted to make you think harder)
    %D. Duck for CISC106 sect 99, 11/10/2006
      result = 0;
      for k=1:length(v)
         result = result + ( v(k)^2 );
      end
      return
    end
    


    Then, suppose you type the following at the MATLAB prompt. What is the output?

    >> stillAnotherFunction([3 4]);
    1. ans = 
      3 4
      >>
    2. ans = 
            7
      >>
    3. No output—just a new >> prompt
    4. ans =
            9 16
      >>
    5. ans =
            25
      >>
  4. (4 pts) Suppose you have the same M-file as in the previous question, and you type the following at the MATLAB prompt. What is the output?

    >> stillAnotherFunction([6 3])
    1. ans = 
      6 3
      >>
    2. ans = 
            9
      >>
    3. No output—just a new >> prompt
    4. ans =
            36    9
      >>
    5. ans =
            45
      >>

    Basics of C++ programming (short answer)


  5. (4 pts) When writing C++ programs, you need to use a text editor to create the source file. What would you type at the Unix command prompt to bring up the text editor to create or make changes to a C++ file called lab08.cc?




  6. (4 pts) Give the Unix command to compile and run a C++ program called lab08.cc





  7. (4 pts) Give the C++ equivalent of the following MATLAB statement. You do not need a complete C++ program to answer this question—just the line(s) of code that correspond to the one line of MATLAB code listed below.


    diameter = input('What is the diameter?');






  8. (4 pts) Give the C++ equivalent of the following MATLAB statement. Assume that the variable result has already been assigned a value (and in the case of C++, it was previously declared as type double.) You do not need a complete C++ program to answer this question—just the line(s) of code that correspond to the one line of MATLAB code listed below.


    fprintf('result = %d\n',result);








    Fill in the missing code

    For each of the questions in this section, you are given

    In each problem, there is one or more piece missing that you have to fill in. Fill in the missing piece.

  9. In the file countNegativeNumbers.m, fill in

    In the file testCountNegativeNumbers.m, fill in

    function result = countNegativeNumbers(vec)
    %countNegativeNumbers counts how many even numbers are in a vector
    %
    %consumes: vec, vector of numbers
    %produces: result, a scalar number
    %
    %Examples:
    %   >> countNegativeNumbers([1 -3 0 -5 6])
    %   ans = 2
    %   >> countNegativeNumbers(-3:1:3)
    %   ans = 3
    %   >> countNegativeNumbers([1 3 5])
    %   ans = 0
    
      count = 0;
    
      for k=1:length(vec)    
    
         if ( vec(k) < 0          )          % <== fill in something here
             count = count + 1;          
         end % end of if
      
      end % end of for
    
      result = count;                        % <== fill in something here on
                                             %     right hand side
    
    end
    
    
    
    % testCountNegativeNumbers.m
    % regression/acceptance test for countNegativeNumbers.m
    % S. J. Cat, CISC106, sect 99, 11/06/2006
    
    
    fprintf('Test 1: ');
    if (                          )             % <== fill in something here
      fprintf('passed\n');
    else
      fprintf('failed\n');
    end
     
    
    fprintf('Test 2: ');
    if (                           )            % <== fill in something here
      fprintf('passed\n');
    else
      fprintf('failed\n');
    end
     
    fprintf('Test 3: ');
    if (                           )             % <== fill in something here
      fprintf('passed\n');
    else
      fprintf('failed\n');
    end
    
    >> testCountNegativeNumbers
    Test 1: passed
    Test 2: passed
    Test 3: passed
    >> 
    
  10. In the file slope.m, fill in

    In the file testSlope.m, fill in

                                                                 % <== fill in 
    %slope return slope of line passing through two points
    %
    %consumes: x1, y1, x2, y2: scalar numbers
    %produces: theSlope, a scalar number
    %
    %Examples:
    %    >> slope(2,2,4,4)
    %    ans = 1
    %    >> slope(3,1,5,5)
    %    ans = 2
    %    >> slope(1,4,1,5)
    %    Error: slope is undefined
    %    >>
      
    denom = x1 - x2;
    num = y1 - y2;
    
    if (                )                     % <== fill in this line of code
      error('slope is undefined');
    else
      theSlope = num/denom;
      return;
    end
    
    % testSlope.m  Peggy Hill for CISC106, sect 99, Arlen Comm. College
    % regression/acceptance testing for slope.m
      
    
    fprintf('Test 1: ');
    if (slope(2,2,4,4)==1)
      fprintf('passed\n');
    else
      fprintf('failed\n');
    end
    
    fprintf('Test 2: ');
    if (slope(3,1,5,5)==2)
      fprintf('passed\n');
    else
      fprintf('failed\n');
    end
    
    fprintf('Test 3: ');
    try
       x = slope(1,4,1,5);
       fprintf(                  );   % <== fill in some code here
    catch
       fprintf(                  );   % <== fill in some code here
    end
    
    >> testSlope
    Test 1: passed
    Test 2: passed
    Test 3: passed
    >> 
    
  11. Writing an M-file

  12. (30 pts)

    In the space below, write a complete M-file to define a function that computes the product of all the elements of a one-dimensional vector of numbers.

    Important notes (read these carefully!)


    Hints (questions to guide your thinking if you are stuck)
>> product([3 4 5])

ans =

    60
>> product([-1 -5 -0.5 -1])

ans =

    2.5000

>>




(Extra space in case you need it for the M-file)
(End of Exam)

Notes Sheet for CISC106, E02, Nov 09 2006

MATLAB vs. C++

  MATLAB C++
comments % comment // comment
string literals 'delimited with single quotes' "delimited with double quotes"
variables don't have to be declared
before they are used
have to be declared before they are used
(e.g. int x;)
choices: double, float, int
input  price = input('please input the price: ');  double price;
 cout << "please input the price: ";
 cin >> price;
output   fprintf('The price is %d\n',price);
or
  disp(['The price is ' num2str(price)]);
 cout << "The price is " << price << endl;
if/else statements

if/else/end is used to delimit blocks

if (condition)
  statement;
else
  statement;
end

{} is used to delimit blocks;
there is no end keyword

if (condition)
{
  statement;
}

else
{
  statement;
}

function
definition

start with the word function
have output variable(s)
types are not needed on result or arguments
put end at the end of function body

function result = nameOfFunction(formalArg1)
%nameOfFunction brief statement of purpose
%rest of H1 comment
  statementsInBodyOfFunction;
  result = someExpression;
  return;
end

The word function does not appear
there is no output variable
but the output type is required (e.g. double)
arguments must have types (e.g. double)
output value follows keyword return
no keyword end—use {} around the body

double nameOfFunction(double formalArg1)
{
  statementsInBodyOfFunction;
  return someExpression;
}