Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Slides:



Advertisements
Similar presentations
Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables)
Advertisements

Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Intro to Microsoft Excel The Microsoft Excel window will open up ready for you to create a new spreadsheet 1 The ribbon, which sits above the spreadsheet,
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Lecture 13 Decision structures
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
CIS 270—Application Development II Chapter 28—Formatted Output.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
Introduction to PASS, and Exercises on Operators and Basic I/O.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
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.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Overview Input Input statements Menu statements Output Display statements fprintf statements Message boxes.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
MATLAB – More Script Files
EGR 115 Introduction to Computing for Engineers
Control Statements in Matlab
TMF1414 Introduction to Programming
Introduction to Programming for Mechanical Engineers (ME 319)
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Lecture 4 Structure plan
Introduction to Java Applications
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Input Some options: Some options: –Assign values to variables in a command line using the equal ( = ) sign. –Use the input function for interactive assignment of values to some of the variables within your computer program.

Input function General format: General format: The display in the Command window: The display in the Command window: Type a value after the displayed prompt; the variable now has been assigned the value that was entered. Type a value after the displayed prompt; the variable now has been assigned the value that was entered. variable = input('Please enter a value: ') Please enter a value:

Area of a circle: Area of a circle: –Type this into an m-file: –Execute and see this display in Command Window: Hands on Please enter the radius: 5.2 radius = area =

Hands on Free falling object: Free falling object: Type this into an m-file: Type this into an m-file: Execute: See this display in Command Window: Execute: See this display in Command Window: Please enter the initial height (in meters): 25 Please enter the time the object was in the air (in seconds): 3 Please enter the initial velocity (in meters): 5 height_f =

Input strings of text General format: General format: The display in the Command window: The display in the Command window: This is used when the input is a string (e.g., names, months, etc.). The variable, in this case, is a ‘char’ class (or type). This is used when the input is a string (e.g., names, months, etc.). The variable, in this case, is a ‘char’ class (or type). string = input('Please enter your name: ', 's') Please enter your name:

The display variables: The display variables: –You can display the value of a variable in different ways.  Typing “variable =” in the command window  Using the fprintf command: –Type into an m-file: –Executing it displays in the Command Window: Output options x = The value of x is 500.

fprintf() The fprintf() command is one way to display the value of a variable with a label. The fprintf() command is one way to display the value of a variable with a label. General format: General format:  MATLAB code: fprintf('format-string', variable) Note: You can also use the disp() function to display results. See the help on disp() and the examples in the text.

Placeholders in fprintf() To print a variable in your display on the command window, its place must be indicted by % in the format-string followed by the format of presentation ( d, f, e, g ). To print a variable in your display on the command window, its place must be indicted by % in the format-string followed by the format of presentation ( d, f, e, g ). %d : integer notation %d : integer notation %f : fixed point (decimal) notation %f : fixed point (decimal) notation –Most commonly used placeholder %e : exponential notation %e : exponential notation %g : whichever is shorter, %f or %e %g : whichever is shorter, %f or %e

Hands on Type into an m-file: Type into an m-file: Executing it displays in the command window: Executing it displays in the command window: smiles = 7 Sarah smiles 7 times a day.

Hands on Type into an m-file: Type into an m-file: Executing it displays in Command Window: Executing it displays in Command Window: Please enter your number month of birth (i.e. May): August Please enter your number day of birth: 11 Your birthday is August 11!!

When fprintf is used consecutively, MATLAB prints the results on the same line in the command window. When fprintf is used consecutively, MATLAB prints the results on the same line in the command window. –After the second execution of this command: Format Sarah smiles 7 times a day.Sarah smiles 7 times a day.>>

The \n command This command is a linefeed. It commands MATLAB to start on a new line so that sequential outputs are on separate lines. This command is a linefeed. It commands MATLAB to start on a new line so that sequential outputs are on separate lines. –Example –Be sure to use \n, not /n. Erin ate 4 waffles today. >>

Width & precision fields The width field specifies the minimum number of characters to be printed. The width field specifies the minimum number of characters to be printed. The precision field specifies the number of those characters that will show up after the decimal point. The precision field specifies the number of those characters that will show up after the decimal point.

Hands on %8.2f specifies that there can be no less than 8 characters in the displayed output, and that two of them are to follow the decimal point. %8.2f specifies that there can be no less than 8 characters in the displayed output, and that two of them are to follow the decimal point. –Notice the blank spaces in the second output. –Use % to have a % sign show up in output. The weight is pounds

Exercises Write a program tomodel a spring-mass system. The spring constant k (N/m), the mass m (kg), amplitude x o (m), and time, t, elapsed (s) are the input. Display output [i.e., dispay the displacement x in meters of the system at the given conditions]. Write a program tomodel a spring-mass system. The spring constant k (N/m), the mass m (kg), amplitude x o (m), and time, t, elapsed (s) are the input. Display output [i.e., dispay the displacement x in meters of the system at the given conditions]. The formula to use is: The formula to use is:  x=x o cos(ωt), where ω=sqrt(k/m)

Summary I/O input input –variable = input('Please enter a value: ') –string = input('Please enter a value: ', 's') fprintf fprintf –fprintf('format-string', variable) –%f = fixed point (decimal) notation –%e = exponential notation –%g = whichever is shorter, %f or %e