Lecture 6 7
This presentation is the property of its rightful owner.
Sponsored Links
1 / 42

Lecture 6 & 7 PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 6 & 7. Variables arithmetic statements print statements inputs to the computer. Notices. Assignment 1 due Friday 4pm 4th floor engineering building no display folders please CMA’s submit anytime Thursday Show holiday - PALs makeup. Readings & activities.

Download Presentation

Lecture 6 & 7

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 6 7

Lecture 6 & 7

Variables

arithmetic statements

print statements

inputs to the computer


Notices

Notices

  • Assignment 1 due Friday 4pm

    • 4th floor engineering building

    • no display folders please

  • CMA’s submit anytime

  • Thursday Show holiday - PALs makeup


Readings activities

Readings & activities

  • Study Book; Module 8 & 9 note error in summary of readings handout

  • Qbasic with an Intro to Visual Basic by Schneider; p40 to 47; p55 to 57

  • Practice Problems and Exercises 3.1 & 3.2 as necessary

  • Schneider pp 59 - 66

  • exercies 3.3 p 67 - as required

  • Schneider p 69 #’s 17, 20, 21, 25


This lecture

This lecture

  • what is a variable

  • types of variables

  • arithmetic operations

  • PRINT statement

  • INPUT and READ and DATA statements


Variables revision

Variables - revision

  • Quantities referred to by symbolic names

  • make general solutions

  • Variable name: is the name of a storage location in primary memory where Qbasic stores the value of the variable

  • value can change during program execution


Variable names

Variable names

  • may only contain letters, digits and full stop

  • may not contain a blank space

  • must start with a letter and may be up to 40 characters

  • may NOT be a reserved word e.g let, print

  • generally given a value of 0 initially but...


Assigning variables

Assigning variables

[LET] variablename = variable

  • e.g.

    LET x = 2

    x = 2

    LET sum = 24

    sum = 24

    LET counter = counter +1

    total = total + x

}

}


Determine variables and outputs

x = 2

y = 3 * x

x = y + 5

PRINT x + 4

y = y + 1

END

xy

2

6

11

OUTPUT - 15

7

Determine variables and outputs


Types of variables

single-precision

default type

double-precision

scientific notation for printing very large or very small numbers

integer

long-integer

string

-3.37*1038to 3.37* 1038

-1.67 *10308to 1.67* 10308

1 * 10-5 = 1E-5

.000013596426 = 1.359643E-5

-32 768 to 32 767

-2 147 483 648 to 2 147 483 648

any alphanumeric

Types of Variables


Assigning variable types

TYPE statement e.g.

DIM x AS integer

DIM variablename AS type

Types

single

double

integer

long

string

Suffix on variable name

! (Single)

# (Double)

% (Integer )

& (Long)

$ (String)

Assigning variable types


Determine types of variables

single precision

integer

double precision

string variable

total = 6

total% = 6

total# = 6

total$ = “6”

Determine types of variables


Dim statement

DIM x AS LONG

DIM y AS STRING

DIM z AS DOUBLE

DIM zz AS INTEGER

x&

y$

z#

zz%

Dim statement


Output of program

Output of Program?

x%= 3.1234

PRINT x%what type of variable is x%?

x = 3.123456789

PRINT x


Arithmetic operations

Arithmetic Operations

  • five arithmetic operations

    • additions (+); subtraction (-); division(/); multiplication (*); exponentiation (^)

  • executed in a specific order

    • ( ) inner to outer; left to right

    • ^ left to right

    • * and /; \ (integer division); MOD (modulus); left to right

    • + and - ; left to right


Print statement

PRINT statement

  • basic form: PRINT operation (arithmetic)

    • PRINT 3 * 2

  • PRINT variablelist

    • PRINT a, b, c orPRINT x; y; z

    • PRINT answer$

  • PRINT “prompt”, variablelist

    • PRINT “the answer is”, a


Screen placement formatting

Screen Placement & Formatting

  • PRINT zones

    • line has 80 characters

    • subdivided into 5 zones

    • zones 1 - 4 have 14 characters

    • zone 5 has 24 characters

    • zones start at columns 1, 15, 29, 43 & 57


Print zones contd

Print zones contd

  • commas as separators in PRINT statement

    • next item printed in next zone

  • statement ends in comma

    • next item printed in next print zone

  • semicolon as separators in PRINT statement

    • separated by 2 spaces

  • statement ends in semicolon

    • next item separated by 2 spaces


Problem

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


Flowchart

Flowchart


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?

Now write the program


Inputs to the computer

Inputs to the computer

  • two main ways to get information into the computer

    • INPUT statement

    • READ and DATA statement


Input simplest form

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


Input with prompt

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


Input statement

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$

  • INPUT$(n[,[#]filenumber%])


Lecture 6 7

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$


Read and data statements

READ and DATA statements

  • READ statement assigns variables from corresponding DATA statement

    READ variable1, variable2...

    .....

    DATA data1, data2...

    variable1 = data1; variable2 = data2 etc


Lecture 6 7

  • 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”


Restore statement

Restore statement

  • Restores data pointer to start of data

    • next READ assigns from first DATA statement, first variable


Lecture 6 7

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


Problem1

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


Program general solution

DIM 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

Program - general solution?


Now change the program to include input statements

DIM 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

Now change the program to include INPUT statements


Your turn

Your turn

  • Draw a flowchart that take two times in hours, minutes and seconds and will calculate the total time in hours minutes and seconds e.g 2 hr, 15 min & 12 sec + 1 hr 10 min and 5 sec = 3 hrs 25 min and 17 sec


The end

The End

Tutorial to follow...


Tutorial

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


Lecture 6 7

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


Lecture 6 7

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


Lecture 6 7

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


Lecture 6 7

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?


Problem2

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


The end1

The End!!


  • Login