Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asking the USER for values to use in a software 1 Input.

Similar presentations


Presentation on theme: "Asking the USER for values to use in a software 1 Input."— Presentation transcript:

1 Asking the USER for values to use in a software 1 Input

2 Hard-coding 2 When values that are written into the program (instead of being either provided from outside the program, or computed from within the program) we say that these values are “hard-coded” In this example, the values of base and height are hard-coded in the script. %givens base = 30.2; height = 20.3; %result area = 1/2*base*height;

3 User Interface 3 Area Triangle solver screen base height area keyboard External interface on the input side means that the values of base and height is entered by the user on the keyboard. (No longer hard-coded) But first, we must talk about the types of data with which we will be working.

4 Data Types A “data type” is a category for information in the program. Common data types are numeric, strings, arrays, and more. Sometimes we get more specific – for example, we might talk about “integers” as a specific kind of numeric data. The type of data we have will determine if we can do certain things – for example, it would be difficult to find the matrix inverse of a string. 4

5 Data Types What data type is the user asked for? >> How many WHOLE apples? The user is asked to provide a number – specifically an integer. >> Enter your name: The user is asked to provide a sequence of characters – i.e. a string. We can collect both types of data (i.e. both “data types”) using the input() function. 5

6 Keyboard interface There are built-in functions to get information from the user. A “function” is a set of instructions in a special format to allow easy re-use. A name is provided to these instructions, and we can use them in our programs. The functions that come installed with MATLAB are referred to as “built-in” functions. These functions typically require the programmer to create prompts to clue the user on what is being asked. General form of a function call: variableName = input(prompt_string); Two examples given in the MATLAB help: num_apples = input('How many apples? '); name = input('Enter your name: ', 's'); 6

7 input() : Form 1 A prompt is a clue to the user – letting the user know for what input the program is waiting. The prompt is shown on the screen. A prompt string is the sequence of characters that the programmer uses to get a prompt on the screen. Prompt strings are typically (but not always) hard-coded in the program. Strings are hard-coded using single quotes (‘) at the beginning and end of the string. 7

8 input() : Form 1 In the first form, only one string is inside the parentheses – it is the prompt string: num_apples = input('How many WHOLE apples? '); This form of use of the input() function is used when the program requires a numeric value from the user. 8

9 input() : Form 1 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:' 9 Do not confuse “the input to a function” with “input from the user”. An argument (“input to the function”) is part of the program being written. Information coming from the user (“input from the user”) comes from outside the program.

10 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'); 1 st argument 2nd argument The second argument tells the input() function to expect a string from the user. If this argument is present, it must be the letter 's' and nothing else! 10

11 Code for the triangle solver The program requires two user-provided values: a base and a height. To collect them from the user: base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); 11

12 Code for the triangle solver The program requires two user-provided values: a base and a height. To collect them from the user: base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); What the run looks like without the space: Not very pretty. The user-provided value appears right next to the prompt. What is the base in cm?3.2 What is the height in cm?4 12 Notice the space… why?

13 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? 13

14 Wrapping Up The built-in function to collect a single input value from the user is input(). The general form is: varName = input(prompt_string); There are 2 ways to use this function As shown above  to obtain a numerical value With an additional argument ‘s’ placed after the prompt, and separated by a comma  to obtain a string value varName = input(prompt_string, ‘s’); 14

15 Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output 15

16 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? 16

17 Display with a specific format There are multiple ways to display the value of a variable 1. Use the semicolon appropriately 2. use the disp() built-in function 3. use the fprintf() built-in function Each is used for specific reasons 1. Debugging – finding problems with the code 2. Simple programs, simple results (the programmer’s use) 3. Formatted (“pretty”) output 17

18 In a short example, what are the differences? 18

19 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute/DISPLAY the area of the triangle area_cm2 = 0.5 * base_cm * height_cm Not very pretty (nothing indicates where and what the output is) What is the base in cm? 3.2 What is the height in cm? 4 area_cm2 = 6.4000 The number of decimal places cannot be controlled, and it generally defaults to 4 in MATLAB. 19 Notice the missing semi-colon (;)

