Introduction to Computer Programming IT-104

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

VBA Modules, Functions, Variables, and Constants
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Adding Automated Functionality to Office Applications.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
Chapter 4: The Selection Process in Visual Basic.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter Six: Working With Arrays in Visual Basic.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
Tutorial 6 The Repetition Structure
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
1.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Arrays 1.
.NET MCQs MULTIPLE CHOICE QUESTION
Visual Basic Fundamental Concepts
Chapter 11 - JavaScript: Arrays
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
IS 350 Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Visual Basic 6 (VB6) Data Types, And Operators
The Selection Structure
Chapter 7 Arrays.
2. Understanding VB Variables
Variables and Arithmetic Operations
Visual Basic..
Exploring Microsoft Excel
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Chapter 3: Introduction to Problem Solving and Control Statements
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CS285 Introduction - Visual Basic
Chapter 7: Using Functions, Subs, and Modules
Introduction to Problem Solving and Control Statements
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Functions continued.
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

Introduction to Computer Programming IT-104 Objectives Introduction Loop Types Determinate Indeterminate Event While-Wend Do-While Do-Until For-Next Exit Introduction to Computer Programming IT-104 Unit Six – Arrays 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Objectives Understand the use of control, list, and table arrays in Visual Basic Use control arrays to work with groups of controls using a single name. Describe the difference between arrays and combo boxes, list boxes, and similar controls. Declare the maximum number of elements or rows and columns for an array. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Objectives Understand the errors that can occur from exceeding the declared upper limits on index values. Input data into an array from the keyboard or files using loops. Manipulate array data to find the sum and average of array values, the largest or smallest value in an array, or to find a particular value. Work with multiple arrays to match values in one array to those in the other. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Objectives Output the result of array processing to a control, the printer, or the Immediate window. Use Step operations to step though an array operation to find and correct an error. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Introduction Home Arrays are an important part of computer programming. An Array is a special type of variable structure implemented in virtually every computer language. Without the array and the loop collectively, programming a computer would be a daunting task indeed. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Using Control Arrays A control array is group of the same type control which is assigned a single name****. The individual controls are identified by the control array name and an index value starting with 0.**** The Case decision structure can be useful in working with a control array. To create a control array, just enter the same 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Using Control Arrays Home name for more than one control and replay “yes” to the query. Alternatively, simply copy an existing control and paste it to your existing form, and then answer ‘Yes’ to the ‘Confirm Control Array’ dialog box. The order in which the controls are entered determines the index values for the controls in the control array. 8/21/2019 Intro to Comp. Programming - IT 104

Check Boxes Vs. Option Buttons Home Check box is used to make multiple selections from a list Prefix is chk Option button is used to make one selection from a list. Often referred to as radio buttons Prefix is opt 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Frame Control Home Option buttons must be within a frame control which acts as a container . Frames have a prefix of fra. Recommendation is to add frame first and add the option button by drawing it in, not double clicking. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Frame Control All option buttons will have the same name but a different Index, such as optDemo(0), optDemo(1), optDemo(2). If the frame is moved, option buttons will move with it. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Option Button Array Home Double click any option button to open the code window and it will have event similar to this : Private Sub optDemo_Click(Index as Integer) The Index value is being passed to the event procedure, in other words, the index of the option button that was clicked is now known to the event procedure. The Index value is used is Select Case statements. 8/21/2019 Intro to Comp. Programming - IT 104

VB Code for Option Buttons Private Sub optDemo_Click(Index As Integer) Select Case Index Case 0 MsgBox "Button One selected" Case 1 MsgBox "Button Two selected" Case 2 MsgBox "Button Three selected" End Select End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Check Box Control Array All check boxes will have the same name but a different Value, such as chkSelect(0).Value, chkSelect(1).Value, chkSelect(2).value The Value will either be True or False. It is True if the check box is selected and False if not selected To determine if it is False use the Not operator Code on Next slide is used to check if a value is True or False 8/21/2019 Intro to Comp. Programming - IT 104

VB Code for Check Box Control Array Private Sub cmdSelect_Click() If chkSelect(0).Value And Not chkSelect(1).Value Then MsgBox "Box One selected" ElseIf Not chkSelect(0).Value And chkSelect(1).Value Then MsgBox "Box Two selected" ElseIf chkSelect(0).Value And chkSelect(1).Value Then MsgBox "Boxes One and Two selected" Else MsgBox "No boxes selected" End If End Sub 8/21/2019 Intro to Comp. Programming - IT 104

VB Code Select Prices Using Option Buttons (Pos demo) Private Sub OptTypes_Click(Index As Integer) Dim curItemPrice As Currency Select Case Index Case 0 ’Perishables curItemPrice = 3.99 Case 1 ’Staples curItemPrice = 1.99 Case 2 ’Hardware curItemPrice = 5.99 Case 3 ‘Cleaning supplies curItemPrice = 2.99 End Select txtItemPrice.text = Format(curItemPrice, "currency") End Sub 8/21/2019 Intro to Comp. Programming - IT 104

