E0001 Computers in Engineering
Built in Functions
General Notes
Error in Assignment A-E & M-R - TYPO Volume should be 152.13, 22.44, 5.77
Assignment S-Z constant = 2.749*10-8
Assignment 2 due 9 April but …. submit until 12 April
Ass1 and 2 handed back after holidays..
Readings and Exercises
Schneider Section 3.4 p 72-78Schneider Practice Problems 3.4 p
78; programming problems #’s 57, 60, 62, 66, 68
Rem statements
Rem short for REMARKused for adding comments to codeuseful for the programmer, not the
usernon executable lineTwo ways of adding comments
REM ‘
Examples
REM this line is a commentREM This program will calculate….REM This program was written by….
INPUT r,b,h ‘radius, base, height
READ n$, no$ ‘name, number
Numeric Functions
covers common numeric and trigonometric terms square root - SQR(x) SIN(angle), COS(angle), TAN(angle)
BEWARE - (angle) must be in RADIANS
LOG (x)see handout for more examples
Examples of Numeric Functions
SQR (x) - square root of x where x is variable - given a value by the program
or the user arithmetic operation - (6-4)/360*12
ABS - absolute value ABS(-3) = 3; ABS(7) = 7 ABS(SQR(CINT(x)))
String Functions
allows examining strings or parts; combining strings
allows string comparisons using <, >, = (relational operators)
Examples of String Functions
LEN(s) - s is known as the argument returns the length of the argument i.e.
the number of characters contained in the argument
blanks are counted as characters LEN(“John and Mary”) = 13 Z$ = “John and Mary”: LEN(Z$) = 13 IF LEN(Z$) = 20 THEN PRINT “HELLO”
LEFT$(x$, n) - x$ is any string; n is an integer from 0 to LEN(x$) x$ = “John and Mary” PRINT LEFT$(x$,4) - gives the output John
see also MID$(x$,n,m) and RIGHT$(x$,n)
LCASE$(X$); UCASE$(X$); INSTR(n,X$,Y$)
VAL(X$) - converts a string whose contents represent a number to its numerical form. ignores leading blanks converts a string up to its first nonnumerical
character into its numerical value.
Built-in Functions
prewritten “subroutine” that does one operation
returns ONE value onlyimplimented by arithametic statement
variable = equationdesigned to manipulate both numerical
and string datacover common calculations
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”, cur1INPUT “current 2”, cur2INPUT “current 3”, cur3
In Memory
User enters 9.16, 12.4, 0.9cur1 9.16cur2 12.4cur3 0.9
Calculate total and average currents and print
totCur = cur1 + cur2 + cur3AvgCur = totCur / 3PRINT , cur1PRINT , cur2PRINT , cur3PRINT , “________”PRINT , TotCurPRINT “Average”, AvgCur
Qbasic Screen
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
liste.gPRINT 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
Character Meaning
# A digit position in the format for anumerical output
. (period) The decimal point position in theformat for a numerical output
^^^^ or ^^^^^ Specifies scientific format for thenumerical output
\spaces\ Characters format the beginningof the value of a string variable:2 + the number of spacecharacters will be printed
& The value of a string variable
Formatting numbers
# # #.# # displays 3 digits preceding the decimal
fewer than 3 before qbasic leaves a spacemore than 3 - will be displayed correctly but
in a different format, will also precede it with a % character
displays 2 digits after the decimalfewer than 2 - displays zeros for the missing
digitsmore than 2 - displays the rounded value
Format string
Fmt$ = “current is ##.## at ###.# volts”
PRINT USING Fmt$; cur; voltsc.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 exponente.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 voltsadditional 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
Take a break
Return for assignments
Write a program to convert a US customary system length in miles, yards, feet and inches to a Metric System length in kilometres, meters and centimetres. After the number of miles, yards, feet and inches are requested as input, the length should be converted entirely to inches and then divided by 39.37 to obtain the value in meters. The INT function should be used to break the total number of meters into a whole number of kilometres and meters. The number of centimetres should be displayed to one decimal place. Some of the needed formulas are:
Total inches = 63360 * miles + 36 * yards + 12 * feet + inches
Total meters = total inches / 39.37 Kilometres = INT (meters/1000)
Kepler's Third Law states that for objects with an elliptical orbit (such as satellites orbiting Earth or planets orbiting the sun), the square of the Period is proportional to a3, where a is half the length of the major axis of the ellipse. For nearly circular satellite orbit above Earth, a can be approximated by the radius of the earth plus the altitude of the satellite. Thus
a ~ 6378 kilometers + altitude
If the Period is expressed in minutes and the length of the major axis is expressed in kilometers, then
Period 2 = 2.749E-8*10-4*a3
Write a program to produce the table shown below. The information in the first two columns should be stored in DATA statements and the period should be computed and displayed to an appropriate number of decimal places.
Type of Satellite Altitude (km) Period (minutes)
Earth imaging 400
Low-altitude communications 1500
Geosynchronous communication 35789
The general law of perfect gases states that PV / T = n R = constant
where • P is the gas pressure (in atmospheres),
• V is the volume (in litres),
• T is the temperature (in degrees Kelvin),
• n is the number of moles,
• R is the constant of perfect gases (0.82 Liter-atm/Kelvin-mole).
Write a program to produce the table for the case n=1 thus giving V = RT/P. The user with an INPUT statement should enter a temperature (in Celsius) and pressure. Two new temperatures (in Celsius) should be calculated from the original, one increased by 500 and one decreased by 500. A corresponding pressure for each new temperature should be entered by the use of an INPUT statement. The temperature in Kelvin and the Volume should be calculated for the three temperatures and displayed. A sample output is shown below:
TemperaturePressure TemperatureVolume
(Celsius) (atm) (Kelvin)(liter)
150 2.280 423 152.13200 17.287 473 22.44250 74.375 523 5.77
The period P of a pendulum of length L and maximum displacement angle is given by the formula
P =
write a program that requests as input the length and maximum angle of displacement, and displays the period of the pendulum
Problem
L/ g(11
4sin (
2))2
Tutorial
1. What will be the contents of x after the following statement is executed?
LET x = SQR((9 + 7) / ( 4 * 2) + 2)
2. Suppose num = 123.4567. What is the output?
LET num = INT(100 * num + .5) / 100
PRINT num
3. What output is generated by the following statements (show the output EXACTLY as it would appear on the screen)? PRINT SQR (3^2 + 4^2) PRINT MID$ ("Milkshake", 5, 5) PRINT UCASE$ ("123jump")
5shake123JUMP
try this for yourself
4. The following statements are valid. (T/F)
LET H$ = "Hello"
LET a$ = INSTR(H$, 3)
5. Given the data in the variable y$ shown below, which of the following statements will assign the value ALL to x$?
LET y$ = "WHEN ALL ELSE FAILS, READ THE DIRECTIONS"
(a) LET x$ = MID$(y$, 6, 3)
(b) LET x$ = INSTR(6, y$, "ALL")
(c) LET x$ = LEFT$(y$, 3)
(d) LET x$ = MIDDLE$(y$, 6, 3)
(e) LET x$ = RIGHT$(y$, 8)
6. What will be displayed when the following program is executed?
LET a$ = "THE WHOLE"
LET b$ = "PART"
LET c$ = MID$(a$, SQR(4), LEN(b$))
PRINT c$
END
The End