20 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? disp(area_cm2); Not very pretty (nothing indicates where and what the output is) What is the base in cm? 3.2 What is the height in cm? 4 6.4000 The number of decimal places cannot be controlled using disp() either, and it defaults to 4 as well. 20

21 Using fprintf() 1. fprintf(...) % is the built-in function 2.fprintf(‘format String!’) % The format string allows you to put words and specify a format – what do you want the output to look like? 3. fprintf(‘format string’, var_list) var_list represents a list of variables which are to have their values printed. 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

22 Placeholders – why and how Placeholders are codes used in a format string which let the programmer use values stored in variables (without knowing the actual value) Why are placeholders needed? Suppose a variable, result, holds a value. Let’s further suppose it holds a float. How does the program print the value? Remember – the programmer may not know what value is in the variable. It may be computed during the running of the program. 22

23 Do not print a value Can this be the solution? fprintf('The value in result is: 23.4\n'); Result: >> fprintf('The value in result is 23.4\n'); The value in result is 23.4 >> 23 Can we just say this? fprintf('The value in result is: result\n'); Result: >> fprintf('The value in result is: result\n'); The value in result is: result >> Do not print the variable name

24 Do not forget the placeholder How about this? fprintf('The value in result is: ', result); Nope: >> fprintf('The value in result is: ', result); The value in result is: >> 24

25 Using placeholders The fprintf() function should print the value stored in the variable result. Placeholders to the rescue! Result: >> fprintf(‘The value in result is %f meters.’, result); The value in result is 33.651243 meters.>> 25 fprintf(‘The value in result is %f meters.’, result); “placeholder” (part of format string)

26 Using placeholders The fprintf() function should print the value stored in the variable result. Placeholders to the rescue! Result: >> fprintf(‘The value in result is %f meters.’, result); The value in result is 33.651243 meters.>> 26 fprintf(‘The value in result is %f meters.’, result); DIFFERENT THAN ; OR DISP… 6 decimal places by default.

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

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

29 Printing multiple variables When more than one variable must be printed to the screen, match each variable with its placeholder, and place the list of variables in order of the placeholders. Example age = input(‘Your age? ’); %ask for age name = input(‘Your name? ’, ‘s’); %ask for name fprintf(‘%s is %d years old.’, name, age); %display Sample run: Your age? 47 Your name? Fred Fred is 47 years old.>> 29

30 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 >> 30

31 Format Modifiers (1/4) Once the base placeholder is ready, modifiers further change how the values are displayed. Complete Format Modifier form: %-7.2f Left-justify the value TOTAL width to occupy Number of decimal places 31

32 Format Modifiers (2/4) To display a floating point value to 3 decimal places: fprintf(‘The value of pi: %-7.3f.’, pi); Output: The value of pi: 3. 1 4 2 _ _ The value of pi: 3. 1 4 2. Underscores indicate whitespace – they will not actually show in the output. There are 7 spaces occupied 32

33 Format Modifiers (3/4) When debugging, it can be helpful to “delimit” the output (using dashes and > < symbols) – this lets you see where the “white space” is: fprintf('The value is:\t-->%9.3f<--\n\n', pi); Output: The value is:--> 3.142<-- >> The delimiters make it easy to notice the white space: spaces, tabs, newlines 33

34 Format Modifiers (4/4) Obviously, the decimal place portion of format modifiers will not apply to strings and integers – but the others do! Example name = ‘Fred’; age = 47; fprintf(‘%-6s is %4d years old!\n’, name, age); Output: Fred is 47 years old! Note the spaces 34

35 Wrapping Up Display strings to the screen to: Give an introduction/welcome screen to the software Give error messages when invalid inputs Terminate the program nicely And of course… To display results Omit the semicolon (debugging purposes) Use disp() – debugging purposes as well Use fprintf() – specify a very specific format to display from 0 to an unlimited amount of variables fprintf(…) requires placeholders, with or without any format modifiers: %d, %f, %s, %c, %-10.2f, %-5s, %2d 35


Download ppt "Asking the USER for values to use in a software 1 Input."

Similar presentations


Ads by Google