Top Banner
E0001 Computers in Engineering Built in Functions
43
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: E0001 Computers in Engineering Built in Functions.

E0001 Computers in Engineering

Built in Functions

Page 2: 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..

Page 3: E0001 Computers in Engineering Built in Functions.

Readings and Exercises

Schneider Section 3.4 p 72-78Schneider Practice Problems 3.4 p

78; programming problems #’s 57, 60, 62, 66, 68

Page 4: E0001 Computers in Engineering Built in Functions.

Rem statements

Rem short for REMARKused for adding comments to codeuseful for the programmer, not the

usernon executable lineTwo ways of adding comments

REM ‘

Page 5: E0001 Computers in Engineering Built in Functions.

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

Page 6: E0001 Computers in Engineering Built in Functions.

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

Page 7: E0001 Computers in Engineering Built in Functions.

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

Page 8: E0001 Computers in Engineering Built in Functions.

String Functions

allows examining strings or parts; combining strings

allows string comparisons using <, >, = (relational operators)

Page 9: E0001 Computers in Engineering Built in Functions.

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”

Page 10: E0001 Computers in Engineering Built in Functions.

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.

Page 11: E0001 Computers in Engineering Built in Functions.

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

Page 12: E0001 Computers in Engineering Built in Functions.

Computers in Engineering

PRINT USING STATEMENT

Page 13: E0001 Computers in Engineering Built in Functions.

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

Page 14: E0001 Computers in Engineering Built in Functions.

Plan

Outputs…Inputs….Process…flowchart/plan/pseudocode

Page 15: E0001 Computers in Engineering Built in Functions.

INPUT statements

Write input statements to enter these variables

INPUT “current 1”, cur1INPUT “current 2”, cur2INPUT “current 3”, cur3

Page 16: E0001 Computers in Engineering Built in Functions.

In Memory

User enters 9.16, 12.4, 0.9cur1 9.16cur2 12.4cur3 0.9

Page 17: E0001 Computers in Engineering Built in Functions.

Calculate total and average currents and print

totCur = cur1 + cur2 + cur3AvgCur = totCur / 3PRINT , cur1PRINT , cur2PRINT , cur3PRINT , “________”PRINT , TotCurPRINT “Average”, AvgCur

Page 18: E0001 Computers in Engineering Built in Functions.

Qbasic Screen

Page 19: E0001 Computers in Engineering Built in Functions.
Page 20: E0001 Computers in Engineering Built in Functions.

To add units to a variable

PRINT “Current”; cur1; “amps”PRINT “prompt” ; variable list ;

“prompt”

Page 21: E0001 Computers in Engineering Built in Functions.
Page 22: E0001 Computers in Engineering Built in Functions.
Page 23: E0001 Computers in Engineering Built in Functions.

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

Page 24: E0001 Computers in Engineering Built in Functions.

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

Page 25: E0001 Computers in Engineering Built in Functions.

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

Page 26: E0001 Computers in Engineering Built in Functions.

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

Page 27: E0001 Computers in Engineering Built in Functions.
Page 28: E0001 Computers in Engineering Built in Functions.
Page 29: E0001 Computers in Engineering Built in Functions.

Format string

Fmt$ = “current is ##.## at ###.# volts”

PRINT USING Fmt$; cur; voltsc.f.PRINT USING “format

string”;expression list

Page 30: E0001 Computers in Engineering Built in Functions.

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”

Page 31: E0001 Computers in Engineering Built in Functions.

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

Page 32: E0001 Computers in Engineering Built in Functions.

Take a break

Return for assignments

Page 33: E0001 Computers in Engineering Built in Functions.

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)

Page 34: E0001 Computers in Engineering Built in Functions.

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

Page 35: E0001 Computers in Engineering Built in Functions.

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

Page 36: E0001 Computers in Engineering Built in Functions.

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:

Page 37: E0001 Computers in Engineering Built in Functions.

TemperaturePressure TemperatureVolume

(Celsius) (atm) (Kelvin)(liter)

150 2.280 423 152.13200 17.287 473 22.44250 74.375 523 5.77

Page 38: E0001 Computers in Engineering Built in Functions.

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

Page 39: E0001 Computers in Engineering Built in Functions.

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

Page 40: E0001 Computers in Engineering Built in Functions.

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

Page 41: E0001 Computers in Engineering Built in Functions.

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)

Page 42: E0001 Computers in Engineering Built in Functions.

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

Page 43: E0001 Computers in Engineering Built in Functions.

The End