Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Similar presentations


Presentation on theme: "Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1."— Presentation transcript:

1 Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1

2 Definition A variable is a name to a memory location. The data stored in that location can, of course, VARY at any time. 2 1101110011001010011100000100110000111000101101001111010111 0110111010100110111001001111000011010101011010010101110110 0110000000001010001111100001111011011110010111000011100101 10111111110011011101111010011101011100011111010011101100010 1100111100111010001000011001111010000000100111111100001001 0111100111111000111101101101100011011110010001010110101100 0001111010101011111001110110110110111100110000010000000000 001010000000000111000100000101101111010011001111010101100 0001010000111100000111001111110100001010010110011100111110 100101100010000000010110010010111101011100011110110100101 1111100100011100111001110101000110001100011001001011101101 0001101011010100110010111001111101000111111100011110110100 001000001000101101101100111011110000001100000010100000101 11000110011011010011011100010011000100101 Catia Project MATLAB Software Variable named area Variable named base

3 An important distinction A variable has a name, a value (or multiple values), a type, and a location. When you refer to a variable, you typically refer to its name, but you actually mean the value that it currently has. – Example: y = x + 3 This really means “take the current value of x, add 3, and store that value in a variable named y” 3

4 Example 4 Area of triangle Problem: Solve the area of a triangle where the base is 12.3, one side is 8.9, and the angle between those two sides are 34 degrees. 1) Summarize the problem – Givens base = 12.3 side1 = 89 angle between two sides = 34 degrees – Find area of triangle

5 Example 5 Area of triangle 2) Diagram 3) Assumptions lengths are in meters 4) Theory area = ½ * base * height height = side * sin(angle) h

6 Example 6 Area of triangle 5) Solve 6) Verify Accuracy Hard to do but sind(34) is a little more than 0.5, so h would be a little more than 4.5. Using 4.5 would give an area of roughly 28, so something a little more than 28 makes sense. 7a) Algorithm Define base, side and angle Calculate area Show result

7 BAD code Why would this be a bad code?  Absolutely NOTHING is re- usable 7

8 Rules to follow when naming variables AND script files 1.It cannot contain spaces: Use_the_underscore, or capitalizeTheFirstLetterOfAdditionalWords 2.It cannot start with a digit. However, it can have digits in the rest of it. 3.It cannot contain any special characters (other than the underscore of course!) 4.It should not be any keywords MATLAB knows, or 5.It should not be the filename of any saved files Also: All variables are case sensitive. AGE is not the same as age. Strong Advice: Avoid single letters, especially i and j. The name should simply represent its content. 8

9 Best habit to naming a variable The name should simply represent its content. 9

10 Avoid single letters 10

11 Final Script File 11

12 CAUTION CAUTION: This isn’t math anymore sind() calculates the sine of an angle in degrees MULTIPLICATION is NOT implied in MATLAB – in math: (2)(5)(5.5) = ? – in MATLAB: 12

13 Basic Data Manipulation How about solving equations? Normal algebra ≠ programming – Assume the following mathematic equation: z = x + y In algebra: when z is 10 and y is 7, what is x equal to? 13 z = x + y 10 = x + 7  Solve for x, obtain _____ Look at MATLAB:

14 Basic Data Manipulation 14 Assign values to variables z and y

15 Basic Data Manipulation 15 Assign values to variables z and y Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

16 Basic Data Manipulation In MATLAB, all the known variables must be on the right side of the equal sign before executing the command. At any time, only one variable can be on the left. 16 Assign values to variables z and y Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

17 “A scalar” A single value. Example: the number 7 is a scalar. age = 17 %creates a scalar variable weight = 145.32 %another scalar variable 17

18 Basic Data Understanding How exactly is the data stored in the memory?  binary (i.e. machine) language: 0 and 1’s How is the number 2 represented then? Remember that the symbol we see is NOT the value represented by it. For example: what does this equal? ||||+= 18 84 579 (in Futurama Alien Language 1) 0100000000000000000000000000000000000000000000000000000000000000

19 Definition – a DATA TYPE Simply put: "The type-of-data stored in a variable" 19

20 Size and Weight Data-types are means of – controlling how much memory is used per “unit", and – Specifying/defining what kind of operations apply to a variable Why do data-types matter? Device“Hard Drive Size” Apollo 11 Computer 2 KB Cell phoneA couple GB (4, 8, 16) TabletsAround 32GB LaptopsAround 640 GB DesktopUp in the Terabytes (TB) now! 20

21 Can’t MATLAB figure it out? Why do I need to know what type of data a variable holds? Can’t MATLAB figure that out? – Yes, when it knows up front how much space a value will take up, but you won’t be using hard-coded values all semester. 21

22 User Interface Instead of the programmer specifying the values, the program uses an external interface where the values are entered by the user 22 (No longer hardcoded) Use of input: Here we use the word “input” to mean “information coming into the solver from outside” – input doesn’t always come from the user.

23 Keyboard interface There are built-in functions to get information from the user. These functions typically ask the programmer to supply ‘prompts’ to clue the user on what is being asked. input() variableName = input(‘prompt’); Two examples given in the MATLAB help: num_apples = input('How many apples? '); name = input('Enter your name: ', 's'); 23 Use of input: Here we use the word “input” to mean a specific function that collections information from the user.

24 Syntax is data type dependent What data type is the user asked for? – num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – here, an integer. – name = input('Enter your name: ', 's'); The user is asked to provide a sequence of characters – i.e. a string. 24 These are the only 2 forms of using the input() built-in function.

25 Form #1. input()- integer In the first form, only the 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? ' 25 function call argument

26 Form #2. input(…, 's') - string 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 the 2 nd argument is present, it must be the letter 's' and no other letter! 26 1 st argument 2nd argument For this function, the second argument tells the input() function to expect a string from the user.

27 Code for the triangle solver The program requires two values: a base and a height. To collect them from the user: Base = input('Enter base of triangle: '); Height = input('Enter height of triangle: '); Units = input('Enter unit system chosen: ', 's'); – Without the space it’s not very pretty, and the user may actually press the space bar, which is a problem for strings: 27 Notice the space… why?

28 Next chapter? 28 How do we display the results to the user…? ….in a clean & professional format?


Download ppt "Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1."

Similar presentations


Ads by Google