Top Banner
E0001 Computers in Engineering Inputs to the Computer
27
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 Inputs to the Computer.

E0001 Computers in Engineering

Inputs to the Computer

Page 2: E0001 Computers in Engineering Inputs to the Computer.

What did we cover last week

Variables• why we use them• how we name them• how we define them• how they work

arithmetic operations print statement and zones

Page 3: E0001 Computers in Engineering Inputs to the Computer.

A Review from Last Week - Determine variables and outputs

x = 2

y = 3 * x

x = y + 5

PRINT x + 4

y = y + 1

END

x y

2

6

11

OUTPUT - 15

7

Page 4: E0001 Computers in Engineering Inputs to the Computer.

Determine types of variables

single precision integer double precision string variable

total = 6

total% = 6

total# = 6

total$ = “6”

Page 5: E0001 Computers in Engineering Inputs to the Computer.

Key points

arithmetic order types of variables and how to assign

specific types PRINT statement

Page 6: E0001 Computers in Engineering Inputs to the Computer.

Readings and Exercises

Schneider pp 59 - 66 exercies 3.3 p 67 - as required Schneider p 69 #’s 17, 20, 21, 25

Page 7: E0001 Computers in Engineering Inputs to the Computer.

Inputs to the computer

two main ways to get information into the computer• INPUT statement• READ and DATA statement

Page 8: E0001 Computers in Engineering Inputs to the Computer.

Input statement

INPUT reads input from the keyboard or a file. LINE INPUT reads a line of up to 255

characters from the keyboard or a file.

INPUT [;] ["prompt"{; | ,}] variablelist

LINE INPUT [;] ["prompt";] variable$

Page 9: E0001 Computers in Engineering Inputs to the Computer.

INPUT - simplest form

INPUT variable1, variable2....

EXAMPLES:

INPUT x

INPUT studnumber, names$ program stops and waits for user to enter

variables number and type of variables must match

Page 10: E0001 Computers in Engineering Inputs to the Computer.

INPUT with prompt

INPUT “prompt”; variablelist

INPUT “enter values for a and b”; a, b

enter values for a and b? 1,2

INPUT “enter values for a and b”, a, b

enter values for a and b1,2 variablelist separated by commas user input must match in type and number,

separated by commas

Page 11: E0001 Computers in Engineering Inputs to the Computer.

CLS

PRINT "This is a program to print envelopes."

PRINT "Press any key when you are ready to begin . . .."

Temp$ = INPUT$(1) 'Assign value to variable

LINE INPUT "Enter your name: "; YourName$

LINE INPUT "Enter your address: "; Address$

LINE INPUT "Enter your city, state, and ZIP code: "; CityEtc$

PRINT "Position envelope in printer and press Enter ."

char% = 1

Temp$ = INPUT$(char%)

LPRINT YourName$: LPRINT Address$

LPRINT CityEtc$

Page 12: E0001 Computers in Engineering Inputs to the Computer.

READ and DATA statements

READ statement assigns variables from corresponding DATA statement

READ variable1, variable2...

.....

DATA data1, data2...

variable1 = data1; variable2 = data2 etc

Page 13: E0001 Computers in Engineering Inputs to the Computer.

following READ statements continue from data list

cannot ‘run out’ of data - OUT OF DATA ERROR

variable types must match

READ studnumber, names$

DATA 12345, “smith”

Page 14: E0001 Computers in Engineering Inputs to the Computer.

Restore statement

Restores data pointer to start of data• next READ assigns from first DATA

statement, first variable

Page 15: E0001 Computers in Engineering Inputs to the Computer.

What will be displayed when the following program is executed?

READ a, b

LET x = a + b

READ a, b

LET y = a * b

RESTORE

READ a, b

LET z = a - b

PRINT x + y + z

DATA 2, 4, 3, 1, 5, 3

END

Page 16: E0001 Computers in Engineering Inputs to the Computer.

Problem

draw a flowchart which will• define variables for pi, radius and

circumference as double, integer and double respectively

• set radius = 3.2, pi = 22/7• calculate the circumference• print all variables

Page 17: E0001 Computers in Engineering Inputs to the Computer.

Flowchart

p rin t va riab les

ca lcu la te c ircu m feren ce

le t rad iu s = 3 .2 &p i = 2 2 /7

d im en s ion variab les

Page 18: E0001 Computers in Engineering Inputs to the Computer.

Dim statement

DIM x AS LONG

DIM y AS STRING

DIM z AS DOUBLE

DIM zz AS INTEGER

x&

y$

z#

zz%

Page 19: E0001 Computers in Engineering Inputs to the Computer.

Now write the program

DIM radius AS integer

DIM pi AS double

DIM circ AS double

radius = 3.2

pi = 22/7

circ = 2*pi*r

PRINT r,pi,circ

Is this program correct?p rin t va riab les

ca lcu la te c ircu m feren ce

le t rad iu s = 3 .2 &p i = 2 2 /7

d im en s ion va riab les

Page 20: E0001 Computers in Engineering Inputs to the Computer.

Now change the program to include INPUT statementsDIM radius AS integer

DIM pi AS double

DIM circ AS double

radius = 3.2

pi = 22/7

circ = 2*pi*radius

PRINT radius,pi,circ

DIM radius AS integer, pi AS double, circ AS double

pi = 22/7

INPUT radius

circ = 2*pi*radius

PRINT”radius = “;radius

PRINT”pi = “;pi

PRINT “circumference is“;circ

Page 21: E0001 Computers in Engineering Inputs to the Computer.

The End

Page 22: E0001 Computers in Engineering Inputs to the Computer.

TUTORIAL

1. Which is the correct hierarchy for arithmetic operators (from those evaluated first to those evaluated last).

2. What value will be assigned to x when the following statement is executed?

LET x = 4 * 3^2 / 5 * 4

Page 23: E0001 Computers in Engineering Inputs to the Computer.

3. The following statement is valid. (T/F)

INPUT "IS THIS OK"; q

4. The following statement is valid. (T/F)

DATA 25, "X + 16", 14

5. The following statement is valid. (T/F)

INPUT x, 5

Page 24: E0001 Computers in Engineering Inputs to the Computer.

6. What will be displayed when the following program is executed?

DATA 5, 3

READ c, d, e

DATA 2, 4, 1

READ a

DATA b, c, d

PRINT c

DATA 3, 4, 2, 1, 5

END

Page 25: E0001 Computers in Engineering Inputs to the Computer.

7. What will be the output of the following program?

CLS

READ a$, b$, c$

PRINT a$, b$, c$, b$

DATA The, best, "of the"

END

Page 26: E0001 Computers in Engineering Inputs to the Computer.

7. Evaluate the following numeric expressions.

(a) 2 - (1 - 1)

(b) 12 / 4 + 3 * 2

(c) 2 + 9 * 2

(d) 10 - 3 ^ 2

(e) How would you modify the expression in (d) without

changing any of the numbers so that it evaluates to 49?

Page 27: E0001 Computers in Engineering Inputs to the Computer.

Problem

Suppose a ball is thrown straight up in the air with an initial velocity of 50 m/s. How high will the ball be after t seconds given that:

ht = -16t2 + v0t + h0

where ht is height after t seconds; v0 is initial velocity and h0 is initial height