chap07 - QBASIC Chapter 7 QBASIC Chapter 7 For Loops,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: QBASIC Chapter 7 QBASIC Chapter 7 For Loops, Arrays, Sorting, and For Table Processing Table Created by Richard D. Taylor Illustrated by Saundra J. Taylor Introduction Introduction ­ New Method for looping: FOR NEXT ­ New Method for assigning variables: ARRAYS ­ New Procedure: SORTING ­ New Command: TABLE The FOR and NEXT Statements The FOR and NEXT Statements To execute a section of a program repeatedly with To automatic changes in the value of a variable between repetitions between DO­WHILE DO­WHILE Vs FOR­NEXT Do WHILE Count <= 100 Sum = Sum + Count Count = Count + 1 Loop FOR Count = 1 to 100 FOR STEP 1 STEP Sum = Sum + Count NEXT Count The FOR Statement The FOR Statement Form: Form: FOR k = initial value TO limit value STEP increases value STEP FOR k = initial value TO limit value IF the keyword STEP is not used, then IF the increment value defaults to 1 The FOR Statement The FOR Statement Purpose: Causes the statements between the Causes FOR and NEXT statements to be executed repeatedly until the value of K exceeds the limit value When k exceeds the limit value, control transfers to the line immediately following the corresponding NEXT statement corresponding The FOR Statement The FOR Statement Examples ­FOR Item = 1 to 20 ­FOR Amount = -5 to 15 STEP 2 ­FOR Count = 10 to -5 STEP -3 ­FOR Tax = 0 to 10 STEP 0.1 ­FOR Total = Start TO Finish STEP Increment ­FOR S= A + 5 to C / D STEP F * B ­FOR J = 20 to 1 The NEXT Statement The NEXT Statement Form: Form: NEXT k NEXT where k is the same variable as the loop variable in the corresponding FOR statement FOR Purpose: Identifies the end of a FOR loop Examples: NEXT Amount NEXT Item The FOR and NEXT Statements The FOR and NEXT Statements Definitions Loop variable For Number = 1 TO 100 STEP 2 Range Increment Value .. .. NEXT Number Initial Value Limit Value The Exit Statement The Exit Statement Form: Form: EXIT Statement EXIT where statement is FOR, DO, DEF, FUNCTION, SUB FUNCTION, Purpose: Exit a For or Do loop function, or Exit subprogram prematurely subprogram Examples: EXIT FOR EXIT DO ARRAYS Vs SIMPLE VARIABLES ARRAYS Vs SIMPLE VARIABLES FOR Number = 1 to 12 READ Month(Number) NEXT Number READ Jan, Feb, Mar Result: Result: Month(1) Month(1) Month(2) Month(3) 46897 43768 39987 Jan Feb Mar 46897 43768 39987 Declaring Arrays Declaring Arrays Form: DIM Array name(size)….,array DIM name(size) name(size) NOTE: NOTE: Where array name represents a numeric or string variable name, size in the first general form defines the upper-bound value of each subscripts of the array value The lower bound of each dimension is 0, unless you include an OPTION BASE statement statement Declaring Arrays Declaring Arrays DIM DIM Array name(1b1 TO ub1),…., array Array TO ),…., name(ib1 TO ub1,….,1bn TO ubn) name(ib NOTE: Size may be a positive integer or Size numeric variable for one dimensional arrays arrays Size may be a series of positive integers Size or a series of numeric variables separor ated by commas for multi-dimensional ated arrays Declaring Arrays Declaring Arrays The DIM statement can declare the type of The array (that is, integer, single precision, etc.) and the availability of the array in subprograms subprograms Declaring Arrays Declaring Arrays DIM Balance(4) DIM Code$ (1 TO 8) DIM Pick (-10 TO -1), Job$(200), Time (1 TO 20, DIM 1 TO 20) TO DIM Cost (-5 TO 45), K$(X TO 50, Y, Z), DIM Amount (2, 1 TO 75, 7) Amount The OPTION BASE Statement The OPTION BASE Statement Form: Form: OPTION BASE n where n is either 0 or 1 where Purpose: to assign a lower bound of 0 or 1 to to all arrays in a program to Examples: OPTION BASE 0 OPTION BASE 1 The OPTION BASE Statement The OPTION BASE Statement NOTE: If the OPTION BASE statement is not If used, the lower-bound value is set so zero for all arrays declared with only an upper-bound value upper-bound Multidimensional Arrays Multidimensional Arrays Manipulating Two-Dimensional Arrays DIM Cost(1 TO 2, 1 TO 5) Arrays with More Than Two Dimensions DIM Time(1 TO 5, 1 TO 6, DIM 1 TO 2, 1 TO 3) TO Sorting Sorting Bubble Sort Shell Sort Sorting Sequence Sorting Sequence Ascending Sequence Ascending ­lowest to highest in value ­Example: 1 4 6 8 9 Descending Sequence ­highest to lowest in value ­Example: 9 8 6 4 1 Bubble Sort Bubble Sort A straightforward method of sorting data items that straightforward have been placed in an array have The SWAP Statement The SWAP Statement Form: Form: SWAP variable1, variable2 where SWAP variable1 and variable2 are of the same variable type type Purpose: Exchanges the values of two variables Purpose: or two elements of an array or The SWAP Statement The SWAP Statement Examples: SWAP Old.Balance, New.Balance SWAP Array (Size), Array (Size + 1) SWAP Code1$(Row, Col), Code2$(Row, Col) Bubble Sort Bubble Sort Bubble Sort: Limit = Number Switch$ = “ON” Do WHILE Switch$ = “ON” Switch$ = “OFF” For I = 1 to (Limit - 1) If B(1) > B(1 + !) THEN SWAP B(1), B(1 + 1) Switch$ = “ON” Bubble Sort Bubble Sort Last = 1 END IF NEXT I Limit = Last LOOP RETURN The Shell Sort The Shell Sort The bubble sort algorithm works well The for a small number of data items, but it can take too much processing time for a large number of data items large Table Processing Table Processing Table Organization ­ Positional Organized Tables - table functions can be accessed by their position in the table can ­ Argument-Organized Tables - table functions are accessed by the value that corresponds to the desired table function desired Table Processing Table Processing ­ Positional Organized Tables ­a program can be written that displays the name of the program month in response to a month number, 1 through 12 month ­To write a program that uses table processing To techniques: techniques: ­ ­ ­ Define the table by declaring an array Load the table functions into the array Write statements to access the table entries Table Processing Table Processing ­ Argument-Organized Tables ­Search Argument - tables are characterized by entries Search made up of multiple functions ­Table Argument - a table entry to retrieve the Table corresponding table function corresponding Table Processing Table Processing ­ Argument-Organized Tables ­Table Search or Table Lookup - an argument-organized Table table must be searched until the search argument agrees with one of the table arguments with ­ ­ Serial Search - begins by comparing the search argument to Serial the first table argument - if the two agree, search is over, if they don’t agree, then the search argument is compared to the second table argument until the end of a match is found second Binary Search - begins the search in the middle of the table Binary and determines whether the table argument that agrees with the search argument is in the upper half or the lower half of the table table Table Processing Table Processing ­ Serial Search ­Linear Search - a procedure that all of us use in everyday Linear life life ­The search argument is tested against each of the table The arguments, beginning with the first, until a bit is made at that point, the corresponding part description and part cost are selected from the table cost Table Processing Table Processing ­ Ordering the Table Arguments for a Serial Search ­Load the table according to the frequency analysis, Load therefore the more frequently requested items are at the beginning beginning ­ Binary Search ­Useful for short tables, not long tables ­Search begins the search in the middle to the table and Search then continues to divide the table in half until the argument is found argument Table Processing Table Processing ­ Combining Table-Access Methods ­A serial search is first used, then a binary search is used serial for larger tables, then expanded further to include several tables and an algorithm that searches one table several ­A directory table is created to determine the table that directory contains the entry contains Table Processing Table Processing ­ Some Formulas for Searching ­Average number of searches may be found from the Average following formula: following ­ S = N / 2 where N = total number of elements and S = average where number of searches number ­Average number of searches when a binary search is used: ­ ­ ­ ­ N = 2S log, n = s S = log,n where n = total number of elements, s = average number of searches Summary Slide Summary Slide ­ Introduction ­ The FOR and NEXT Statements ­ DO-WHILE DO-WHILE Vs FOR-NEXT FOR-NEXT ­ The FOR Statement ­ The NEXT Statement ­ The FOR and NEXT Statements Summary Slide (cont.) Summary Slide (cont.) ­ The Exit Statement ­ ARRAYS Vs SIMPLE VARIABLES ­ Declaring Arrays ­ The OPTION BASE Statement ­ Multidimensional Arrays ­ Sorting Summary Slide (cont.) Summary Slide (cont.) ­ Sorting Sequence ­ Bubble Sort ­ The SWAP Statement ­ Bubble Sort ­ The Shell Sort ­ Table Processing ...
View Full Document

This note was uploaded on 06/06/2011 for the course MIS 105 taught by Professor Grigoletti during the Spring '11 term at Moraine Valley Community College.

Ask a homework question - tutors are online