1 / 95

Final Exam Review

Final Exam Review. From the first half of the class. for loops, if, while, cout the diff between if and else if the diff between variables and functions int presentValue; int presentValue( ); the diff between defining functions (outside of main) and calling functions (from main).

zenia
Download Presentation

Final Exam Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Final Exam Review

  2. From the first half of the class • for loops, if, while, cout • the diff between if and else if • the diff between variables and functions • int presentValue; • int presentValue( ); • the diff between defining functions (outside of main) and calling functions (from main). int presentMenu(); choice = presentMenu(); int presentMenu( ) { main

  3. if (a == b) { // do this } else if (c == d) // depends on a and b or if (c == d) // a and b are NOT critical { // do that }

  4. bubble sort ordering a list of numbers

  5. start with any array const int arraySize = 20; // cannot change value int list[ arraySize ] = { 325, 122, 15, 34, 99, 123, 78, 0, 89, 65, 23, 67, 33, 9, 41, 31, 678, 342, 821, 2 };

  6. we'll need to swap array elements e..g. swap items list[4] and list[12]… values: 99 33 list[4] = list[12]; // list[4] gets 33 list[12] = list[4]; // list[12] gets 33 !

  7. we'll need to swap array elements e..g. swap items list[4] and list[12]… values: 99 33 // use a temporary variable… temp = list[12]; // temp gets 33 list[12] = list[4]; // list[12] gets 99 list[4] = temp; // list[4] gets 33

  8. nested for loops for (x=0; x<5; x++) // 0 thru 4 {cout<< "X: " << x << endl; for (y=0; y<10; y++) // 0 thru 9 FOR EVERY X{ cout<< " Y: " << y << endl; } }

  9. for (x=0; x<arraySize; x++) // 0 thru 19 { for (y=x+1; y<arraySize; y++) // x+1 thru 19 { if ( list[x] > list[y] ) // test which is greater { // swap greater and lesser so lesser is 1st temp = list[y]; list[y] = list[x]; list[x] = temp; } } } BUBBLE SORT

  10. Excel

  11. this is cell C3 Row H6:K6 Column E7:E16 Rectangle J14:L18 know what a colon means

  12. What is a formula? • A calculation using numeric data and cell values • Data may be numbers OR references to other cells • Excel performs calculation and displays the result, updating in real time • Excel updates cell formula if the cell containing it is moved

  13. Entering a Formula • Always begins with the equals sign (=) • Use numbers and/or cell references • Use arithmetic operators + addition - subtraction / division * multiplication ( ) parentheses know what an equal sign means in Excel vs. C++

  14. the best part Copy and paste are RELATIVE…Select cell, then: • Copy/Paste • CTRL + C copy • CTRL + V paste • Fill Handle • Move mouse over bottom right-hand corner of cell • It becomes a cross-hair (not an arrow) • Right-click and Drag to copy and paste formulas

  15. Absolute References • Allows cell reference to remain same as its copy/pasted • Absolute: $A$1 • Mixed: A$1 or $A1 • Relative : A1 know what $ means

  16. Common Functions • = SUM( range) • = AVERAGE( range ) • = MAX( range ) • = MIN( range ) • = STDEVP( range )

  17. Numerical Methods in Excel

  18. In flight, thethrustproduced by an aircraft’s engine is used to overcome the drag of flying through thick air. • Thrust and Drag are FORCES, measured in Newtons • Since the plane is flying through "liquid" air, • Power = VELOCITY x Force • is relevant to keeping the plane aloft.

  19. double click on the worksheet tab and rename the worksheet to ‘calculations’ • create the table of calculation constants • create the table for calculation results with the formatting shown

  20. enter the formula to calculate the Power Required • note that the calculated power is divided by 106 so that the results are shown in MW • format the calculated results as shown note the $

  21. enter the formula to calculate the Power Available • note that the calculated power is divided by 106 so that the results are shown in MW • format the calculated results as shown

  22. enter the formula to calculate the Climb Velocity or Rate of Climb • note that the excess power is multiplied by 106 to convert to N*m or Watts. • format the calculated results as shown

  23. Plot the Power Required and Power Available versus Flight Speed - use SCATTER rather than LINE plot (will pick leftmost column as the independent variable) know the difference between line and scatter (line uses cell row number as x axis and plots every column; scatter uses left column as x axis, and plots the right column(s)

  24. format the plot as shown

  25. Intro to MATLAB

  26. variables type here files and folders know Command Window vs script (m) file. command history

  27. some useful commands • variable (scalar) definition: x = 10 • a scalar is a single-valued variable, but MATLAB regards it as a 1x1 matrix • no semicolons needed, although a semicolon will stop echo • comments start with %, used mostly in scripts know what the semi-colon does in MATLAB vs. C++

  28. assignments work, as in C++ t = 0 t = t + 1 equal sign the same in C++and MATLAB, different in Excel

  29. Enter an array degrees = [ 0, 10, 20, 30, 40, 50, 60 ] or degrees = [0 : 60 ] 0,1,2,3,4,5,6,7,8 … 60 degrees = [ 0 : 20 : 60 ] 0, 20, 40, 60 know what a colon means

  30. plot a sine curve

  31. >> y = sin(x) y = Columns 1 through 8 0 0.1736 0.3420 0.5000 0.6428 0.7660 0.8660 0.9397 Columns 9 through 16 0.9848 1.0000 0.9848 0.9397 0.8660 0.7660 0.6428 0.5000 Columns 17 through 19 0.3420 0.1736 0.0000

  32. >> plot( x, y )

  33. m files • type commands into MATLABs "notepad" • load/save as usual • two ways to run: • hit the PLAY button • Type the filename at the command prompt know the difference between m and mat files.... mat files are the result of the s"save" command, and merely save your variables, not your commands

  34. To define a matrix, you can treat it like a column of row vectors A = [ 1 2 3 4 5 6 7 8 9]

  35. Rows may be separated by semi-colons rather than a new line B = [ 1 2 3; 4 5 6; 7 8 9] • You can also define a matrix like that: D = [1:5; 6:10; 11:2:20] last row - 11 to 20, by twos

  36. Matrix definition • a = [ 1 47 65 89 22] • b = [25 55 22 43 72] • c = a * b % dimensions? • d = b * a % dimensions?

  37. You can try to define some special matrices E = ones(2,3) % ones(m,n) gives an m*n matrix of 1's F = eye(3) % The identity matrix eye(n) is a matrix of zeros except for having ones along its leading diagonal G = [-3 4 2], H = diag(G) % define a vector and give a diagonal matrix I = rand(1,3) % rand(m,n) gives m*n matrix of uniformly distributed random numbers on (0,1)

  38. Individual Array elements • Let's start with the simple case of a vector and a single subscript. The vector is  v = [16 5 9 4 2 11 7 14] • The subscript can be a single value.  v(3)     % Extract the third element  ans =        9 • Colons work: v(1:4) ans = 16 5 9 4

  39. Indexing Matrices with Two Subscripts • Now consider indexing into a matrix.  A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 • indexing in matrices is done using two subscripts - one for the rows and one for the columns. A(2,4)   % Extract the element in row 2, column 4  ans = 8

  40. surface plot • x=[1:10] • y=transpose(x) • z= y * x • figure(1) • surf(x,y,z) • figure(2) • z = rand(10) • surf(x,y,z)

  41. Matrix Math, MATLAB, and data tables

  42. matrices • usually represent a table, or a data relationship • or - referring to C++ - correlated arrays 3 x + 4y + 16z = 12 3 4 16 12 6x - 17y - 23z = -168 6 -17 -23 = -168 x + 42y + 101 z = 140 1 42 101 140 3 equations in 3 unknowns, represented by matrices

  43. matrix multiplication simple A * B = C each Row in A x each Col in B = (Row,Col) item in C a11b12 + a12b22 = c12

  44. 1 2 3 * 1 2 = 22 28 4 5 6 3 4 49 64 5 6 2 X 3 * 3 X 2 results in a 2 x 2 inner dimensions must be the same out dimensions reveal size

  45. represent N equations in N unknowns 1)   x + y − z = 4   2)   x − 2y + 3z = −6   3)   2x + 3y + z = 7 1 1 -1 x 4 1 -2 3 * y = -6 2 3 1 z 7 know: coefficient matrix vs. variables matrix vs. constants matrix

  46. the Identity Matrix (1s in the diagonal) Any matrix times an appropriately sized identity matrix yields itself 3x2 2x2 3x2 23 45 1 0 23 45 17 22 * 0 1 = 17 22 1 32 1 32 Size of ID matrix: SQUARE, dictated by COLUMNS of the multiplying matrix

  47. what is a matrix inverse? • A matrix multiplied by it's Inverse yields the identity matrix • A * AI = Identity • "Singular" matrices have no Inverse

  48. Why? A 1)   x + y − z = 4   2)   x − 2y + 3z = −6   3)   2x + 3y + z = 7 multiply both sides by AT: 1 1 -1 x 4 1 -2 3 * y = -6 2 3 1 z 7 x 4 AI * A * y = AI * -6 z 7 find AI, and you can solve for x, y, z

  49. mat files Saving data: http://www.mathworks.com/help/techdoc/ref/save.html Setting output format: http://www.mathworks.com/help/techdoc/ref/format.html

More Related