03_GENN004m_InputOutput
-
Upload
bishoy-emile -
Category
Documents
-
view
214 -
download
0
Transcript of 03_GENN004m_InputOutput
-
7/29/2019 03_GENN004m_InputOutput
1/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
Input and Output
Algorithms
Input and Output Functions
Outline
Algorithms
Input Function
Controlling Output
-
7/29/2019 03_GENN004m_InputOutput
2/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
Algorithms
Before writing any computer program, it is usefulto first outline the steps that will be necessary.
An algorithm is the sequence of steps needed tosolve a problem.
In a modular approach to programming, theproblem solution is broken down into separatesteps, and then each step is further refined untilthe resulting steps are small enough to be
manageable tasks. This is called the top-downdesign approach.
Algorithm Example
Calculating the area of a circle
1. Get the input: the radius
2. Calculate the result, the area
3. Display the output
For most programs, the basic algorithm
1. Get the input(s)2. Calculate the result(s)
3. Display the result(s)
-
7/29/2019 03_GENN004m_InputOutput
3/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
Input Function
MATLAB allows you to prompt for inputinteractively
This is through the input function:
input(prompt_string)
input(prompt_string, options_string)
prompt_stringtells MATLAB what text todisplay while waiting for a user to type something
options_stringlets you specify how
MATLAB should interpret the input
Use s to indicate the input is a string
Input Examples
Try
>> radius=input(Enter the radius:)
>> user_name = input(What is your name? \n, s)
>> user_age = input(What is your age? \n)
-
7/29/2019 03_GENN004m_InputOutput
4/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
Controlling Output
MATLAB gives you a number of ways to
control how output is displayed
Changing how numbers are displayed does
not change the precision of the underlying
computations
To change the default precision of echoed
numbers in the Command Window, use theformat command
format Optionsformat Description Example
short 4 digits after decimal 3.1416
long 14 digits after decimal 3.141592653589793
short e 5 digits plus exponent 3.1416e+000
short g 5 digits,with or without exponent 3.1416
long e 15 digits plus exponent 3.141592653589793e+000
long g 15 digits, with or without exponent 3.14159265358979
bank dollars and cents 3.14
hex 4-bit hexadecimal 400921fb54442d18
rat approximate ratio of small integers 355/113
Try
>> x=pi;
>> format short, x
>> format long, x
>> format bank, x
-
7/29/2019 03_GENN004m_InputOutput
5/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
fprintf function
fprintf can be used to control how output isdisplayed
fprintf(format,data)
fprintf(format,data1,data2, )
format is a string specifying how to display the data
data, dataxare variables or values (either scalar orarray)
Example:
fprintf(pi:%d pi rounded: %1.0f\n,pi,pi)
fprintf function
The %d characters in the format string arecalled conversion characters
Each conversion character is a data place-holder in the format string
Each conversion character has a number of
optional formatting parameters These parameters go between the % and the
conversion character (d, f, i, etc.)
-
7/29/2019 03_GENN004m_InputOutput
6/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
fprintf function
%-8.2fNumber insertion
marker
Optional Flag
indicating positioning
inside the field
Field width &
decimal precision
Conversion
character
fprintf function%? Results
%d displayvalue in standard decimal notation
%e display value in exponential form
%f display value in fixed(floating)-point form
%g display as %e or %f, whichever is shorter
%s display a string of characters (not used with numbers)
To display a %, use %% in fprintfs format
For complete list of options, use >> help fprintf
Try
>> x=3.1253;
>> fprintf(x= %.0f, %10.5f, %-10.2f, %%\n,x,x,x);
>> fprintf(x= %.0f, %10.5f, %-10.1f, \n %%,x,x,x);
-
7/29/2019 03_GENN004m_InputOutput
7/7
GENN004: Introduction to Computers 09-Feb-
Input and Output
Test it out!
Write a script to take the radius of a circle as
an input from the user, calculate the area,
then print the area.
Sample Input/Output
This program calculates the area of a circle
Enter the radius: 5
The area of a circle with radius 5 equal to 78.54
>>
clc;
fprintf('This program calculates the area of a circle\n');
r=input('Enter the radius:');
a=pi*r^2;
fprintf('The area of a circle with radius %1.0f equal to %10.2f\n',r,a);