1 / 42

Lecture 6 & 7

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.

maili
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 & 7 Variables arithmetic statements print statements inputs to the computer

  2. Notices • Assignment 1 due Friday 4pm • 4th floor engineering building • no display folders please • CMA’s submit anytime • Thursday Show holiday - PALs makeup

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

  4. This lecture • what is a variable • types of variables • arithmetic operations • PRINT statement • INPUT and READ and DATA statements

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

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

  7. Assigning variables [LET] variablename = variable • e.g. LET x = 2 x = 2 LET sum = 24 sum = 24 LET counter = counter +1 total = total + x } }

  8. x = 2 y = 3 * x x = y + 5 PRINT x + 4 y = y + 1 END x y 2 6 11 OUTPUT - 15 7 Determine variables and outputs

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

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

  11. single precision integer double precision string variable total = 6 total% = 6 total# = 6 total$ = “6” Determine types of variables

  12. DIM x AS LONG DIM y AS STRING DIM z AS DOUBLE DIM zz AS INTEGER x& y$ z# zz% Dim statement

  13. Output of Program? x%= 3.1234 PRINT x% what type of variable is x%? x = 3.123456789 PRINT x

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

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

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

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

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

  19. Flowchart

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

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

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

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

  24. 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%])

  25. 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$

  26. READ and DATA statements • READ statement assigns variables from corresponding DATA statement READ variable1, variable2... ..... DATA data1, data2... variable1 = data1; variable2 = data2 etc

  27. 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”

  28. Restore statement • Restores data pointer to start of data • next READ assigns from first DATA statement, first variable

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

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

  31. 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?

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

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

  34. The End Tutorial to follow...

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

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

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

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

  39. 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?

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

  41. The End!!

More Related