1 / 37

Topic Review for Exam 1

Topic Review for Exam 1. 0. Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions Computer 101 Developing algorithms and programs Variables and data types User I/O Operators Conditionals (IF). 1. Computer 101.

unity-pope
Download Presentation

Topic Review for Exam 1

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. Topic Review for Exam 1 0. Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions • Computer 101 • Developing algorithms and programs • Variables and data types • User I/O • Operators • Conditionals (IF)

  2. 1. Computer 101 • The Von Neumanncomputer architecture is mostly what we use today. The architecture separates a computer in 3 major parts: • The Central Processing Unit (CPU) • The computer memory • The Input/Output (I/O) devices ? ? CPU + memory Screen=output Mouse=input Speakers=output Knob=input Keyboard=input History 4/4

  3. Exam Topics Software: OS, apps, compilers 1. Describe the purpose of a compiler. 2. What differentiates the Operating System (OS) from an application (app)? Answer: Next slide 

  4. Categories of software • Software contains the instructions the CPU uses to run programs. There are several categories, including: • Operating systems(OS) – manager of the computer system as a whole • Software applications– commercial programs that have been written to solve specific problems • Compilers / Interpreters- to ‘translate’ programs written by people into something understandable by the machine

  5. Generations of Languages used to write software 1) Machine language – also called binary language. Sequence of 0’s and 1’s. 2) Assembly language – each line of code produces a single machine instruction (add, subtract…), see bottom of page 11. 3) High-level language – slightly closer to spoken languages. add b,c add a,b a= a + b + c; This line does the same as the two above.

  6. Finally… MATLAB • Is an interpretedlanguage – does not require compilation, but instead has an interpreter running behind the scenes • Has an interactiveenvironment – • “In the MATLAB environment, you can develop and execute programs that contain MATLAB commands. You can execute a MATLAB command, observe the results, and then execute another MATLAB command that interacts with the information in memory, observe its results and so on.”

  7. 2. Develop a Solution Follow these steps in order: • State the problem clearly • Identify the givens vs. the results wanted • This will be referred as the I/O diagram • Manually solve the problem • Computerize the solution • Test, test, test!!!

  8. 3. MATLAB interface, Variable and Data Types • Know variable and file naming constraints • Understand the functions listed in the lecture notes [e.g. sin() , cos() , sind() , cosd() , sqrt() , etc] – know the difference between sin() and sind(), for example. • What is ans? • What does clear do? • What about clc? • How do you suppress default output?

  9. Frame #3 “Workspace” Frame #2 “Current Directory” Frame #1 “The Command Window” It will show the variables that have been created. “Your Calculator Screen” You can do exactly as on your calculator: add, subtract, divide, multiple, use parentheses to override the order of operations… Later on, you will realize you can do a LOT more in this frame. This frame shows the files (excel, text, Matlab files) that can immediately be used. Frame #4 “Command History” It will show all the commands executed previously.

  10. Exam Topics Starting with MATLAB

  11. Exam Topics Starting with MATLAB

  12. Variables • A variable is a name given for a memory location • “Assigning a value to a variable” means to place a value in the memory location associated with the variable name • Matlab usually shows what the variables are - in the Command Window, but also in the Workspace.

  13. Functions • Common functions • sin(), cos(), tan(), sind(), cosd(), tand(), • asin(), acos(), atan(), asind(), acosd(), atand() • exp(), sqrt(), log(), log10() • abs(), round() 13

  14. “Suppressing” the output • Second version, with semi-colons Using a semicolon on these lines “suppresses the output”. In other words, it does not show the result of the command. But… did the commands do anything?

  15. Data Types A data type is a category of information. The data type determines how information is stored in the computer and how it can be used. • Integers whole numbers • Floats numbers with fractional portion • Strings sequences of characters

  16. 4. Inputs and Outputs • What data-typeis the user asked for? • num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – specifically an integer. • name = input('Enter your name: ', 's'); The user is asked to provide a sequence of characters – i.e. a string. These are the only 2 forms of using the input() built-in function.

  17. Form #1. input() • In the first form, only 1 prompt is inside the parentheses: num_apples = input('How many WHOLE apples? '); There is only one argument to the function. Arguments are inputs to the function – information being sent into the function so it can do its job. The one argument is the prompt string: 'How many WHOLE apples:'

  18. Form #2. input(…,‘s’) • In the second form of the input() function, there are TWO arguments: the prompt string, and another string: ‘s’ name = input('Enter your name: ', 's'); • If this argument is present, it must be the letter 's' and no other letter! 1stargument 2nd argument

  19. Output The fprintf() function The format string Placeholders Format modifiers left justification width specifier precision specifier Escape sequences

  20. Using fprintf() • fprintf(...)% is the built-in function 2. fprintf(‘format String InSeRtEd hErE!’) % The format string allows you to put words and specify a format (UPPER CASE vs. Lower case, and punctuation only) • fprintf(‘format string with placeholders’, list of variables to print are inserted here. If more than one variable is to be printed, each is separated by a comma from the previous one) % Placeholders allow a specific format to be set (aligned right, and 2 decimal places for example)

  21. Stop for vocabulary • Just a quick recall about the vocabulary. fprintf(‘The value in result is %f meters.’, result); “placeholder” (part of format string) variable to be printed “function call” ‘format string’

  22. Most common placeholders • Each data-type has a placeholder • Integer %d • Floats %f • Strings %s • A single letter %c

  23. Special Characters • Escape sequences can also be used within the format string: \n - this will create a new line when printing the string \t - tab (tabs the text to the right) '' - this will place one apostrophe in the final sentence displayed • Example of all three: >> fprintf('%s''s age:\t\t%d years old\n\n', name, age); Fred's age: 47 years old >>

  24. Format Modifiers • Once the base placeholder is ready, modifiers further change how the values are displayed. • Complete Format Modifier form: %-7.2f Nb. of decimal places Left-justify the value TOTALwidth to occupy

  25. 5. Operators & Operands • Operators work on operands. There are 2 types of operands: • Numerical 1, 3.5, -47 • Logicaltrue, false • Arithmetic (+,-,/,*,^) and relational (<,<=,>,>=,==,~=) operators work with numerical operands • Boolean (&&,||,~) operators work on logical operands

  26. Boolean Operators • These operators take logical values and perform some operation on them to yield a logical value • Two Boolean operators allow to COMBINE relational expressions • && Logical AND • || Logical OR • One Boolean operator allows to NEGATE the result • ~ Logical NOT • “Negates”: turns true values into false, and false values into true

  27. Operators: unary vs. binary • Operators can be unary – taking only one operand: y = -x; opposite = ~result; • Or binary operators – taking two operands: z = x * y; z = x + y; z = x – y; %both unary and binary! z = x / y; z = x ^ y; x >= 7 (x<3) && (3>y)

  28. Operators: Output values Type Input values Output values Arithmetic: Numbers Numbers e.g. 5 * 315 Relational: Numbers Logical e.g. 5 < 3false Boolean: Logical Logical e.g. ~truefalse

  29. Unary vs Binary operators &, &&, |, ||, +, -, *, /, ~, <, =, == From the set of operators above… Which one(s) can be used as either unary or binary operators? Which one(s) are only unary operators?

  30. Name these operators: Relational <, >, <=, >=, ==, ~= Boolean &&, ||, ~ Know the true and false keywords

  31. Q: Operators

  32. 6. IF: General template if <logical expression 1> <code block 1> elseif <logical expression 2> <code block 2> . . . elseif <logical expression n> <code block n> else <default code block> end

  33. Example: weekend? weekday? % ask user for day day = input(‘What day # is it? ’); % find which day it is if day == 7 %saturday state = ‘weekend’; elseif day == 1 %sunday state = ‘weekend’; else %any other day state = ‘weekday’; end

  34. Using Logical OR % ask user for day day = input(‘What day number is it (1-7)? ’); % find which day it is if day == 7 || day == 1 %Saturday or Sunday state = ‘weekend’; else %any other day state = ‘weekday’; end

  35. Common Mistakes WHAT YOU CANNOT DO: is shortcut the expression %if angle is between 0 and 90 degrees if 0<=angle<90 quadrant = 1; elseif 90<angle<=180 %quadrant 2 quadrant = 2; end DOESNOTWORKRIGHT • Instead, rewrite each condition separately! • if 0<=angle && angle<90 • quadrant = 1; • elseif 90<angle && angle<=180 • quadrant = 2; • end

  36. Q: Conditionals Boolean expressions Using &&, ||, and ~ What is the output of this code? a = true; b = 1; c = -4; if a && ~(b>c) disp('IF'); else disp('ELSE'); end

  37. Q: Conditionals

More Related