Docsity

CMPSC 200 MIDTERM EXAM REVIEW – MATLAB PROGRAMMING, Exams of Computer Science

Partial preview of the text

Download CMPSC 200 MIDTERM EXAM REVIEW – MATLAB PROGRAMMING and more Exams Computer Science in PDF only on Docsity!

CMPSC 200 MIDTERM | COMPUTER

SCIENCE STUDY GUIDE & PRACTICE

QUESTIONS | 100% VERIFIED | A+

GUARANTEE

Updated Questions and Answers | 100% Verified Exam Prep

computer a programmable electronic device that can store, retrieve, and process data GIGO garbage in, garbage out CPU central processing unit 2 components of CPU ALU: Arithmetic and LogicControl Unit: fetches next command

volatile memory fast, temporary storage (RAM, DRAM) non-volatile memory slower, permanent storage (ROM, CD) program set of instructions to perform specific tasks algorithm set of steps for doing something operating system (OS) allocated computer resources software consists of the OS and other programs used by the computer high-level language easy to understand, hard for computers low-level language hard to understand, better for computers machine language binary digits (1s, 0s) good for computers compiled languages instructions are converted to machine instructions prior to run time then saved as a filewhich can be run (C++)

interpreted languages (^) instructions are saved in the same format as they were written and converted to machineinstructions at run time, easier to develop (MATLAB)

MATLAB matrix laboratory

rem(x,y) remainder of x divided by y exp(x) exponential (e^x) log(x) ln(x) log10(x) log base 10 of x logb(x) (change of base) logd(x)/logd(b) round(x) rounds to integers fix(x) truncation(removes decimals) floor(x) rounds to integer closest to -inf ceiling(x) rounds to integer closest to inf factor(x) prime factors of a number x gcd(x,y) greatest common denominator of x and y lcm(x,y) least common multiple of x and y rats(x) converts decimal to fraction factorial(n) factorial primes(x) find all prime numbers less than or equal to x isprime(x) is a number prime (1 for true, 0 for false) trig functions (^) sin, asin, sind, asind, sinh, asinh (repeat for all trig) help _____ built in help

doc _____ pop-up help transpose(x) flips a matrix over its diagonal mean(x) average, when x is a vector it returns the row vector mean of each column median(x) median mode(x) mode sum(x) sum (if x is a matrix, returns a vector of ___'s from each column) prod(x) product (if x is a matrix, returns a vector of ___'s from each column) cumsum(x) cumulative sum (if x is a matrix, returns a vector of ___'s from each column) cumprod(x) cumulative product (if x is a matrix, returns a vector of ___'s from each column) sort(x) sorts elements in ascending order (if x is a matrix it sorts the columns) sort(x, 'descend') sorts in descending order sortrows(x) sorts the rows in a matrix in ascending order on the basis of the elements on the firstcolumn, keeping the row intact

sortrows(x,n) same as sortrows(x,n) but selects elements from column n size(x) returns the number of rows and columns in x [a,b] = size(x) assigns number of rows and columns to a and b respectively length(x) largest number of elements in any dimension numel(x) total number of elements std(x) sample standard deviation

inf IEEE representation for positive infinity NaN not a number clock returns 6 element array of year month day hour minute second fix(clock) displays clock in military time (more readable) date character string listing the date eps machine epsilon ... ellipses, can be used to continue a thought onto the next line A(2,2) extract an element from a matrix (Row, Column) A(:,1) extract rows or columns from a matrix (Row, Column) A(:) convert a matrix to a column vector A(1,end) call last element in a row or column zeros(m)/zeros(m,n) creates a matrix of zeros ones(m)/ones(m,n) creates a matrix of ones diag(A) extracts the diagonal of A fliplr(A) flips a matrix into its mirror image from left to right flipud(A) flips a matrix vertically magic(n) (^) Creates a "magic" matrix; constructed from the integers 1 through n^2 with equal row andcolumn sums. n must be a scalar greater than or equal to 3.

plot(x1, y1) plots y vs. x, different colors are used ifmore than 1 x-y pair is specified

plotyy(x, y1, x, y2) plots separate y axes comet(x,y) animated version title('some text') adds a title xlabel('some text') adds x/y/z labels grid on/grid off turns grid lines on and off pause pauses the graph (waits for keystroke) pause(time) pauses for a certain amount of time figure calls new figure figure(n) calls a figure n, creating it if it does not already exist hold on/hold off freezes the current plot so you can add more plots to it clf clears current plot clf(n) clears plot n close closes current figure close all closes all figures title('\alpha') greek letters (case sensitive) works with titles, axis labels legend('string1','string2',...) creates a legend; you should have the same number of strings as you do data sets/plotlines

surf(x,y) surface plot: 3D representation of 2D matrices/datasurf plots (if z is included, z gives information about color)

colormap('type') (^) color maps (see doc colormap) contour(x,y,z) contour plots (2D representations of 3D surfaces) surfc(x,y,z) surf plots + contour plots computer functions "A piece of computer code that accepts an input argument from the user and providesoutput to the program"

four key parts to a function [output] = name (input) comments nargin('') determines the number of input arguments (-1 means there are multiple) nargout('') determines the number of output arguments (-1 means there are multiple) local variables - local scope- local function variables are not displayed in the workspace, local script variables are displayed in the workspace- variables are only available to the code they are associated with (functions or script)

global variables - global scope- are displayed in the workspace

  • variables are available to other sections of code if they are made available type _____ (^) accessing M-files pathtool save functions so MATLAB looks for them when you call them @ (anonymous functions) simple functions you create that are available elsewhere in the program subfunctions - groupings of functions all continued within one primary function file- The name of the function file is the same name as the primary function (the first function listed)

input('Enter a number: ') request input number from the user input('Enter a matrix: ') request input matrix from the user number display doc "format short" input('Enter your name: ')/input('Enter your name: ','s') request input name from the user (character string input must be in single quotes)'s' tells MATLAB to expect string input and therefore the single quotes are no longer necessary disp print the contents of a variable without its label num2str allows you to print strings and numbers on one line by converting numbers to a characterarray

fprintf formatted print functionfprintf(format-string, var, ...)

%f % is a place holder, f is a type field (fixed point notation) %e exponential notation %d decimal notation %i integer notation %g the shorter of %f or %e %c character notation (one at a time) %s string of characters fprintf('blah blah \n') new line \r carriage return

== equality check ~= not equal to & and: used with vectors | or: used with vectors ~ not: used with scalars/vectors xor exclusive or && shortcut and: used with scalars || shortcut or: used with scalars pseudocode verbal description of code oval indicates the beginning or end of a section of code parallelogram indicates input or output processes diamond indicates a decision point rectangle indicates calculations find searches a matrix for given search criteria if elseif, else, end (can have either elseif or else), once a true statement is found the rest areignored

switch (^) case, otherwise, end menu whatever = menu('title','option 1','option 2',...) elseif and else if are NOT interchangeable

loop characteristics low performancegood alternatives: array operations, find command, code vectorization

three types of loops for, while, do loop for loop commonly used if you know how many times it needs to be runat the end of the loop, the index variable keeps its most recent value use index 'k', 'i'/'j' are not good in MATLAB because they represent sqrt (-1)when nesting for loops, the inner counter resets every time the loop is reinitialized

counter a variable that keeps track of some parameter of interestmain types: how many, how much

break causes the termination of the smallest enclosing for or while loop continue used to skip the rest of the loop, advancing to the next loop pass