Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s name obeys the same rules as when declaring a variable:

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Introduction to arrays
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 C++ Syntax and Semantics The Development Process.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
VARIABLES: Your Own Piece of the ram. Why Use Variables? To save a piece of data in memory To save a piece of data in memory To store results of a calculation.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Private Sub bntRedDemo_Click(…. Dim intTheOnes As Integer For intTheOnes = 0 To 9 bntRedDemo.Text = intTheOnes.ToString bntRedDemo.Refresh() Sleep(100)
BACS 287 Programming Fundamentals 4. BACS 287 Programming Fundamentals This lecture introduces the following iteration control structure topics: – Do.
Sorting an Array. Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Computer Memory 21 oneGrade integer We can declare, assign and manipulate individual variables with ease…
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
ISBN Chapter 7 Expressions and Assignment Statements.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Calculator Program Explained by Arafa Hamed. First Designing The Interface Ask yourself how many places are there that will be used to input numbers?
DATA TYPES, VARIABLES AND CONSTANTS. LEARNING OBJECTIVES  Be able to identify and explain the difference between data and information  Be able to identify,
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Data Structures Arrays and Lists Part 2 More List Operations.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Arrays 1.
Welcome to Computer Programming II! Computer Programming II Summer 2015.
Fundamentals 2.
Visual Basic Variables
Data Types, Arithmetic Operations
Data Types, Identifiers, and Expressions
Arrays ICS 111: Introduction to Computer Science I
Data Types, Identifiers, and Expressions
Computers & Programming Languages
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Visual Basic 2010 How to Program
Numbers.
Chapter (3) - Looping Questions.
Fundamentals of visual basic
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Variables and Constants
Introduction to Computer Programming IT-104
Presentation transcript:

Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s name obeys the same rules as when declaring a variable: it begins with a letter and may be followed by any number of letters, underscores, or digits. An array name can be as small as one letter or as large as 255 letters, underscores, and digits. Individual values within an array are selected by using an index. The lowest index of an array is 0, while the upper bound of the array is the highest index in the array. An index must be a Short, an Integer, or a Long data type. The data type is any valid variable like a Short, an Integer, or a String.Arrays

Computer Memory shtGrades (short) Index0123 Values0000 Dim shtGrades(3) As Short

Computer Memory shtGrades (short) Index0123 Values21000 shtGrades(0) = 21 We can assign an individual variable by using its name, and index. Index and subscript are synonymous

Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount As Integer Dim intMaxVal As Integer = 0 For intLoopCount = 0 To UBound(shtAges) If intMaxVal < shtAges(intLoopCount) Then intMaxVal = shtAges(intLoopCount) End If Next intLoopCount bntRedDemo.Text = intMaxVal.ToString Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount As Integer Dim intMaxVal As Integer = 0 For intLoopCount = 0 To UBound(shtAges) If intMaxVal < shtAges(intLoopCount) Then intMaxVal = shtAges(intLoopCount) End If Next intLoopCount bntRedDemo.Text = intMaxVal.ToString Finding the largest value in an array

Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMaxVal As Integer = 0 For intLoopCount = 0 To UBound(shtAges) If intMaxVal < shtAges(intLoopCount) Then intMaxVal = shtAges(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str( intLocation ) Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMaxVal As Integer = 0 For intLoopCount = 0 To UBound(shtAges) If intMaxVal < shtAges(intLoopCount) Then intMaxVal = shtAges(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str( intLocation ) We often want to know the location of the max value..

Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12} Dim intLoopCount, intLocation As Integer Dim intMaxVal As Integer = 0 For intLoopCount = 0 To UBound(shtA) If intMaxVal < shtA(intLoopCount) Then intMaxVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str( intLocation ) Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12} Dim intLoopCount, intLocation As Integer Dim intMaxVal As Integer = 0 For intLoopCount = 0 To UBound(shtA) If intMaxVal < shtA(intLoopCount) Then intMaxVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str( intLocation ) Our code can fail because of an incorrect assumption…

Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12} Dim intLoopCount, intLocation As Integer Dim intMaxVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMaxVal < shtA(intLoopCount) Then intMaxVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str( intLocation ) Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12} Dim intLoopCount, intLocation As Integer Dim intMaxVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMaxVal < shtA(intLoopCount) Then intMaxVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str( intLocation ) Here the incorrect assumption is fixed

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMinVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMinVal > shtA(intLoopCount) Then intMinVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation) Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMinVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMinVal > shtA(intLoopCount) Then intMinVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation) Consider finding the minimum value, again a pessimistic assumption Note relational test Note pessimistic assumption

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMinVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMinVal >= shtA(intLoopCount) Then intMinVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation) Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMinVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMinVal >= shtA(intLoopCount) Then intMinVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation) The previous slide found the first occurrence of the lowest value, this code finds the last occurrence, do you see the difference?

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMinVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMinVal >= shtA(intLoopCount) Then intMinVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation) Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim intLoopCount, intLocation As Integer Dim intMinVal As Integer = For intLoopCount = 0 To UBound(shtA) If intMinVal >= shtA(intLoopCount) Then intMinVal = shtA(intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation) From now on I won’t bother showing this stuff, you can assume it from context