VB Code to Clear the OptTypes Option Buttons Home Private Sub cmdClear_Click() For Counter = 0 to 2 ’Clear option buttons OptTypes(Counter).Value = False Next End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Using List Arrays Arrays are lists or tables of data which have single name. They provide a way of working with long lists in memory versus working with short lists in a list or combo box. Arrays can only store one type of data. Individual array elements are identified by the array name and one or more subscripts or index values. For instance: curPrices(0), curPrices(1), curPrices(2) The index must be an integer constant, variable or 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Using List Arrays expression. To be used in Visual Basic, an array must be declared just like any other variable. Arrays can be fixed size or dynamic size (we use only fixed arrays.) Fixed size-array- specific amount of memory set aside for it. Dynamic array - no fixed amount of memory is reserved. 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Using List Arrays Form of array declaration : Dim ArrayName (max index value) as variable type Dim curPrices(9) as Currency Above statement allows for 10 values to be stored (0-9) The declaration statement defines the upper limit but by default the lower limit on an array index is zero but this can be changed to one or any other value. It is not possible to exceed the upper limit or go below the lower limit on the array index values . Will receive a “Subscript out of range” error 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Using List Arrays Home If a lower bound other than zero is desired, the programmer can implement his own numbering scheme as in the following: Dim sngRate(100 to 199) as Single This statement allocates 100 elements, but the lower bound is now 100 instead of 0. This gives the programmer the flexibility of matching existing numbering schemes that may exist within files he has been given to process. 8/21/2019 Intro to Comp. Programming - IT 104

Entering Event Driven Array Data Most of the time you want to input multiple values into an array. Arrays can be input with any of the three types of loops discussed earlier--event driven, for-next, or while/until loops. If input is from a keyboard and you don’t know the values to be input, an event driven loop is most appropriate. Similar to loops in Chapter 5 except values are entered directly in an array versus being added to a list box. Values don’t need to be summed 8/21/2019 Intro to Comp. Programming - IT 104

Entering Event Driven Array Data immediately. Index for the array corresponds to the value of the counting variable for each value that is input. With an event driven loop, each click of a button increments a counter and inputs the corresponding array element. 8/21/2019 Intro to Comp. Programming - IT 104

Input Using For-Next Loops With a For-Next loop, you must input the number of arrays elements. The array values must be input with an Inputbox with the For-Next counter variable matching the array index Private Sub Input_Click() Dim intCounter As Integer intNumPrices = CInt(InputBox("How many prices?")) For intCounter = 0 To intNumPrices - 1 curPrices(Counter) = CCur(InputBox("Next price:")) Next End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Processing Arrays Typical array operations include summing and averaging the array values and finding the largest or smallest value in the array. Working with arrays usually involves a For-Next loop. Summing Prices array elements involves using a statement of the form: Sum = Sum + Prices(Counter) Averaging Prices array elements involves dividing the Sum by the number of elements 8/21/2019 Intro to Comp. Programming - IT 104

Intro to Comp. Programming - IT 104 Processing Arrays Finding the largest or smallest value involves multiple comparisons of array elements. To find the maximum Price, you must compare each array value to the current highest price; if it is higher, it becomes the highest price. Do this using For-Next loop and If-then 8/21/2019 Intro to Comp. Programming - IT 104

VB Code to Compute the Sum and Average in Array Home Private Sub CmdSumAverage_Click() Dim curSum as Currency ’Sum variable Dim intCounter as Integer ’counter variable Dim curAverage as Currency ’Average variable curSum = 0 ’Set sum to 0 For intCounter = 0 to intNumPrices - 1 curSum = curSum + curPrices(intCounter) ’add prices Next If intNumPrices >0 Then ’check to see if >0 curAverage = curSum/intNumPrices ’true average prices Else ’if false MsgBox "No values to average!” ’display Msgbox Exit Sub End If txtSum.Text=Format(curSum, “currency”) txtAverage.Text = Format (curAverage, “currency” End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Pseudocode to Find Maximum Value Begin procedure to find largest value Set largest value to first item in list Repeat beginning with second item to last item If item in list > largest value then Largest value = item in list End decision End repeat Display largest value End procedure 8/21/2019 Intro to Comp. Programming - IT 104

VB Code to Find Maximum Value in List Private Sub cmdFindMax_Click() ’Declare variables and set largest value to first item in the array Dim intCounter As Integer, curLargest As Currency curLargest = curPrices(0) For intCounter = 1 To intNumPrices - 1 If curPrices(intCounter) >curLargest Then ’if item >largest # curLargest = curPrices(intCounter) ’ replace it if it is larger End If Next ’Display largest number as currency txtMaxPrice.Text = Format(curLargest, "currency") End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Code to Find Minimum Value in List Private Sub cmdFindMin_Click() ’Declare variables and set smallest value to first item in the array Dim intCounter As Integer, curSmallest As Currency curSmallest = Prices(0) For intCounter = 1 To intNumPrices - 1 If curPrices(intCounter) < curSmallest Then ’if item > smallest # curSmallest = Prices(Counter) ’ replace if it is smaller End If Next ’Display smallest number as currency txtMaxPrice.Text = Format(curSmallest, "currency") End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Find the Largest/Smallest String Value in a List Collating Sequence - order in which characters are displayed Digits come before alphabetic characters. Upper case A is smaller than uppercase B because it comes first in alphabetical ordering Lower case letters come after upper case letters Chr() function converts Integer values of the For-Next variable into the corresponding characters To reverse operation use the ASC() function with the character as the argument 8/21/2019 Intro to Comp. Programming - IT 104

VB Code for Displaying the Collating Sequence Private Sub Form_Load() Dim intCounter As Integer For intCounter = 0 To 255 Debug.Print Chr(intCounter); Next End Sub 8/21/2019 Intro to Comp. Programming - IT 104

Finding Items and Working with Multiple Lists The goal of the project is to find a specified part ID and display the part identifier and the price of the part To input both the part identifiers and a price a second array, PartID needs to be declared as a string in the Form General declaration section Dim strPartId(25) as String The Input Statement in the Form-Load event must be modified Input #5, strPartID(intNumPrices), curPrices(intNumPrices) For a given part, the index values or the strPartId, and the curPrices arrays are the same. The index acts as a link between them. 8/21/2019 Intro to Comp. Programming - IT 104