Presentation is loading. Please wait.

Presentation is loading. Please wait.

E0001 Computers in Engineering Built in Functions.

Similar presentations


Presentation on theme: "E0001 Computers in Engineering Built in Functions."— Presentation transcript:

1 E0001 Computers in Engineering Built in Functions

2 General Notes zError in Assignment A-E & M-R - TYPO Volume should be 152.13, 22.44, 5.77 zAssignment S-Z constant = 2.749*10 -8 zAssignment 2 due 9 April but …. submit until 12 April zAss1 and 2 handed back after holidays..

3 Readings and Exercises zSchneider Section 3.4 p 72-78 zSchneider Practice Problems 3.4 p 78; programming problems #’s 57, 60, 62, 66, 68

4 Rem statements zRem short for REMARK zused for adding comments to code zuseful for the programmer, not the user znon executable line zTwo ways of adding comments yREM y‘

5 Examples REM this line is a comment REM This program will calculate…. REM This program was written by…. INPUT r,b,h‘radius, base, height READ n$, no$‘name, number

6 Numeric Functions zcovers common numeric and trigonometric terms ysquare root - SQR(x) ySIN(angle), COS(angle), TAN(angle) xBEWARE - (angle) must be in RADIANS yLOG (x) zsee handout for more examples

7 Examples of Numeric Functions zSQR (x) - square root of x where x is yvariable - given a value by the program or the user yarithmetic operation - (6-4)/360*12 zABS - absolute value yABS(-3) = 3; ABS(7) = 7 yABS(SQR(CINT(x)))

8 String Functions zallows examining strings or parts; combining strings zallows string comparisons using, = (relational operators)

9 Examples of String Functions zLEN(s) - s is known as the argument yreturns the length of the argument i.e. the number of characters contained in the argument yblanks are counted as characters yLEN(“John and Mary”) = 13 yZ$ = “John and Mary”: LEN(Z$) = 13 yIF LEN(Z$) = 20 THEN PRINT “HELLO”

10 zLEFT$(x$, n) - x$ is any string; n is an integer from 0 to LEN(x$) yx$ = “John and Mary” yPRINT LEFT$(x$,4) - gives the output John zsee also MID$(x$,n,m) and RIGHT$(x$,n) zLCASE$(X$); UCASE$(X$); INSTR(n,X$,Y$) zVAL(X$) - converts a string whose contents represent a number to its numerical form. yignores leading blanks yconverts a string up to its first nonnumerical character into its numerical value.

11 Built-in Functions zprewritten “subroutine” that does one operation zreturns ONE value only zimplimented by arithametic statement yvariable = equation zdesigned to manipulate both numerical and string data zcover common calculations

12 Computers in Engineering PRINT USING STATEMENT

13 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

14 Plan zOutputs… zInputs…. zProcess… zflowchart/plan/pseudocode

15 INPUT statements Write input statements to enter these variables INPUT “current 1”, cur1 INPUT “current 2”, cur2 INPUT “current 3”, cur3

16 In Memory User enters 9.16, 12.4, 0.9 cur1 9.16 cur2 12.4 cur3 0.9

17 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

18 Qbasic Screen

19

20 To add units to a variable PRINT “Current”; cur1; “amps” PRINT “prompt” ; variable list ; “prompt”

21

22

23 PRINT USING zPRINT USING statement can be used instead of PRINT to make decimal points line up zhas general form - PRINT USING “format string”;expression list e.g PRINT USING “##.##”; cur1

24 Format string zString of characters yletters, numbers, punctuation zsome have special meanings yif no special meaning qb will simply display it on the screen yif has special meaning qb uses it to determine the format for displaying one of the expressions in the list

25

26 Formatting numbers z# # #.# # ydisplays 3 digits preceding the decimal xfewer than 3 before qbasic leaves a space xmore than 3 - will be displayed correctly but in a different format, will also precede it with a % character ydisplays 2 digits after the decimal xfewer than 2 - displays zeros for the missing digits xmore than 2 - displays the rounded value

27

28

29 Format string Fmt$ = “current is ##.## at ###.# volts” PRINT USING Fmt$; cur; volts c.f. PRINT USING “format string”;expression list

30 Scientific notation - e.g. 2E-6 z^^^^ yrepresent positions for E, minus or plus sign, two digit exponent z^^^^^ yfor double precision numbers yrepresent positions for E, minus or plus sign, three digit exponent ze.g. FMT$ = “current is ##.###^^^^ at ##.#### ^^^^ volts”

31 Output? Current is 23.5, volts are 117.6 FMT$ = “current is ##.###^^^^ at ##.####^^^^ volts” Current is 2.350E+01 at 1.1760E+02 volts zadditional strings can be added yFMT$ = “current is & ##.###^^^^ at ##.####^^^^ volts” ycurType$ = “direct” yPRINT USING Fmt$; curType$; cur; volts Current is direct 2.350E+01 at 1.176E+02 volts

32 Take a break Return for assignments

33 zWrite 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: zTotal inches = 63360 * miles + 36 * yards + 12 * feet + inches zTotal meters = total inches / 39.37 zKilometres = INT (meters/1000)

34 zKepler'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 a 3, 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 za ~ 6378 kilometers + altitude zIf the Period is expressed in minutes and the length of the major axis is expressed in kilometers, then zPeriod 2 = 2.749E-8*10 -4 *a 3 zWrite 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 SatelliteAltitude (km)Period (minutes) Earth imaging400 Low-altitude communications1500 Geosynchronous communication35789

35 zThe general law of perfect gases states that PV / T = n R = constant zwhere 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).

36 zWrite 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 50 0 and one decreased by 50 0. 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:

37  TemperaturePressureTemperature Volume  (Celsius)(atm)(Kelvin)(liter)  1502.280423152.13  20017.28747322.44  25074.3755235.77

38  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

39 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

40 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") 5 shake 123JUMP try this for yourself

41 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)

42 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

43 The End


Download ppt "E0001 Computers in Engineering Built in Functions."

Similar presentations


Ads by Google