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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Exam 1: Review 1.History of computers 2.Fundamentals of computer architecture 3.Developing algorithms and programs 4.Variables and data types 5.User I/O.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Lecture 4 Input/Output Conditional Statements 1. Data Types 2. Input/Outputs 3. Operators 4. Conditionals 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Asking the USER for values to use in a software 1 Input.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer Developing algorithms.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Python Let’s get started!.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Overview Input Input statements Menu statements Output Display statements fprintf statements Message boxes.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Python Let’s get started!.
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
Introduction to Scripting
Basic Elements of C++ Chapter 2.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

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

Asking the USER for values to use in a software 2 Input CAUTION: We use the word “input” in two different ways – be sure you understand how it’s being used.

Data Types A “data type” is: An “information category” – information that looks and acts similarly. Common data types (more will come later): Integers whole numbers Floats {floating point} numbers with a fractional part Strings {char}sequence of characters 3

“Solve for the area of a triangle” 4 Area Triangle solver

Inputs are hardcoded 5 Area Triangle solver screen area No external interface on the input side means that the values of base and height are hard-coded in the script. (They are not inputs) base height %givens base = 30.2; height = 20.3; %result area = 1/2*base*height;

User Interface 6 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 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.

Keyboard interface There are built-in functions to get information from the user. 7

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() general form variableName = input(prompt); 8 Use of input: Here we use the word “input” to mean a specific function that collections information from the user.

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() general form variableName = input(prompt); Two examples given in the MATLAB help: num_apples = input('How many apples? '); name = input('Enter your name: ', 's'); 9

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 – specifically an integer. 10

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. 11

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. These are the only 2 forms of using the input() built-in function. 12

Form #1. input() 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:' 13

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 14

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 For this function, the second argument tells the input() function to expect a string from the user. 15

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! 1 st argument 2nd argument 16

Code for the triangle solver Our program requires two 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? '); 17

Code for the triangle solver The program requires two 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? '); 18 Notice the space… why?

Code for the triangle solver The program requires two 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? '); Without the space it’s not very pretty, and the user may actually press the space bar, which is a problem for strings: What is the base in cm?3.2 What is the height in cm?4 19 Notice the space… why?

% 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 % Display the answer on the screen 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? 21

Wrapping Up The built-in function to prompt (i.e. ask) the user for a value (integer, float, string) is input() varName = input(prompt); 22

Wrapping Up The built-in function to prompt (i.e. ask) the user for a value (integer, float, string) is input() varName = input(prompt); There are 2 ways to use this function Just the prompt argument  obtain a numerical value With an additional argument ' s ' placed after the prompt argument, and separated by a comma  to obtain a string value 23

Wrapping Up The built-in function to prompt (i.e. ask) the user for a value (integer, float, string) is input() varName = input(prompt); There are 2 ways to use this function Just the prompt argument  obtain a numerical value With an additional argument ' s ' placed after the prompt argument, and separated by a comma  to obtain a string value Note: there is no method to get two values in one shot, write as many input() calls as needed. There are other ways to ask for values which we will cover later… 24

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

% 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? 26

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 27

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 28

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) 29

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 30

In a short example, what are the differences? 31

% 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 32

% 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 = The number of decimal places cannot be controlled, and it generally defaults to 4 in MATLAB. 33

% 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); 34

% 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?

% 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? The number of decimal places cannot be controlled using disp() either, and it defaults to 4 as well. 36

Using fprintf() 1. fprintf(...) % is the built-in function 37

Using fprintf() 1. 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) 38

Using fprintf() 1. 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) 3. 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) 39

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. 40

Do not print a value Can this be the solution? fprintf('The value in result is 23.4\n'); 41

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

Do not print the variable name Can we just say this? fprintf('The value in result is: result\n'); 43

Do not print the variable name 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 >> 44

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

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: >> 46

Using placeholders The fprintf() function should print the value stored in the variable result. Placeholders to the rescue! fprintf('The value in result is %f meters.', result); “placeholder” (part of format string) 47

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 meters.>> 48 fprintf('The value in result is %f meters.', result); “placeholder” (part of format string)

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 meters.>> 49 fprintf(‘The value in result is %f meters.’, result); DIFFERENT THAN ; OR DISP… 6 decimal places by default.

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

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

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.>> 52

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

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 54

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: _ _ The value of pi: Underscores indicate whitespace – they will not actually show in the output. There are 7 spaces occupied 55

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 56

Format Modifiers (4/4) Normally we don’t use the decimal place portion of format modifiers for strings, and it doesn’t work at all for integers – but the other portions still work! Example name = ‘Fred’; age = 47; fprintf(‘%-6s is %4d years old!\n’, name, age); Output: Fred is 47 years old! Note the spaces 57

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 58

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 59

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 60