200 likes | 296 Views
Computers in Engineering. PRINT USING STATEMENT. Overview. In a circuit three currents are represented by the variables cur1, cur2, cur3 Plan and write a program to input these values and calculate and display the average current. Plan. Outputs… Inputs…. Process… flowchart/plan/pseudocode.
E N D
Computers in Engineering PRINT USING STATEMENT
Overview In a circuit three currents are represented by the variables cur1, cur2, cur3 Plan and write a program to input these values and calculate and display the average current
Plan • Outputs… • Inputs…. • Process… • flowchart/plan/pseudocode
INPUT statements Write input statements to enter these variables INPUT “current 1”, cur1 INPUT “current 2”, cur2 INPUT “current 3”, cur3
In Memory User enters 9.16, 12.4, 0.9 cur1 9.16 cur2 12.4 cur3 0.9
Calculate total and average currents and print totCur = cur1 + cur2 + cur3 AvgCur = totCur / 3 PRINT , cur1 PRINT , cur2 PRINT , cur3 PRINT , “________” PRINT , TotCur PRINT “Average”, AvgCur
To add units to a variable PRINT “Current”; cur1; “amps” PRINT “prompt” ; variable list ; “prompt”
PRINT USING • PRINT USING statement can be used instead of PRINT to make decimal points line up • has general form - PRINT USING “format string”;expression list e.g PRINT USING “ ##.##”; cur1
Format string • String of characters • letters, numbers, punctuation • some have special meanings • if no special meaning qb will simply display it on the screen • if has special meaning qb uses it to determine the format for displaying one of the expressions in the list
Formatting numbers • # # #.# # • displays 3 digits preceding the decimal • fewer than 3 before qbasic leaves a space • more than 3 - will be displayed correctly but in a different format, will also precede it with a % character • displays 2 digits after the decimal • fewer than 2 - displays zeros for the missing digits • more than 2 - displays the rounded value
Format string Fmt$ = “current is ##.## at ###.# volts” PRINT USING Fmt$; cur; volts c.f. PRINT USING “format string”;expression list
Scientific notation - e.g. 2E-6 • ^^^^ • represent positions for E, minus or plus sign, two digit exponent • ^^^^^ • for double precision numbers • represent positions for E, minus or plus sign, three digit exponent • e.g. FMT$ = “current is ##.###^^^^ at ##.####^^^^ volts”
Output? Current is 23.5, volts are 117.6 FMT$ = “current is ##.###^^^^ at ##.####^^^^ volts” Current is 2.350E+01 at 1.1760E+02 volts • additional strings can be added • FMT$ = “current is & ##.###^^^^ at ##.####^^^^ volts” • curType$ = “direct” • PRINT USING Fmt$; curType$; cur; volts Current is direct 2.350E+01 at 1.176E+02 volts