Dim intFindMe As Short = 45 Dim blnWasFound As Boolean = False For intLoopCount = 0 To UBound(shtA) If intFindMe = shtA(intLoopCount) Then blnWasFound = True intLocation = intLoopCount End If Next intLoopCount If blnWasFound Then bntRedDemo.Text = "Found at " & Str(intLocation) Else bntRedDemo.Text = "Not found!" End If Dim intFindMe As Short = 45 Dim blnWasFound As Boolean = False For intLoopCount = 0 To UBound(shtA) If intFindMe = shtA(intLoopCount) Then blnWasFound = True intLocation = intLoopCount End If Next intLoopCount If blnWasFound Then bntRedDemo.Text = "Found at " & Str(intLocation) Else bntRedDemo.Text = "Not found!" End If If IntFindMe was say, 1111 {21, 23, 45, 12, 23, 65, 12} Note the pessimistic assumption. Question: Does it find the first or last occurrence?

Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58} Dim intMaxChange As Integer = 0 For intLoopCount = 0 To UBound(shtA) If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then intMaxChange = shtA (intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Max change at” & Str( intLocation ) Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58} Dim intMaxChange As Integer = 0 For intLoopCount = 0 To UBound(shtA) If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then intMaxChange = shtA (intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Max change at” & Str( intLocation ) Lets try to find the location of the greatest (positive change) Looks good, but there is a bug!

Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58} Dim intMaxChange As Integer = 0 For intLoopCount = 0 To UBound(shtA) - 1 If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then intMaxChange = shtA (intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Max change at” & Str( intLocation ) Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58} Dim intMaxChange As Integer = 0 For intLoopCount = 0 To UBound(shtA) - 1 If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then intMaxChange = shtA (intLoopCount) intLocation = intLoopCount End If Next intLoopCount bntRedDemo.Text = “Max change at” & Str( intLocation ) This fixes our bug…

Let us try to find a repeated item

Dim intInnerLoop, intOuterLoop As Integer Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim blnWasRepeat As Boolean = False For intInnerLoop = 0 To UBound(shtA) bntRedDemo.Text = Str(shtA(intInnerLoop)) Next intInnerLoop Dim intInnerLoop, intOuterLoop As Integer Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim blnWasRepeat As Boolean = False For intInnerLoop = 0 To UBound(shtA) bntRedDemo.Text = Str(shtA(intInnerLoop)) Next intInnerLoop …

Dim intInnerLoop, intOuterLoop As Integer Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim blnWasRepeat As Boolean = False For intOuterLoop = 0 To UBound(shtA) For intInnerLoop = 0 To UBound(shtA) bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop)) Next intInnerLoop Next intOuterLoop Dim intInnerLoop, intOuterLoop As Integer Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim blnWasRepeat As Boolean = False For intOuterLoop = 0 To UBound(shtA) For intInnerLoop = 0 To UBound(shtA) bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop)) Next intInnerLoop Next intOuterLoop …

… intOuterLoop = 0 {21, 23, 45, 12, 23, 65, 12} intInnerLoop = 0 intInnerLoop = 1 intInnerLoop = 2 Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))

… … intOuterLoop = 1 intOuterLoop = 6 {21, 23, 45, 12, 23, 65, 12} … … intOuterLoop = 0

Dim intInnerLoop, intOuterLoop As Integer Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim blnWasRepeat As Boolean = False For intOuterLoop = 0 To UBound(shtA) If intOuterLoop <> intInnerLoop For intInnerLoop = 0 To UBound(shtA) bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop)) Next intInnerLoop End If Next intOuterLoop Dim intInnerLoop, intOuterLoop As Integer Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12} Dim blnWasRepeat As Boolean = False For intOuterLoop = 0 To UBound(shtA) If intOuterLoop <> intInnerLoop For intInnerLoop = 0 To UBound(shtA) bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop)) Next intInnerLoop End If Next intOuterLoop

… … intOuterLoop = 1 intOuterLoop = 6 {21, 23, 45, 12, 23, 65, 12} … … intOuterLoop = 0

Dim blnWasRepeat As Boolean = False For intOuterLoop = 0 To UBound(shtA) For intInnerLoop = 0 To UBound(shtA) If intOuterLoop <> intInnerLoop If shtA(intOuterLoop) = shtA(intOuterLoop) blnWasRepeat = True End If Next intInnerLoop Next intOuterLoop Dim blnWasRepeat As Boolean = False For intOuterLoop = 0 To UBound(shtA) For intInnerLoop = 0 To UBound(shtA) If intOuterLoop <> intInnerLoop If shtA(intOuterLoop) = shtA(intOuterLoop) blnWasRepeat = True End If Next intInnerLoop Next intOuterLoop