Final Exam Review Questions | Introduction to Computing Using MATLAB | CS 1112, Exams of Computer Science

Material Type: Exam; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Unknown 2008;

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-hl2-1
koofers-user-hl2-1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Review questions for the final exam
1. A Pascal’s triangle with levels 0 to 4 is shown below. Level 0 has a single value, and each value on subsequent
levels is the sum of the two entries diagonally above in the previous level of the triangle. For example, the value
6 in level 4 is the sum of the values 3 and 3 in level 3.
1
11
121
1331
14641
.
.
.
level 0
level 1
level 2
level 3
level 4
Complete function pascalVector below to return the row vector corresponding to a specified level of Pascal’s
triangle. For example, if level lev is 4, then the returned vector must be [1,4,6,4,1]. Assume that lev is a
non-negative integer. The only Matlab built-in functions allowed are zeros,ones, and length.
Do not use the formula for binomial coefficients to solve this problem. Use a loop (or loops): the vector for each
level is based on the vector from the previous level.
function p = pascalVector(lev)
% p is the vector corresponding to level lev of Pascal’s triangle
2. Cis a cell array with three components: a real number, a vector of numbers, and a matrix of numbers.
Write a fragment to copy all the values in cell array Cto a column vector v. For the values in the matrix, copy
the values to the vector row-wise. Do not use vectorized code (recall that concatenation is vectorized code and
therefore cannot be used in this question).
3. Given integers nBig and nSm where nBig >nSm, write a fragment that lists the integers in decreasing order
and annotates the integers that are prime numbers. Assume nBig,nSm >1. Do not use built-in function isPrime
and do not define your own function. Example output for nBig=6 and nSm=3 is
6
5 is prime
4
3 is prime
4. Complete the following function.
function MyHistogram(v)
% Draw a histogram for the data in v using asterisks in the COMMAND WINDOW (not figure window).
% v is a vector of non-negative values.
% The histogram is scaled so that the largest data value is represented by
% ten asterisks. Round as necessary in order to draw whole asterisks.
% Example: v = [12 4.1 0.5 9.2 20]
% Output in Command Window:
% ******
%**
%
% *****
% **********
5. Review the project questions, lecture examples, section exercises, and prelim review questions!
1

Partial preview of the text

Download Final Exam Review Questions | Introduction to Computing Using MATLAB | CS 1112 and more Exams Computer Science in PDF only on Docsity!

Review questions for the final exam

  1. A Pascal’s triangle with levels 0 to 4 is shown below. Level 0 has a single value, and each value on subsequent levels is the sum of the two entries diagonally above in the previous level of the triangle. For example, the value 6 in level 4 is the sum of the values 3 and 3 in level 3.

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 .. .

level 0 level 1 level 2 level 3 level 4

Complete function pascalVector below to return the row vector corresponding to a specified level of Pascal’s triangle. For example, if level lev is 4, then the returned vector must be [1, 4 , 6 , 4 , 1]. Assume that lev is a non-negative integer. The only Matlab built-in functions allowed are zeros, ones, and length.

Do not use the formula for binomial coefficients to solve this problem. Use a loop (or loops): the vector for each level is based on the vector from the previous level.

function p = pascalVector(lev) % p is the vector corresponding to level lev of Pascal’s triangle

  1. C is a cell array with three components: a real number, a vector of numbers, and a matrix of numbers. Write a fragment to copy all the values in cell array C to a column vector v. For the values in the matrix, copy the values to the vector row-wise. Do not use vectorized code (recall that concatenation is vectorized code and therefore cannot be used in this question).
  2. Given integers nBig and nSm where nBig > nSm, write a fragment that lists the integers in decreasing order and annotates the integers that are prime numbers. Assume nBig,nSm > 1. Do not use built-in function isPrime and do not define your own function. Example output for nBig=6 and nSm=3 is

6 5 is prime 4 3 is prime

  1. Complete the following function.

function MyHistogram(v) % Draw a histogram for the data in v using asterisks in the COMMAND WINDOW (not figure window). % v is a vector of non-negative values. % The histogram is scaled so that the largest data value is represented by % ten asterisks. Round as necessary in order to draw whole asterisks. % Example: v = [12 4.1 0.5 9.2 20] % Output in Command Window: % ****** % ** % % ***** % **********

  1. Review the project questions, lecture examples, section exercises, and prelim review questions!