Download presentation
Presentation is loading. Please wait.
1
16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2014 Lecture 6: Output formatting If statements
2
ECE Application Programming: Lecture 6
Lecture outline Announcements/reminders Program 2 due 2/5 Today’s lecture Output formatting Intro to if statements 5/11/2018 ECE Application Programming: Lecture 6
3
Formatted output: field width
Specifying field width (min # characters) (assume n = 12): printf(“%10d”, n); (8 spaces, then number 12) To left justify value in field, use – flag before width printf(“%-10d”, n); 12________ (Number 12, then 8 spaces) To force the sign to show, use + flag before width printf(“%+10d”, n); (7 spaces, a plus sign, then number 12) To place 0s before the value, use 0 flag before width printf(“%010d”, n); printf(“%+010d”, n); printf(“%-010d”, n); 12________ To use a variable to specify field width, use * as the width printf(“%*d”, n, n); (Field width = 12 10 spaces, then the number 12) Field width never truncates printf(“%1d”, n); still prints 12 5/11/2018 ECE Application Programming: Lecture 6
4
Formatted output: precision
For examples, int n = 12; double x = 3.754 Specifying precision: # chars after decimal point for FP (%f, %lf) Rounds last digit printf(“%.6lf”, x); printf(“%.1lf”, x); 3.8 printf(“%.0lf”, x); 4 Minimum # chars for integer (%d, %i, %u, %x) Does not truncate; will pad with leading 0s printf(“%.1d”, n); 12 printf(“%.3d”, n); 012 Max # chars for string (%s) printf(“%.5s”, “one”); one printf(“%.5s”, “one two”); one t No effect for char (%c) As with field width, can use * to specify that field width is a variable 5/11/2018 ECE Application Programming: Lecture 6
5
Formatting and scanf()
Formatting specified above used only for printf() Have very different meaning for scanf() printf("%3d", x) print x w/field width of 3 scanf("%3d", &x) read 3 characters into x If you enter: 12 x = 12 If you enter: 1234 x = 123 Bottom line: you probably don’t want to “format” your input! 5/11/2018 ECE Application Programming: Lecture 6
6
ECE Application Programming: Lecture 6
Note The next three slides contain full specification for printf() flags, field width, and precision Read on if you want lots of details; skip these slides if the previous ones were sufficient 5/11/2018 ECE Application Programming: Lecture 6
7
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type flag meaning default - Left align the result within the given field width Right align + Prefix the output value with a sign (+ or -) if the output value is of a signed type. Sign appears only for negative signed values (-) If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and - appear, the 0 is ignored. No padding (actually space padding) blank Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears # When used with the o, x, or X format, the # flag prefixes any nonzero output with 0, 0x, or 0X, respectively. Ignored when used with c, d, i, u, or s. No prefix When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases. Decimal point appears only if digits follow it. When used with g or G format, forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Decimal point appears only if digits follow it. Trailing zeros are truncated. This slide adapted from information in MSDN Library 5/11/2018 ECE Application Programming: Lecture 6
8
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. Notes: If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). 5/11/2018 ECE Application Programming: Lecture 6 This slide adapted from information in MSDN Library
9
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type typ meaning default c The precision has no effect character is printed d,i, u,o, x,X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision Default precision is 1. e,E The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period f The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. (.) appears without a number following it, no decimal point is printed. g,G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated. s,S The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered. 5/11/2018 ECE Application Programming: Lecture 6 This slide adapted from information in MSDN Library
10
ECE Application Programming: Lecture 6
examples - printf() float a=67.49,b= ; printf("Price:█%10f█%10f\n",a,b); printf("%8.2f█%8.4f█%5.1f█%7.5f\n",a,a,b,b); printf("a=%5.5f,█b=%0.2f\n",a,b); Printed: print position Price:██ ███ columns ███67.49██ ██10.0█ to 35 a= ,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width. 5/11/2018 ECE Application Programming: Lecture 6
11
ECE Application Programming: Lecture 6
examples - printf() float a=67.49,b= ; int i=184,j=-51; double x= , y=-22.33; printf("%7.1lf%5d%8.2f\n",y,i,a); printf("%13.2f%4d%9.2lf\n",b,j,x); //changed Printed: print position columns to 35 Notes: d indicates decimal integer f indicates float lf (that’s lower case L and f) indicates double (for double output, f OK too; not OK for input) 5/11/2018 ECE Application Programming: Lecture 6
12
ECE Application Programming: Lecture 6
printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78); ..../ / / / <= ruler vv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78); ..../ / / / <= ruler vv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78); ..../ / / / <= ruler vv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78); ..../ / / / <= ruler vv 12ww34 xx +56yy 78zz 5/11/2018 ECE Application Programming: Lecture 6
13
ECE Application Programming: Lecture 6
printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78); ..../ / / / <= ruler v-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78); ..../ / / / <= ruler v 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78); ..../ / / / <= ruler v w x y z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78); ..../ / / / <= ruler v w x y z 5/11/2018 ECE Application Programming: Lecture 6
14
ECE Application Programming: Lecture 6
printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6); ..../ / / / <= ruler w x y z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6); ..../ / / / <= ruler w w y z printf("w%7.2fx%7.2f\n", , ); ..../ / / / <= ruler w x z 5/11/2018 ECE Application Programming: Lecture 6
15
Example: Formatted output
Assume int x = 123; float y = 4.56; double z = What does each of the following lines print? printf("%4d %5f %6lf\n", x, y, z); printf("%.4d %.4f %.4lf\n", x, y, z); printf("%08d %-7.1f %+-4.1lf !\n", The second and third specifiers have precision of 1 One is a float (%f), the other is a double (%lf) 5/11/2018 ECE Application Programming: Lecture 6
16
ECE Application Programming: Lecture 6
Example solution printf("%4d %5f %6lf\n", x, y, z); █123█ █ printf("%.4d %.4f %.4lf\n", 0123█4.5600█7.8999 printf("%08d %-7.1f %+-4.1lf!\n", █4.6█████+7.9█! 5/11/2018 ECE Application Programming: Lecture 6
17
Example: Formatted output
Write a short code sequence to do each of the following: Print three integers—x, y, and z Use field widths of 10, 20, and 30, respectively Put an extra space between each field Show the signs of all values and left justify them Print four doubles—d1, d2, d3, d4 Use field widths of 7 for all values Show 1, 2, 3, and 4 places after the decimal point, respectively Given three variables—int w, p; double var; Read values for w and p from the input Print var using field width w and precision p 5/11/2018 ECE Application Programming: Lecture 6
18
ECE Application Programming: Lecture 6
Example solution Print three integers—x, y, and z Use field widths of 10, 20, and 30, respectively Put an extra space between each field Show the signs of all values and left justify them printf(“%+-10d %+-20d %+-30d\n”, x, y, z); Print four doubles—d1, d2, d3, d4 Use field widths of 7 for all values Show 1, 2, 3, and 4 places after the decimal point, respectively printf(“%7.1lf %7.2lf %7.3lf %7.4lf\n”, d1, d2, d3, d4); 5/11/2018 ECE Application Programming: Lecture 6
19
Example solution (cont.)
Given three variables—int w, p; double var; Read values for w and p from the input Print var using field width w and precision p scanf(“%d %d”, &w, &p); printf(“%*.*lf\n”, w, p, var); 5/11/2018 ECE Application Programming: Lecture 6
20
ECE Application Programming: Lecture 6
Decisions Recall that flowcharts can include decisions Conditionally execute some path May want to: Only perform operation if condition is true: A = 0? TRUE FALSE A = 0? TRUE X = x + 1 FALSE 5/11/2018 ECE Application Programming: Lecture 6
21
ECE Application Programming: Lecture 6
Decisions (cont.) May want to: Perform one operation if condition is true, another if false: A = 0? TRUE X = x + 1 FALSE X = x - 1 5/11/2018 ECE Application Programming: Lecture 6
22
ECE Application Programming: Lecture 6
Decisions (cont.) May want to: Check multiple conditions, in order A = 0? TRUE X = x + 1 FALSE B=1? TRUE X = x - 1 FALSE X = 0 5/11/2018 ECE Application Programming: Lecture 6
23
ECE Application Programming: Lecture 6
if statements Frequently want to conditionally execute code Range checking Error checking Different decisions based on input, or result of operation Basic conditional execution: if statement Form: if (<expression>) <statement> [ else brackets show <statement> ] else is optional 5/11/2018 ECE Application Programming: Lecture 6
24
ECE Application Programming: Lecture 6
if statements (cont.) <expression> can be any valid expression Considered “false” if 0, “true” if nonzero Can use comparisons: Greater than/less than: > < e.g. if (a < b) Greater than or equal/less than or equal: >= <= e.g. if (x <= 20) Equal/not equal: == != e.g. if (var == 10) 5/11/2018 ECE Application Programming: Lecture 6
25
ECE Application Programming: Lecture 6
if statements (cont.) <expression> can be any valid expression Can combine multiple conditions using Logical AND: && Logical OR: || e.g. if ((x < 3) && (y > 5)) Always put each condition in parentheses Can test inverse of condition using logical NOT: ! e.g. if (!(x < 3)) equivalent to if (x >= 3) These operators: not bitwise operators! A & B is a bitwise operation A && B has only 2 possible results: 0 or “non-zero” 5/11/2018 ECE Application Programming: Lecture 6
26
ECE Application Programming: Lecture 6
if statements (cont.) <statement> can be one or more lines If just one line, no additional formatting needed if (x < 3) printf(“x = %d\n”, x); If multiple lines, statement is block enclosed by { } if (x < 3) { x = x + 3; } else part is optional—covers cases if condition is not true 5/11/2018 ECE Application Programming: Lecture 6
27
ECE Application Programming: Lecture 6
if if (a > b) big = a; else big = b; if (a+6*3-43) printf("wow is this not cool"); else printf("this is not cool"); 5/11/2018 ECE Application Programming: Lecture 6
28
ECE Application Programming: Lecture 6
if (common pitfalls) a single equals means ASSIGN. a double equal must be used to check for equality. x=12345; if (x=3) { printf("x is 3\n"); } else { printf("x is not 3\n"); } This code will ALWAYS print: x is 3 5/11/2018 ECE Application Programming: Lecture 6
29
ECE Application Programming: Lecture 6
if (example) void main(void) { float a,b,c,disc; : scanf("%f %f %f",&a,&b,&c); if (a==0) { // statements } else { disc = b*b-4*a*c; if ( disc < 0 ) { // statements } else { // statements } } } 5/11/2018 ECE Application Programming: Lecture 6
30
ECE Application Programming: Lecture 6
Final notes Next time Continue with if statements Switch statements Reminders: Program 2 due 2/5 5/11/2018 ECE Application Programming: Lecture 6
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.